西安 網(wǎng)站 制作百度學術(shù)論文查重入口
總說
過程參考黑馬程序員SpringBoot3+Vue3全套視頻教程,springboot+vue企業(yè)級全棧開發(fā)從基礎(chǔ)、實戰(zhàn)到面試一套通關(guān)_嗶哩嗶哩_bilibili
之前又偷懶幾天。回老家沒事干,玩也玩不好,一玩老是被家里人說。寫代碼吧還是,他們都看不懂,也沒法說我
目錄
總說
一、功能實現(xiàn)
1.1 Controller層
1.2 Service層
1.3 Impl層
1.4 Mapper層
1.5 測試接口
二、參數(shù)校驗
2.1 validation自定義校驗
2.1.1 新建注解
2.1.2 新建校驗
2.1.3 添加注解
2.1.4 測試檢驗
一、功能實現(xiàn)
參數(shù)詳情:
1.1 Controller層
創(chuàng)建ArticleController(之前創(chuàng)建過了就不用創(chuàng)建了),代碼如下:
@RestController
@RequestMapping("/article")
public class ArticleController {@Autowiredprivate ArticleService articleService;@PostMappingpublic Result add(@RequestBody Article article){articleService.add(article);return Result.success();}
}
1.2 Service層
創(chuàng)建ArticleService,代碼如下:
public interface ArticleService {//添加文章void add(Article article);
}
1.3 Impl層
創(chuàng)建ArticleServiceImpl,代碼如下:
同時補充一些屬性值
@Service
public class ArticleServiceImpl implements ArticleService {@Autowiredprivate ArticleMapper articleMapper;@Overridepublic void add(Article article) {//補充屬性值article.setCreateTime(LocalDateTime.now());// 創(chuàng)建時間article.setUpdateTime(LocalDateTime.now());// 更新時間Map<String, Object> map = ThreadLocalUtil.get();Integer id = (Integer)map.get("id");article.setCreateUser(id);// 用戶idarticleMapper.add(article);}
}
1.4 Mapper層
創(chuàng)建ArticleMapper,代碼如下:
@Mapper
public interface ArticleMapper {//新增文章@Insert("insert into article (title, content, cover_img, state, category_id, create_user, create_time, update_time)" +" values (#{title}, #{content}, #{coverImg}, #{state}, #{categoryId}, #{createUser}, #{createTime}, #{updateTime})")void add(Article article);
}
1.5 測試接口
創(chuàng)建一個新接口,如下:
二、參數(shù)校驗
上面的新增文章過程中,沒有對參數(shù)信息校驗,我們對參數(shù)的要求如下:
來到pojo層的Article
2.1 validation自定義校驗
2.1.1 新建注解
創(chuàng)建一個新目錄anno
在這個目錄下寫一個注解State,如下圖:
加入代碼如下:
@Documented //元注解,用于描述注解的元數(shù)據(jù)
//@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Target({ElementType.FIELD}) //元注解,用于描述注解的適用范圍,這里只用在屬性上,只留field
@Retention(RetentionPolicy.RUNTIME)//元注解,用于描述注解的生命周期,這里只保留在運行時@Constraint(validatedBy = {StateValidation.class} ) //元注解,用于指定是哪個類為該注解提供校驗規(guī)則public @interface State {//提供校驗失敗的提示信息String message() default "state的參數(shù)值只能是已發(fā)布或者草稿";//指定分組Class<?>[] groups() default {};//指定負載,獲取到State注解的附加信息Class<? extends Payload>[] payload() default {};
}
2.1.2 新建校驗
創(chuàng)建一個新目錄validation
在這個目錄下寫一個注解StateValidation,如下圖:
代碼如下:
//<a,b> 第一個參數(shù)a是表示給哪個注解提供校驗規(guī)則,第二個參數(shù)b是表示校驗的數(shù)據(jù)類型
public class StateValidation implements ConstraintValidator<State, String> {//value是將來要校驗的數(shù)據(jù),@Overridepublic boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {//提供校驗規(guī)則//return:false校驗不通過,true校驗通過if(value == null)return false;if(value.equals("已發(fā)布") || value.equals("草稿"))return true;return false;}}
2.1.3 添加注解
我們來帶pojo層的Article,修改代碼如下:
@Data
public class Article {private Integer id;//主鍵ID//1~10個非空字符@NotEmpty@Pattern(regexp = "^\\S{1,10}$")private String title;//文章標題@NotEmptyprivate String content;//文章內(nèi)容@NotEmpty@URLprivate String coverImg;//封面圖像@Stateprivate String state;//發(fā)布狀態(tài) 已發(fā)布|草稿@NotNullprivate Integer categoryId;//文章分類idprivate Integer createUser;//創(chuàng)建人IDprivate LocalDateTime createTime;//創(chuàng)建時間private LocalDateTime updateTime;//更新時間
}
2.1.4 測試檢驗
我們將state的值刪去,得到結(jié)果如下:
將結(jié)果上傳git保存