取出刚刚插入的id
增删改查语句使用不同的标签进行处理,查询:select、新增:insert、修改:update、删除:delete。
语句默认返回值 ,引用自mybatis中的sql语句的返回值
select语句
insert语句
update语句
delete语句
实体
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/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/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/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/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 , ""));
}
}
Springboot 2.3.5使用 Mybatis 实现增删改查(单表),并实现增改获取对应的id
原文:https://www.cnblogs.com/xiaqiuchu/p/15150102.html