❤️❤️❤️SSM专栏更新中,各位大佬觉得写得不错,支持一下,感谢了!❤️❤️❤️
在之前我们讲解了大部分查询相关的操作,接下来进行增删改的学习。
不同的表应用不同的id生成策略
这个时候我们就可以使用@TableId去修改id设置。

我们可以看到一共有八种IdType,也就是说八种id生成策略,在之前我们添加一共新用户的id特别长:

然后我们给user实体类id添加 @TableId注解,并且设置属性type:
- package com.example.domain;
-
- import com.baomidou.mybatisplus.annotation.IdType;
- import com.baomidou.mybatisplus.annotation.TableField;
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.Data;
-
- @Data
- @TableName("tbl_user")
- public class User {
- @TableId(type= IdType.AUTO)
- private Long id;
- private String name;
- @TableField(value = "pwd",select = false)
- private String password;
- private Integer age;
- private String tel;
- @TableField(exist = false)
- private Integer online;
- }
我们去添加新用户试试,看id的变换:
- @Test
- void textSave(){
- User user=new User();
- user.setName("热爱编程");
- user.setPassword("123456");
- user.setAge(60);
- user.setTel("123456789");
- userDao.insert(user);
- }
运行代码,控制台:

数据库中id自动递增了:

我们点进IdType进入查看源码,这里它给出了所有的ID策略:
- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by FernFlower decompiler)
- //
-
- package com.baomidou.mybatisplus.annotation;
-
- public enum IdType {
- AUTO(0),
- NONE(1),
- INPUT(2),
- ASSIGN_ID(3),
- ASSIGN_UUID(4),
- /** @deprecated */
- @Deprecated
- ID_WORKER(3),
- /** @deprecated */
- @Deprecated
- ID_WORKER_STR(3),
- /** @deprecated */
- @Deprecated
- UUID(4);
-
- private final int key;
-
- private IdType(int key) {
- this.key = key;
- }
-
- public int getKey() {
- return this.key;
- }
- }
我们可以看到这些策略后面都有一个数字,数字相同的是同一个策略,只不过是换了一个名字。数字为3的是通过雪花算法生成一个id给你,ID_WORKER(3) 这个是用来生成整数的,而ID_WORKER_STR(3)是用来生成字符串的,它们两个统一合并为ASSIGN_ID(3)。
接下来我们试试将@TableId(type= IdType.AUTO)改成@TableId(type= IdType.INPUT),而在数据库中我们需要将id的自增策略关闭:

这个策略需要我们必须传id值:

不然id为null。
给它设置id继续运行:
- @Test
- void textSave(){
- User user=new User();
- user.setId(123L);
- user.setName("热爱编程");
- user.setPassword("123456");
- user.setAge(60);
- user.setTel("123456789");
- userDao.insert(user);
- }

数据库数据添加:

ASSIGN ID(3): 雪花算法生成id (可兼容数值型与字符串型 )
雪花算法(Snowflake Algorithm)是一种用于生成唯一ID的分布式算法。它最早由Twitter公司提出,并在分布式系统中广泛应用。雪花算法的核心思想是通过组合不同的因素来生成全局唯一的ID。
雪花算法的ID由以下几部分组成:
通过将时间戳、机器ID和序列号进行组合,就可以生成一个全局唯一的ID。雪花算法的优点包括高性能、高可用性和可扩展性,适用于大规模分布式系统中生成唯一ID的需求。
需要注意的是,雪花算法并不保证ID的全局唯一性,而是在实际应用中通过合理的配置和使用来达到足够的唯一性。另外,雪花算法在分布式系统中的应用还需要考虑时钟回拨等异常情况的处理。

这个是不需要自己添加id的:
- @Test
- void textSave(){
- User user=new User();
- user.setName("热爱编程");
- user.setPassword("123456");
- user.setAge(60);
- user.setTel("123456789");
- userDao.insert(user);
- }
可以看到数据库中的id和最开始是一样的,分配的随机id

