MyBatis-Plus的几种常见用法

MyBatis-Plus(MP)是一个 MyBatis 的增强工具,它在不改变 MyBatis 核心功能的基础上,为其提供了一系列强大的功能和便捷的操作。以下是 MyBatis-Plus 的几种常见用法,涵盖基本的 CRUD 操作、条件构造器、分页插件和自动填充等内容。

一、基本CRUD操作

MyBatis-Plus 提供了基础的 CRUD 接口,使得开发者无需编写大量重复的 SQL 语句,只需继承相应的 Mapper 接口即可使用。

  1. 新增记录

    @Autowired
    private UserMapper userMapper;
    
    public void addUser(User user) {
        userMapper.insert(user);
    }
    ​
    
  2. 删除记录

    @Autowired
    private UserMapper userMapper;
    
    public void deleteUserById(Long id) {
        userMapper.deleteById(id);
    }
    ​
    
  3. 更新记录

    @Autowired
    private UserMapper userMapper;
    
    public void updateUser(User user) {
        userMapper.updateById(user);
    }
    ​
    
  4. 查询记录

    @Autowired
    private UserMapper userMapper;
    
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }
    
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }
    ​
    
二、条件构造器

条件构造器(Wrapper)是 MyBatis-Plus 提供的强大功能,用于动态构建查询条件,避免拼接 SQL 语句。

  1. 单条件查询

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "John");
    List<User> users = userMapper.selectList(queryWrapper);
    ​
    
  2. 多条件查询

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "John").gt("age", 18);
    List<User> users = userMapper.selectList(queryWrapper);
    ​
    
  3. 模糊查询

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like("name", "John");
    List<User> users = userMapper.selectList(queryWrapper);
    ​
    
三、分页插件

分页查询是开发中常见的需求,MyBatis-Plus 提供了分页插件,简化分页查询的实现。

  1. 配置分页插件

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
    ​
    
  2. 分页查询

    IPage<User> page = new Page<>(1, 10); // 查询第1页,每页10条记录
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.gt("age", 18);
    IPage<User> userPage = userMapper.selectPage(page, queryWrapper);
    ​
    
四、自动填充

MyBatis-Plus 支持自动填充功能,能够在插入和更新时自动填充字段值。

  1. 定义自动填充字段

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    ​
    
  2. 配置自动填充处理器

    @Component
    public class MyMetaObjectHandler implements MetaObjectHandler {
    
        @Override
        public void insertFill(MetaObject metaObject) {
            this.setFieldValByName("createTime", new Date(), metaObject);
        }
    
        @Override
        public void updateFill(MetaObject metaObject) {
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值