ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
整体结构如图 ![](https://img.kancloud.cn/d1/1a/d11a44fce68831a22fffdbb795dd201d_438x703.png) 一、为方便区分将model中的Article修改为ArticleVO ![](https://img.kancloud.cn/c0/0d/c00d993bc6a813b45e78bba3c0575445_420x181.png) 二、service接口 ~~~ public interface ArticleRestService { ArticleVO saveArticle(ArticleVO article); void deleteArticle(Long id); void updateArticle(ArticleVO article); ArticleVO getArticle(Long id); List<ArticleVO> getAll(); } ~~~ 配置dozer ~~~ <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.4.0</version> </dependency> ~~~ ![](https://img.kancloud.cn/3f/b6/3fb6f33506a3c641ed4331a1ee81db2c_401x90.png) ~~~ public class DozerUtils { static DozerBeanMapper dozerBeanMapper = new DozerBeanMapper(); public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) { List destinationList = Lists.newArrayList(); for (Iterator i$ = sourceList.iterator(); i$.hasNext(); ) { Object sourceObject = i$.next(); Object destinationObject = dozerBeanMapper.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; } } ~~~ ![](https://img.kancloud.cn/76/8d/768d8c4b3f19a2dd50f9cc58ef1cc56f_394x107.png) ~~~ @Configuration public class DozerBeanMapperConfigure { @Bean public DozerBeanMapper mapper() { DozerBeanMapper mapper = new DozerBeanMapper(); return mapper; } } ~~~ service接口实现 ~~~ package com.kimgao.bootlauch.service; import com.kimgao.bootlauch.generator.Article; import com.kimgao.bootlauch.generator.ArticleMapper; import com.kimgao.bootlauch.model.ArticleVO; import com.kimgao.bootlauch.uitls.DozerUtils; import org.dozer.Mapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class ArticleMybatisRestServiceImpl implements ArticleRestService{ @Resource protected Mapper dozerMapper; @Resource private ArticleMapper articleMapper; //新增 @Override public ArticleVO saveArticle(ArticleVO article) { Article articlePO = dozerMapper.map(article,Article.class); articleMapper.insert(articlePO); return null; } //删除 @Override public void deleteArticle(Long id) { articleMapper.deleteByPrimaryKey(id); } //更新 @Override public void updateArticle(ArticleVO article) { Article articlePO = dozerMapper.map(article,Article.class); articleMapper.updateByPrimaryKeySelective(articlePO); } //查询 @Override public ArticleVO getArticle(Long id) { return dozerMapper.map(articleMapper.selectByPrimaryKey(id),ArticleVO.class); } //查询所有 @Override public List<ArticleVO> getAll() { List<Article> articles = articleMapper.selectByExample(null); return DozerUtils.mapList(articles,ArticleVO.class); } } ~~~ 三、修改控制层 ~~~ package com.kimgao.bootlauch.controller; import com.kimgao.bootlauch.AjaxResponse; import com.kimgao.bootlauch.model.ArticleVO; import com.kimgao.bootlauch.service.ArticleRestService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @Slf4j @RestController @RequestMapping("/rest") public class ArticleController { @Resource(name = "articleMybatisRestServiceImpl") ArticleRestService articleRestService; //增加一篇Article ,使用POST方法 //PostMapping 等于 RequestMapping 里的POST其他方法同理 //@RequestMapping(value = "/article", method = POST, produces = "application/json") @PostMapping("/articles") public @ResponseBody AjaxResponse saveArticle(@RequestBody ArticleVO article) { articleRestService.saveArticle(article); //因为使用了lombok的Slf4j注解,这里可以直接使用log变量打印日志 log.info("saveArticle:{}",article); return AjaxResponse.success(article); } //删除一篇Article,使用DELETE方法,参数是id //@PathVariable 是对url定位路径的,用于操作id对应数据 //@RequestMapping(value = "/article/{id}", method = DELETE, produces = "application/json") @DeleteMapping("/articles/{id}") public @ResponseBody AjaxResponse deleteArticle(@PathVariable Long id) { articleRestService.deleteArticle(id); return AjaxResponse.success(id); } //更新一篇Article,使用PUT方法,以id为主键进行更新 //@RequestMapping(value = "/article/{id}", method = PUT, produces = "application/json") @PutMapping("/articles/{id}") public @ResponseBody AjaxResponse updateArticle(@PathVariable Long id, @RequestBody ArticleVO article) { article.setId(id); articleRestService.updateArticle(article); return AjaxResponse.success(article); } //获取一篇Article,使用GET方法 //@RequestMapping(value = "/article/{id}", method = GET, produces = "application/json") @GetMapping("/articles/{id}") public @ResponseBody AjaxResponse getArticle(@PathVariable Long id) { //使用lombok提供的builder构建对象 log.info("get info by id controller"+id); return AjaxResponse.success(articleRestService.getArticle(id)); } @GetMapping("/articles") public @ResponseBody AjaxResponse getAll() { //使用lombok提供的builder构建对象 log.error("Get all info controller"); return AjaxResponse.success(articleRestService.getAll()); } } ~~~ POSTman测试一下