如果我们一个一个id去添加注解,这是相当麻烦的,所以可以使用全局配置来进行统一的id生成策略。
- # 配置数据库的连接字符串
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/ku2022?characterEncoding=utf8
- username: root
- password: "123456"
- driver-class-name: com.mysql.cj.jdbc.Driver
- main:
- banner-mode: off #不显示logo
- mybatis-plus:
- mapper-locations: classpath:mapper/*Mapper.xml
- configuration: # 配置打印 MyBatis-plus 执行的 SQL
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- banner: false #不显示logo
- db-config:
- id-type: assign_id
设置id-type就可以进行配置id生成策略,不仅仅id可以,上篇提到的表名不同步也可以:
- # 配置数据库的连接字符串
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/ku2022?characterEncoding=utf8
- username: root
- password: "123456"
- driver-class-name: com.mysql.cj.jdbc.Driver
- main:
- banner-mode: off #不显示logo
- mybatis-plus:
- mapper-locations: classpath:mapper/*Mapper.xml
- configuration: # 配置打印 MyBatis-plus 执行的 SQL
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- banner: false #不显示logo
- db-config:
- id-type: assign_id
- table-prefix: tbl_
我们将实体类表名的@TableName("tbl_user")注掉,运行添加用户的代码:

在上面我们添加很多用户名为 热爱编程 的用户,下面我们将他们一起删除:
- @Test
- void testDelete(){
- List
list=new ArrayList<>(); - list.add(6L);
- list.add(123L);
- list.add(1696120064304058370L);
- list.add(1696122438468800513L);
- userDao.deleteBatchIds(list);
- }
就成功删除了:

我们去查询id为1、2、3的用户
- @Test
- void testselect(){
- List
list=new ArrayList<>(); - list.add(1L);
- list.add(2L);
- list.add(3L);
- userDao.selectBatchIds(list);
- }
查询结果为:

删除操作业务问题:业务数据从数据库中丢弃
逻辑删除:为数据设置是否可用状态字段,删除时设置状态字段为不可用状态,数据保留在数据库中。
数据库和实体类中添加deleted字段和属性。

- package com.example.domain;
-
- import com.baomidou.mybatisplus.annotation.IdType;
- import com.baomidou.mybatisplus.annotation.TableField;
- import com.baomidou.mybatisplus.annotation.TableId;
- import com.baomidou.mybatisplus.annotation.TableName;
- import lombok.Data;
-
- @Data
- /*@TableName("tbl_user")*/
- public class User {
- @TableId(type= IdType.ASSIGN_ID)
- private Long id;
- private String name;
- @TableField(value = "pwd",select = false)
- private String password;
- private Integer age;
- private String tel;
- private Integer deleted;
- @TableField(exist = false)
- private Integer online;
- }
- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by FernFlower decompiler)
- //
-
- package com.baomidou.mybatisplus.annotation;
-
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
- public @interface TableLogic {
- String value() default "";
-
- String delval() default "";
- }
查看源码我们可以发现可以设置两个属性值value和delval,分别表示未删除的值和删除的值,这个属性设置会直接影响查找数据和删除数据的SQL(后面会加where deleted)。
- @TableLogic(value = "0",delval = "1")
- private Integer deleted;
- @Test
- void testDelete(){
- userDao.selectById(1L);
- }
控制台输出和数据库更改:

我们逻辑删除了用户1,然后去查看一下能不能查找到用户1:
- @Test
- void testSelect(){
- System.out.println(userDao.selectList(null));
- }

我们可以看到自能查询到四条语句,用户名为1的未查询出来。
这个同样也可以进行全局配置。
- # 配置数据库的连接字符串
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/ku2022?characterEncoding=utf8
- username: root
- password: "123456"
- driver-class-name: com.mysql.cj.jdbc.Driver
- main:
- banner-mode: off #不显示logo
- mybatis-plus:
- mapper-locations: classpath:mapper/*Mapper.xml
- configuration: # 配置打印 MyBatis-plus 执行的 SQL
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- banner: false #不显示logo
- db-config:
- id-type: assign_id
- table-prefix: tbl_
- logic-delete-field: deleted
- logic-delete-value: 1
- logic-not-delete-value: 0
两种方式稍微对比一下就知道对应的含义:

