MyBatisPlus-02-主键生成策略及CRUD

MyBatisPlus-02-主键生成策略及CRUD

前言

  • 具体雪花算法可以参考:

  • 雪花算法:

    • snowflake是Twitter开源的分布式ID生成算法,结果是一个long型的ID。其核心思想是:使用41bit作为毫秒数,10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID),12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID),最后还有一个符号位,永远是0。可以保证几乎全球唯一!

1. 主键自增

1、实体类字段上 @TableId(type = IdType.AUTO)

2、数据库字段一定要是自增!

2. 其他策略源码

1
2
3
4
5
6
7
8
public enum IdType {
AUTO(0), // 数据库id自增
NONE(1), // 未设置主键
INPUT(2), // 手动输入
ID_WORKER(3), // 默认的全局唯一id
UUID(4), // 全局唯一id uuid
ID_WORKER_STR(5); //ID_WORKER 字符串表示法
}

3. 基本CRUD

3.1 插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 测试插入

@Test

public void testInsert(){

User user = new User();
user.setName("Zhuuu");
user.setAge(3);
user.setEmail("12345678@qq.com");
int result = userMapper.insert(user); // 帮我们自动生成id
System.out.println(result); // 受影响的行数
System.out.println(user); // 发现,id会自动回填
}

3.2 更新

1
2
3
4
5
6
7
8
9
10
11
12
// 测试更新
@Test
public void testUpdate(){
User user = new User();
// 通过条件自动拼接动态sql
user.setId(6L);
user.setName("zhuuu");
user.setAge(18);
// 注意:updateById 但是参数是一个对象!
int i = userMapper.updateById(user);
System.out.println(i);
}

3.3 删除

1、根据 id 删除记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 测试删除
@Test
public void testDeleteById(){
userMapper.deleteById(1L);
}
// 通过id批量删除
@Test
public void testDeleteBatchId(){
userMapper.deleteBatchIds(Arrays.asList(2L,3L
4L));
}
// 通过map删除
@Test
public void testDeleteMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name","Zhuuu");
userMapper.deleteByMap(map);
}

在工作中中会遇到一些问题:逻辑删除!(下一篇博客会说)

3.4 查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 测试查询
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
// 测试批量查询!
@Test
public void testSelectByBatchId(){
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
}
// 按条件查询之一使用map操作
@Test
public void testSelectByBatchIds(){
HashMap<String, Object> map = new HashMap<>();
// 自定义查询
map.put("name","Zhuuu");
map.put("age",3);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}

3.5 分页查询

有以下三种方式进行分页查询

1、原始的 limit 进行分页

2、pageHelper 第三方插件

3、MP 其实也内置了分页插件!


  • 对于MP来说,分页查询的使用

1.配置拦截器组件即可

1
2
3
4
5
// 分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}

2、直接使用Page对象即可!

1
2
3
4
5
6
7
8
9
10
11
// 测试分页查询
@Test
public void testPage(){
// 参数一:当前页
// 参数二:页面大小
// 使用了分页插件之后,所有的分页操作也变得简单的!
Page<User> page = new Page<>(2,5);
userMapper.selectPage(page,null);
page.getRecords().forEach(System.out::println);
System.out.println(page.getTotal());
}

3.6 逻辑删除

物理删除 :从数据库中直接移除

逻辑删除 :再数据库中没有被移除,而是通过一个变量来让他失效! deleted = 0 => deleted = 1

作用 : 管理员可以查看被删除的记录!防止数据的丢失,类似于回收站!

1、在数据表中增加一个 deleted 字段

2、实体类中增加属性

1
2
@TableLogic //逻辑删除
private Integer deleted;

3、配置!

1
2
3
4
5
// 逻辑删除组件!
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
1
2
3
# 配置逻辑删除
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

4、测试一下删除!

  • 记录依旧在数据库,但是值确已经变化了!
  • 以上的所有CRUD操作及其扩展操作
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信