Mybatis plus使用

本文介绍了Mybatis Plus的使用,包括如何通过多个ID批量查询、简单条件查询、条件构造器和常用接口的运用。还涉及到乐观锁的概念及实现方式,以及逻辑删除的配置和使用。同时提到了Service层的CRUD接口及其优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入依赖;

<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>

正常连接数据库:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

编写mapper接口,并继承baseMapper接口(指定泛型)

@Repository
public interface UserMapper extends BaseMapper<User> {
}

启动类接口包扫描:

@MapperScan("com.sen.demomptest.mapper")

Mybatis plus有查看SQL输出日志:在配置文件中配置:

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

数据插入操作:

指定主键策略:

 自动填充:

需求描述:

项目中经常会遇到一些数据,每次都使用相同的方式填充,例如记录的创建时间,更新时间等。

1、在实体类中添加注解,表示自动填充:

@TableField(fill = FieldFill.INSERT)
private Date createTime;  //create_time

@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime; //update_time

2、实现元素对象处理接口:

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //mp执行添加操作,这个方法执行
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
        this.setFieldValByName("version",1,metaObject);//新建数据的时候,乐观锁版本值设置为1
    }

    //mp执行修改操作,这个方法执行
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}

乐观锁:(通过添加一个版本号进行控制,事务版本号一致,可以提交,不一致则放弃提交)

主要适用场景:当要更新一条记录的时候,希望这条记录没有被别人更新,也就是说实现线程安全的数据更新

乐观锁实现方式:

取出记录时,获取当前version

更新时,带上这个version

执行更新时, set version = newVersion where version = oldVersion

如果version不对,就更新失败

1、数据表添加一个版本的字段,

2、实体类添加相应的属性并在实体类中的属性中添加一个version注解

3、在配置类中注册乐观锁插件

/**
* 乐观锁插件
*/
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}

查询

通过多个id批量查询

完成了动态sql的foreach的功能

//多个id批量查询
@Test
public void testSelect1() {
    List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
    System.out.println(users);
}

简单的条件查询

通过map封装查询条件

注意:map中的key对应数据库中的列名。如:数据库user_id,实体类是userId,这时map的key需要填写user_id

//简单条件查询
@Test
public void testSelect2() {
    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("name","Jack");
    columnMap.put("age",20);
    List<User> users = userMapper.selectByMap(columnMap);
}

分页查询:

1、分页插件

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

在配置类中添加分页插件:

