参考
知识点
-
取出刚刚插入的id
- 通过给insert、update的xml映射语句设置useGeneratedKeys与keyProperty属性,可以让mybatis取出由数据库内部生成的主键,如果生成列不止一个,可以用逗号分隔多个属性名称。
- 需要将文章实体实例化之后作为参数传入Mapper的查询方法,这样mybatis就会按照 1 的设置将新增/修改的id传入到文章实体内,然后把文章实体返回给控制器,控制器就能够获取到刚刚插入/修改的id了。
-
增删改查语句使用不同的标签进行处理,查询:select、新增:insert、修改:update、删除:delete。
-
语句默认返回值 ,引用自mybatis中的sql语句的返回值
-
select语句
- 正确执行时,返回查询的结果或结果集
- 未查询到结果,返回值为null
-
insert语句
- 正确执行时,返回在数据库中影响的行数
- 插入数据失败,抛出com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
-
update语句
- 正确执行时,返回在数据库中匹配的行数
- 插入数据失败,抛出com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
-
delete语句
- 正确执行时,返回在数据库中影响的行数
- 插入数据失败,抛出com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
-
(单表)实现增删改查代码
-
实体
- 文章实体
entity/ArticleEntity.java
/** * 文章实体 * @Author 夏秋初 * @Date 2021/8/13 10:10 */ @Data @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public class ArticleEntity implements Serializable { /** * 序列化安全 */ private static final long serialVersionUID = 1L; private Integer id; private String title; private String introduction; private Date createdAt; private Date updatedAt; private String content; private Integer shows; private Integer likes; }
- 文章添加Dto
dto/admin/request/ArticleAddDto.java
/** * @Author 夏秋初 * @Date 2021/8/16 10:45 */ @Data public class ArticleAddDto { private String title; private String introduction; private String content; }
- 文章更新Dto
dto/admin/request/ArticleUpdateDto.java
/** * @Author 夏秋初 * @Date 2021/8/16 10:45 */ @Data public class ArticleUpdateDto { private String title; private String introduction; private String content; }
- 文章实体
-
文章xml映射文件
resources/mappers/ArticleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--这里对应Maper接口类的命名空间-->
<mapper namespace="com.xxx.blog.mapper.ArticleMapper">
<!-- 这里 resultType 对应响应实体类,也可以是其他类型,请参考文档-->
<select id="findById" parameterType="int" resultType="com.xxx.blog.entity.ArticleEntity">
select * from `articles` where id = #{id}
</select>
<select id="findByPaging" resultType="com.xxx.blog.entity.ArticleEntity" parameterType="map">
select
*
from `articles`
</select>
<insert id="add" useGeneratedKeys="true"
keyProperty="id" parameterType="com.xxx.blog.entity.ArticleEntity">
insert into articles (title, introduction,content,created_at,updated_at) values(#{title}, #{introduction},#{content},#{createdAt},#{updatedAt})
</insert>
<update id="update" useGeneratedKeys="true" keyProperty="id" parameterType="com.xxx.blog.entity.ArticleEntity">
update articles set
title = #{title}, introduction = #{introduction}, content=#{content}, updated_at = #{updatedAt}
where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete from articles where id = #{id}
</delete>
</mapper>
- 文章Mapper类
mapper/ArticleMapper.java
/**
* @Author 夏秋初
* @Date 2021/8/13 10:24
*/
@Mapper
public interface ArticleMapper {
public ArticleEntity findById(Integer id);
public List<ArticleEntity> findByPaging(Map param);
public Integer add(ArticleEntity articleEntity);
public Integer update(ArticleEntity articleEntity);
public Integer delete(Integer id);
}
- 文章 service
service/ArticleService.java
/**
* @Author 夏秋初
* @Date 2021/8/13 10:24
*/
@Service
public class ArticleService {
@Autowired
ArticleMapper articleMapper;
public ArticleEntity findById(Integer id){
return articleMapper.findById(id);
}
/**
* 分页查询
* @param pageNum
* @param pageSize
* @param map
* @return
*/
public PageInfo findByPaging(Integer pageNum, Integer pageSize,Map map){
PageHelper.startPage(pageNum, pageSize);
List<ArticleEntity> articleEntityPage = articleMapper.findByPaging(map);
PageInfo pageInfo = new PageInfo(articleEntityPage);
return pageInfo;
}
/**
* 创建一个文章
* @param articleAddDto
* @return
*/
public ArticleEntity add(ArticleAddDto articleAddDto){
ArticleEntity articleEntity = new ArticleEntity();
articleEntity.setTitle(articleAddDto.getTitle());
articleEntity.setIntroduction(articleAddDto.getIntroduction());
articleEntity.setContent(articleAddDto.getContent());
Date date = new Date();
articleEntity.setCreatedAt(date);
articleEntity.setUpdatedAt(date);
articleMapper.add(articleEntity);
return articleEntity;
}
/**
* 更新一个文章
* @param articleUpdateDto
* @return
*/
public ArticleEntity update( Integer id,ArticleUpdateDto articleUpdateDto){
ArticleEntity articleEntity = new ArticleEntity();
articleEntity.setId(id);
articleEntity.setTitle(articleUpdateDto.getTitle());
articleEntity.setIntroduction(articleUpdateDto.getIntroduction());
articleEntity.setContent(articleUpdateDto.getContent());
articleEntity.setUpdatedAt(new Date());
articleMapper.update(articleEntity);
return articleEntity;
}
public Integer delete( Integer id){
return articleMapper.delete(id);
}
}
- 文章控制器
/**
* @Author 夏秋初
* @Date 2021/8/13 10:26
*/
@RestController
@RequestMapping("admin/v1/article")
public class ArticleController {
@Autowired
ArticleService articleService;
/**
* 获取文章分页
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping(value = "", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<PageInfo>> list(@RequestParam(name = "page_num", defaultValue = "1") Integer pageNum, @RequestParam(name = "page_size",defaultValue = "10") Integer pageSize){
Map<String, String> map = new HashMap<>();
PageInfo pageInfo = articleService.findByPaging(pageNum, pageSize, map);
return ResponseEntity.ok(new CustomResponseStructureDto(0, pageInfo, "成功"));
}
/**
* 获取指定id
* @param id
* @return
*/
@GetMapping(value = "{id}", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> list(@PathVariable("id") Integer id){
ArticleEntity article = articleService.findById(id);
return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, article, "成功"));
}
/**
* 新增文章
* @param articleAddDto
* @return
*/
@PostMapping(value = "", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> add(@RequestBody ArticleAddDto articleAddDto){
return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, articleService.add(articleAddDto), ""));
}
/**
* 更新文章
* @param id
* @param articleUpdateDto
* @return
*/
@PutMapping(value = "{id}", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<ArticleEntity>> add(@PathVariable("id") Integer id, @RequestBody ArticleUpdateDto articleUpdateDto){
return ResponseEntity.ok(new CustomResponseStructureDto<ArticleEntity>(0, articleService.update(id, articleUpdateDto), ""));
}
/**
* 删除文章
* @param id
* @return
*/
@DeleteMapping(value = "{id}", produces = "application/json;charset=UTF-8")
public ResponseEntity<CustomResponseStructureDto<Object>> delete(@PathVariable("id") Integer id){
articleService.delete(id);
return ResponseEntity.ok(new CustomResponseStructureDto<Object>(0, null , ""));
}
}