SpringBoot下Mybatis-plus通用CRUD

本文详细介绍了在SpringBoot环境中使用Mybatis-plus进行通用的增删查改操作,包括insert、updateById、deleteById、selectById等方法的源码解析及测试用例展示。
摘要由CSDN通过智能技术生成

Mybatis-plus通用CRUD

插入操作

insert

  • 方法源码
    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    int insert(T entity);
  • 测试
@Test
public void testInsert(){
   
    User user=new User();
    user.setAge(20);
    user.setName("liuchun");
    user.setUserName("lc");
    user.setEmail("107546184@qq.com");
    user.setPassword("ababa");
    int result = userMapper.insert(user);  //返回的result为受影响的行数
    System.out.println(result);
    System.out.println(user.getId());  // 生成的id会返回user中
}
  • 结果
2021-11-28 09:49:35.217 DEBUG 15160 --- [           main] ltd.lccyj.mapper.UserMapper.insert       : ==>  Preparing: INSERT INTO tb_user ( id, user_name, password, name, age, email ) VALUES ( ?, ?, ?, ?, ?, ? )
2021-11-28 09:49:35.316 DEBUG 15160 --- [           main] ltd.lccyj.mapper.UserMapper.insert       : ==> Parameters: 1464773431795281921(Long), lc(String), ababa(String), liuchun(String), 20(Integer), 107546184@qq.com(String)
2021-11-28 09:49:35.345 DEBUG 15160 --- [           main] ltd.lccyj.mapper.UserMapper.insert       : <==    Updates: 1

发现此处主键ID为自动生成的唯一值而非数据库自增长

  • MD的id生成策略

    /**
     * 生成ID类型枚举类
     *
     * @author hubin
     * @since 2015-11-10
     */
    @Getter
    public enum IdType {
          
        /**
         * 数据库ID自增
         * <p>该类型请确保数据库设置了 ID自增 否则无效</p>
         */
        AUTO(0),
        /**
         * 该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
         */
        NONE(1),
        /**
         * 用户输入ID
         * <p>该类型可以通过自己注册自动填充插件进行填充</p>
         */
        INPUT(2),
    
        /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
        /**
         * 分配ID (主键类型为number或string),
         * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)
         *
         * @since 3.3.0
         */
        ASSIGN_ID(3),
        /**
         * 分配UUID (主键类型为 string)
         * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))
         */
        ASSIGN_UUID(4);
    
        private final int key;
    
        IdType(int key) {
          
            this.key = key;
        }
    }
    
  • 使用@TableId指定id生成策略

    /**
     * tb_user表实体类
     * @author 刘淳
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @TableName("tb_user")
    public class User {
          
        @TableId(type = IdType.AUTO)  // 指定Id生成策略为自增长
        private Long id;
        private String userName;
        private String password;
        private String name;
        private int age;
        private String email;
    }
    

@TableField

该注解可以指定字段的一些属性

  • 对象中的属性名和字段名不一致且非驼峰不一致

    @TableFiled(value="email")  //解决字段名不一致
    private String mail
    
  • 对象中的属性名在表中不存在

    @TableFiled(exist=false)  
    private String address  // 数据库表中不存在该字段
    
  • 设置不参与查询的字段

    @TableFiled(select=false)  // 使该字段不参与查询
    private String password
    

更新操作

updateById

  • 方法源码
    /**
     * 根据 ID 修改
     *
     * @param entity 实体对象
     */
    int updateById(@Param(Constants.ENTITY) T entity);
  • 测试
@Test
public void testUpdateById(){
   
    User user=new User();
    user.setId(1L);
    user.setAge(100);
    int result = userMapper.updateById(user);
    System.out.println(result);
}
  • 结果
2021-11-28 10:13:34.402 DEBUG 2564 --- [           main] ltd.lccyj.mapper.UserMapper.updateById   : ==>  Preparing: UPDATE tb_user SET age=? WHERE id=?
2021-11-28 10:13:34.502 DEBUG 2564 --- [           main] ltd.lccyj.mapper.UserMapper.updateById   : ==> Parameters: 100(Integer), 1(Long)
2021-11-28 10:13:34.513 DEBUG 2564 --- [           main] ltd.lccyj.mapper.UserMapper.updateById   : <==    Updates: 1

update

  • 方法源码
    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象 (set 条件值,可以为 null)
     * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@未安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值