/**
 * 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
}

编写分页的查询:

Page<User> page = new Page(1,3);
Page<User> userPage = userMapper.selectPage(page, null);
//返回对象得到分页所有数据
long pages = userPage.getPages(); //总页数
long current = userPage.getCurrent(); //当前页
List<User> records = userPage.getRecords(); //查询数据集合
long total = userPage.getTotal(); //总记录数
boolean hasNext = userPage.hasNext();  //下一页
boolean hasPrevious = userPage.hasPrevious(); //上一页

其中提供了类似的分页查询方法,selectMapsPage。

//Page不需要泛型
Page<Map<String, Object>> page = newPage<>(1, 5);
Page<Map<String, Object>> pageParam = userMapper.selectMapsPage(page, null);
List<Map<String, Object>> records = pageParam.getRecords();
records.forEach(System.out::println);
System.out.println(pageParam.getCurrent());
System.out.println(pageParam.getPages());
System.out.println(pageParam.getSize());
System.out.println(pageParam.getTotal());
System.out.println(pageParam.hasNext());
System.out.println(pageParam.hasPrevious());

删除:

deleteById根据ID删除数据
deleteBatchIds根据多个ID批量删除数据
deleteByMap根据map集合条件删除相应数据

逻辑删除:

物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除数据

逻辑删除:假删除,将对应数据中代表是否被删除字段状态修改为“被删除状态”,之后在数据库中仍旧能看到此条数据记录

逻辑删除的实现:

在数据表中添加一个表示删除状态的字段:

ALTERTABLE `user` ADD COLUMN `deleted` boolean DEFAULT false

在实体类中添加相应的属性,并加上@TableLogic注解

@TableLogic
private Integer deleted;

application.properties 加入以下配置,此为默认值,如果你的默认值和mp默认的一样,该配置可无

mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

此时,再删除带删除字段的数据,就是逻辑删除,,默认修改状态值,相应的查询数据也是一样,只能查询出未删除的数据,即运行的SQL语句逻辑后面加上where deleted=0

条件构造器和常用接口

wapper介绍

Wrapper : 条件构造抽象类,最顶端父类  

    AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件

        QueryWrapper : 查询条件封装

        UpdateWrapper : Update 条件封装

    AbstractLambdaWrapper : 使用Lambda 语法

        LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper

        LambdaUpdateWrapper : Lambda 更新封装Wrapper

其中构造的查询方法有:

查询方式

说明

setSqlSelect

设置 SELECT 查询字段

where

WHERE 语句,拼接 + WHERE 条件

and

AND 语句,拼接 + AND 字段=值

andNew

AND 语句,拼接 + AND (字段=值)

or

OR 语句,拼接 + OR 字段=值

orNew

OR 语句,拼接 + OR (字段=值)

eq

等于=

allEq

基于 map 内容等于=

ne

不等于<>

gt

大于>

ge

大于等于>=

lt

小于<

le

小于等于<=

like

模糊查询 LIKE  等同 LIKE ‘%值%’

notLike

模糊查询 NOT LIKE

likeLeft模糊查询  LIKE ‘%值’
likeRight模糊查询  LIKE ‘值%’

in

IN 查询

notIn

NOT IN 查询

isNull

NULL 值查询

isNotNull

IS NOT NULL

groupBy

分组 GROUP BY

having

HAVING 关键词

orderBy

排序 ORDER BY

orderAsc

ASC 排序 ORDER BY

orderDesc

DESC 排序 ORDER BY

exists

EXISTS 条件语句

notExists

NOT EXISTS 条件语句

between

BETWEEN 条件语句

notBetween

NOT BETWEEN 条件语句

addFilter

自由拼接 SQL

last

拼接在最后,例如:last(“LIMIT 1”)

2.3 Service CRUD接口

另外一套CRUD是Service层的,只需要编写一个接口,继承IService,并创建一个接口实现类,即可食用。(这个接口提供的CRUD方法,和Mapper接口提供的功能大同小异,比较明显的区别在于IService支持了更多的批量化操作,如saveBatch,saveOrUpdateBatch等方法。

  1. 首先,新建一个接口,继承IService
  2. package com.example.mp.service;  
    import com.baomidou.mybatisplus.extension.service.IService;  
    import com.example.mp.po.User;  
    
    public interface UserService extends IService<User> {  
    }

  3. 创建这个接口的实现类,并继承ServiceImpl,最后打上@Service注解,注册到Spring容器中
package com.example.mp.service.impl;  

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;  
import com.example.mp.mappers.UserMapper;  
import com.example.mp.po.User;  
import com.example.mp.service.UserService;  
import org.springframework.stereotype.Service;  

@Service  
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }

 IService也支持链式调用,代码写起来非常简洁,查询示例如下:

@Test  
public void testChain() {  
    userService.lambdaUpdate()  
        .gt(User::getAge, 39)  
        .likeRight(User::getName, "王")  
        .set(User::getEmail, "w39@baomidou.com")  
        .update();  
}

mybatis-plus
也可以使用mp提供的Wrapper条件构造器,来自定义SQL

注解方式:

public interface UserMapper extends BaseMapper<User> {  

 // SQL中不写WHERE关键字,且固定使用${ew.customSqlSegment}  
 @Select("select * from user ${ew.customSqlSegment}")  
 List<User> findAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper);  
}

Mybatis PlusMybatis 的一个增强工具包,提供了许多实用的功能,例如自动分页、自动填充、逻辑删除、多租户等。下面简单介绍一下使用 Mybatis Plus 的步骤: 1. 引入 Mybatis Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 配置数据源,可以使用 Mybatis Plus 提供的 `MybatisPlusInterceptor` 实现自动分页: ```java @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource); // 添加分页插件 MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); sqlSessionFactory.setPlugins(interceptor); return sqlSessionFactory.getObject(); } } ``` 3. 创建实体类和对应的 Mapper 接口: ```java @Data @AllArgsConstructor @NoArgsConstructor @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; } public interface UserMapper extends BaseMapper<User> { } ``` 4. 使用 Mybatis Plus 提供的通用方法进行 CRUD 操作: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public List<User> list() { return userMapper.selectList(null); } public User getById(Long id) { return userMapper.selectById(id); } public boolean save(User user) { return userMapper.insert(user) > 0; } public boolean updateById(User user) { return userMapper.updateById(user) > 0; } public boolean removeById(Long id) { return userMapper.deleteById(id) > 0; } } ``` 以上就是使用 Mybatis Plus 的简单步骤,更多详细的使用方法可以参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值