做宣傳圖冊在什么網(wǎng)站浙江企業(yè)seo推廣
目錄
發(fā)布文章
接口文檔
?業(yè)務(wù)實現(xiàn)
自定義參數(shù)校驗
項目參數(shù)要求?
實現(xiàn)思路?
實現(xiàn)步驟
文章列表(條件分頁)
接口文檔?
業(yè)務(wù)實現(xiàn)?
mapper映射?
更新文章
?接口文檔
?業(yè)務(wù)實現(xiàn)
獲取文章詳情?
接口文檔?
業(yè)務(wù)實現(xiàn)?
刪除文章?
接口文檔?
業(yè)務(wù)實現(xiàn)?
文章管理業(yè)務(wù)表結(jié)構(gòu)
發(fā)布文章
接口文檔
?業(yè)務(wù)實現(xiàn)
創(chuàng)建ArticleController類并完成請求的方法
@RestController
@RequestMapping("/article")
public class ArticleController {@Autowiredprivate ArticleService articleService; //注入ArticleService接口@PostMappingpublic Result add(@RequestBody Article article){articleService.add(article);return Result.success();}
}
創(chuàng)建ArticleService接口并完成方法
public interface ArticleService {//新增文章void add(Article article);
}
創(chuàng)建ArticleServiceimpl接口實現(xiàn)類并完成方法
@Service
public class ArticleServiceimpl implements ArticleService {@Autowiredprivate ArticleMapper articleMapper; //注入ArticleMapper接口@Overridepublic void add(Article article) {//補(bǔ)充id屬性值 用于添加到create_usera字段中Map<String,Object> map = ThreadLocalUtil.get();Integer id = (Integer) map.get("id");article.setCreateUser(id);articleMapper.add(article);}
}
創(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},now(),now())")void add(Article article);
}
運(yùn)行請求查看?
?數(shù)據(jù)庫中查看已添加成功
自定義參數(shù)校驗
? ? 已有的注解不能滿足所有的校驗需求,特殊的情況需要自定義校驗(自定義校驗注解)?
項目參數(shù)要求?
實現(xiàn)思路?
- 自定義注解State
- 自定義校驗數(shù)據(jù)的類StateValidation 實現(xiàn)ConstraintValidator接口
- 在需要校驗的地方使用自定義注解?
?實現(xiàn)步驟
?在Article實體類中對能滿足校驗要求的成員變量進(jìn)行校驗
在ArticleController接口中對方法參數(shù)使用@Validated注解?
但對于state變量參數(shù)已有的注解不能滿足所有的校驗需求,所以需要對其使用自定義參數(shù)校驗。
新建anno包,在包下新定義State注解,并完善定義注解的代碼
@Documented //元注解 用于抽取自定義的注解到幫助文檔
@Target({ElementType.FIELD}) //元注解 自定義的標(biāo)注用在哪些地方 FIELD表示在變量屬性上標(biāo)注
@Retention(RetentionPolicy.RUNTIME) //元注解 用于標(biāo)識自定義的注解會在哪一階段保留 RUNTIME表示運(yùn)行階段
@Constraint(validatedBy = {}) //用于指定誰給自定義的注解定義參數(shù)校驗規(guī)則
public @interface State {//message用于提供校驗失敗后的提示信息String message() default "'state參數(shù)的值只能是已發(fā)布或者草稿";//用于指定分組Class<?>[] groups() default {};//負(fù)載 獲取到State注解的附如信息Class<? extends Payload>[] payload() default {};
}
新建validation包,在包下定義StateValidation校驗規(guī)則類,并完善校驗規(guī)則代碼
public class StateValidation implements ConstraintValidator<State,String> { //接口的泛型:<會給哪個注解提供校驗規(guī)則,校驗的數(shù)據(jù)類型>/***參數(shù)解釋:** value:將來要校驗的數(shù)據(jù)* context* return: 如果返回false則校驗不通過, 如果返回true則校驗通過*/@Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {//提供校驗規(guī)則if(value == null){return false;}if(value.equals("已發(fā)布") || value.equals("草稿")){return true;}return false; //其余情況返回false}
}
?再回到State注解中完善要指定的校驗規(guī)則
到實體類中在需要的成員變量使用該自定義注解用于達(dá)到注解參數(shù)校驗的目的
文章列表(條件分頁)
接口文檔?
業(yè)務(wù)實現(xiàn)?
創(chuàng)建PageBean實體類?
//分頁返回結(jié)果對象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean <T>{private Long total;//總條數(shù)private List<T> items;//當(dāng)前頁數(shù)據(jù)集合
}
編寫ArticleController中的請求的方法
@GetMappingpublic Result<PageBean<Article>> list(Integer pageNum,Integer pageSize, //使用@RequestParam(required = false)可以使參數(shù)設(shè)置為可傳入也可不傳入@RequestParam(required = false) String categoryId,@RequestParam(required = false) String state){PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);return Result.success(pb);}
編寫ArticleService接口的方法
//條件分頁列表查詢PageBean<Article> list(Integer pageNum, Integer pageSize, String categoryId, String state);
}
在pom文件導(dǎo)入pagehelper插件依賴用于完成分頁查詢?
<!-- pagehelper插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version></dependency>
編寫ArticleServiceimpl接口實現(xiàn)類的方法
@Overridepublic PageBean<Article> list(Integer pageNum, Integer pageSize, String categoryId, String state) {//定義pageBean對象PageBean<Article> pb = new PageBean<>();//開啟分頁查詢 PageHelperPageHelper.startPage(pageNum,pageSize);//添加id參數(shù) 調(diào)用mapper接口的方法Map<String,Object> map = ThreadLocalUtil.get();Integer id = (Integer) map.get("id");List<Article> arlist = articleMapper.list(id,categoryId,state);//page中提供了方法可以獲取pagehelper分頁查詢后,得到的總記錄條數(shù)和當(dāng)前頁數(shù)Page<Article> p = (Page<Article>) arlist;//將page對象獲取的記錄和條數(shù)添加到pagebean對象中pb.setTotal(p.getTotal());pb.setItems(p.getResult());return pb; //返回pagebean對象}
編寫ArticleMapper接口的方法?
//條件分頁列表查詢List<Article> list(Integer id, String categoryId, String state);
mapper映射?
由于參數(shù)?categoryId和state參數(shù)為非必填,所以這里sql需要用到sql映射文件來寫動態(tài)sql
在resource目錄下創(chuàng)建和mapper包一樣的結(jié)構(gòu)目錄
目錄結(jié)構(gòu)要和mapper接口目錄相同
配置文件名稱要和接口名稱相同
編寫sql映射配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- 命名空間要和mapper接口的路徑全類名要相同 -->
<mapper namespace="com.springboot.springboot_test.mapper.ArticleMapper">
<!-- 編寫動態(tài)sql
select標(biāo)簽的
id參數(shù)要和mapper接口的方法名稱一致
resultType 返回值類型要和實體類名稱一致--><select id="list" resultType="com.springboot.springboot_test.pojo.Article">select * from article<where><if test="categoryId != null">category_id = #{categoryId}</if><if test="state != null">and state = #{state}</if>and create_user= #{id}</where></select></mapper>
運(yùn)行請求查看
?
更新文章
?接口文檔
?業(yè)務(wù)實現(xiàn)
編寫ArticleController中的請求的方法
@PutMappingpublic Result update(@RequestBody @Validated Article article){articleService.update(article);return Result.success();}
編寫ArticleService接口的方法
//更新文章void update(Article article);
編寫ArticleServiceimpl接口實現(xiàn)類的方法
@Overridepublic void update(Article article) {articleMapper.update(article);}
編寫ArticleMapper接口的方法
//更新文章@Update("update article set title = #{title},content = #{content},cover_img = #{coverImg},state = #{state}, " +"category_id = #{categoryId},update_time = now() where id = #{id}")void update(Article article);
運(yùn)行請求查看
?
獲取文章詳情?
接口文檔?
業(yè)務(wù)實現(xiàn)?
編寫ArticleController中的請求的方法
@GetMapping("/detail")public Result detail(Integer id){Article ar = articleService.findById(id);return Result.success(ar);}
編寫ArticleService接口的方法
//查看文章詳情Article findById(Integer id);
編寫ArticleServiceimpl接口實現(xiàn)類的方法
@Overridepublic Article findById(Integer id) {Article ar = articleMapper.findById(id);return ar;}
編寫ArticleMapper接口的方法
//查看文章詳情@Select("select * from article where id =#{id}")Article findById(Integer id);
?運(yùn)行請求查看
刪除文章?
接口文檔?
業(yè)務(wù)實現(xiàn)?
編寫ArticleController中的請求的方法
@DeleteMappingpublic Result delete(Integer id){articleService.delete(id);return Result.success();}
編寫ArticleService接口的方法
//刪除文章void delete(Integer id);
編寫ArticleServiceimpl接口實現(xiàn)類的方法
@Overridepublic void delete(Integer id) {articleMapper.delete(id);}
編寫ArticleMapper接口的方法
//刪除文章@Delete("delete from article where id = #{id}")void delete(Integer id);
運(yùn)行請求查看
?
?