单表的CRUD
单表 crud 比较简单 、直接看怎么建立通用代码生成器模板
生成 Live Template 组
PageHelper 插件的使用
分页查询至少需要两条 SQL
一条查总记录数(通过每页条数计算出总共有多少页)
一条查当前页的记录
- 引入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.5</version>
</dependency>
- 语法规则
分页功能关键字 : limit.
limit 1 相当于 limit 0 1 从第0行开始 查1条
插页分件语法规则 调用它后 往后遇到的第一个 【select】语句会进行分页
public List<ChapterDto> list() {
PageHelper.startPage(1,1); //直接写这句就调用了 对应 limit 0 1
ChapterExample chapterExample = new ChapterExample();
List<Chapter> chapters = chapterMapper.selectByExample(chapterExample);
List<ChapterDto> chapterDtoList = new ArrayList<>();
for (Chapter chapter : chapters) {
ChapterDto chapterDto = new ChapterDto();
BeanUtils.copyProperties(chapter,chapterDto);
chapterDtoList.add(chapterDto);
}
return chapterDtoList;
}
- 分页DTO类
public class PageDto<T> {
private int page;
private int size;
private long total;
private List<T> list;
public int getPage() {
return page;
}
public int getSize() {
return size;
}
public void setTotal(long total) {
this.total = total;
}
public void setList(List<T> list) {
this.list = list;
}
- dto类中 total 和list 经过 pageInfo 得到可以设置
- 最后直接返回设置好的 pageDto 即可。
- 测试结果
边界值测试
小知识
Live Template
private static final Logger LOG = LoggerFactory.getLogger(ChapterServiceImpl.class);
post 参数接收
jquery 默认 表单 vue 默认 json 流。
自增ID 的问题
1。 id 连续、容易被探测 2、分布式存储中、id会出现重复 3、需要 +1 次才能查询到 id 的值。
分布式架构下建议使用 雪花算法 生成ID