Mybatis Plus简单使用

1. Mybatis Plus简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

  • 润物无声:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。
  • 效率至上:只需简单配置,即可快速进行 CRUD 操作,从而节省大量时间。
  • 丰富功能:热加载、代码生成、分页、性能分析等功能一应俱全。

2. 学习环境搭建

数据库环境

首先我们在数据库中创建一个user表

CREATE TABLE USER
(
    id BIGINT(20)NOT NULL COMMENT '主键ID',
    NAME VARCHAR(30)NULL DEFAULT NULL COMMENT '姓名',
    age INT(11)NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50)NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);

插入一些测试数据

INSERT INTO user (id, name, age, email)VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

Java项目配置

在我们创建的Springboot项目中添加相关的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1</version>
    </dependency>

    <!--mysql依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--lombok用来简化实体类-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

在application.properties配置文件中配置数据库连接的相关信息

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=1234
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

在启动类中添加@MapperScan注解来扫描Mapper包

@SpringBootApplication
@MapperScan("com.atguigu.demomptest.mapper")
public class DemomptestApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemomptestApplication.class, args);
    }
}

创建实体类对象

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

mapper包下创建UserMapper接口

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

到此位置,我们的学习环境就搭建完毕,下面开始在测试类中进行测试MP的相关API

3. 简单插入操作

3.1 语法

我们可以直接调用MP中提供的insert方法实现对数据的插入:

@Test
public void testAdd() {
    User user = new User();
    user.setName("lucy");
    user.setAge(20);
    user.setEmail("1243@qq.com");
    int insert = userMapper.insert(user);
    System.out.println(insert);
}

在这里插入图片描述

我们发现我们在插入数据的时候没有指定ID,但是MP插入的时候也插入了ID,这是因为MP默认的主键策略是通过雪花算法为插入的记录自动生成一个ID。

3.2 MP的主键策略

MP中的主键策略有很多中,最常用的是ASSIGN_ID和AUTO两种。

3.2.1 ASSIGN_ID

ASSIGN_ID是通过雪花算法实现ID。雪花算法是由Twitter公布的分布式主键生成算法,它能够保证不同表的主键的不重复性,以及相同表的主键的有序性

核心思想:

  • 长度共64bit(一个long型)。
  • 首先是一个符号位,1bit标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0。
  • 41bit时间截(毫秒级),存储的是时间截的差值(当前时间截 - 开始时间截),结果约等于69.73年。
  • 10bit作为机器的ID(5个bit是数据中心,5个bit的机器ID,可以部署在1024个节点)。
  • 12bit作为毫秒内的流水号(意味着每个节点在每毫秒可以产生 4096 个 ID)。

在这里插入图片描述
雪花算法的优点是:整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞,并且效率较高。

3.2.2 AUTO

需要在创建数据表的时候设置主键自增,和在数据库中我们设置字段自增是一样的。

3.3 如何设置MP的主键策略

MP主键策略默认为ASSIGN_ID,可以在实体类字段上通过@TableId注解的方式来显示指定主键策略

@TableId(type = IdType.AUTO)
private Long id;
@TableId(type = IdType.ASSIGN_ID)
private String id;

也可以在配置文件中指定全局的主键策略

#全局设置主键生成策略
mybatis-plus.global-config.db-config.id-type=auto

4. 自动填充

项目中经常会遇到一些数据,每次都使用相同的方式填充,例如记录的创建时间,更新时间等。我们可以使用MyBatis Plus的自动填充功能,完成这些字段的赋值工作。

4.1 实现创建和更新时间的自动填充

在User表中添加datetime类型的新的字段 create_time、update_time

ALTERTABLE `user` ADD COLUMN create_time date 
ALTERTABLE `user` ADD COLUMN update_time date 

修改实体类

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

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

通过@TableField主键来指定这个字段什么时候被自动填充,比如createTime字段就是在插入的时候需要填充,而更新字段需要在插入和更新的时候填充

实现元数据处理器接口

@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);
    }

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

注意别忘了添加@Component注解

5. 查询

5.1 根据ID批量查询

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

5.2 简单条件查询

@Test
public void testSelect2() {
    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("name","Jack");
    columnMap.put("age",20);
    List<User> users = userMapper.selectByMap(columnMap);
    System.out.println(users);
}

这里通过map封装查询条件。map中的key对应数据库中的列名。如:数据库user_id,实体类是userId,这时map的key需要填写user_id

5.3 分页查询

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

配置分页插件

在MP的配置类中注册一个bean

@Bean
public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
}

测试分页相关功能

//分页查询
@Test
public void testSelectPage() {
    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(); //上一页
}

6. 删除

6.1 物理删除

6.1.1 根据ID删除
@Test
public void testDeleteById(){
    int result = userMapper.deleteById(5L);
system.out.println(result);
}
6.1.2 根据ID批量删除
@Test
public void testDeleteBatchIds() {
    int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
system.out.println(result);
}
6.1.3 简单条件删除
@Test
public void testDeleteBatchIds() {
    int result = userMapper.deleteBatchIds(Arrays.asList(8, 9, 10));
	system.out.println(result);
}

6.2 逻辑删除

物理删除:真实删除,将对应数据从数据库中删除,之后查询不到此条被删除数据
逻辑删除:假删除,将对应数据中代表是否被删除字段状态修改为“被删除状态”,之后在数据库中仍旧能看到此条数据记录。主要使用场景有:有关联数据,不便删除。

6.2.1 MP中实现逻辑删除

首先修改user表,添加一个逻辑删除标识字段

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

修改实体类,添加相关字段
添加deleted 字段,并加上 @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

配置完成后,在MP中删除操作就会变成将变成逻辑删除,删除时只修改标识位的值。而查询时也会指查询标识位为未删除的记录。

7. 条件构造器Wrapper

在这里插入图片描述

  • Wrapper : 条件构造抽象类,最顶端父类
    • AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
      • QueryWrapper : 查询条件封装
      • UpdateWrapper : Update 条件封装
    • AbstractLambdaWrapper : 使用Lambda 语法
      • LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
      • LambdaUpdateWrapper : Lambda 更新封装Wrapper

Wrapper相关API

API效果
setSqlSelect设置 SELECT 查询字段
whereWHERE 语句,拼接 + WHERE 条件
andAND 语句,拼接 + AND 字段=值
orOR 语句,拼接 + OR 字段=值
orNewOR 语句,拼接 + OR (字段=值)
eq等于=
allEq基于 map 内容等于=
ne不等于<>
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
like模糊查询 LIKE
notLike模糊查询 NOT LIKE
inIN 查询
notInNOT IN 查询
isNullNULL 值查询
isNotNullIS NOT NULL
groupBy分组 GROUP BY
havingHAVING 关键词
orderBy排序 ORDER BY
orderAscASC 排序 ORDER BY
orderDescDESC 排序 ORDER BY
existsEXISTS 条件语句
notExistsNOT EXISTS 条件语句
betweenBETWEEN 条件语句
notBetweenNOT BETWEEN 条件语句
addFilter自由拼接 SQL
last拼接在最后,例如:last(“LIMIT 1”)

相关测试
ge、gt、le、lt、isNull、isNotNull

@Test
public void testQuery() {
QueryWrapper<User>queryWrapper = newQueryWrapper<>();
queryWrapper
        .isNull("name")
        .ge("age", 12)
        .isNotNull("email");
    int result = userMapper.delete(queryWrapper);
System.out.println("delete return count = " + result);
}

eq、ne

@Test
public void testSelectOne() {
QueryWrapper<User>queryWrapper = newQueryWrapper<>();
queryWrapper.eq("name", "Tom");
Useruser = userMapper.selectOne(queryWrapper);//只能返回一条记录,多余一条则抛出异常
System.out.println(user);
}

between、notBetween

@Test
public void testSelectCount() {
QueryWrapper<User>queryWrapper = newQueryWrapper<>();
queryWrapper.between("age", 20, 30);
    Integer count = userMapper.selectCount(queryWrapper); //返回数据数量
System.out.println(count);
}

like、notLike、likeLeft、likeRight

@Test
public void testSelectMaps() {
QueryWrapper<User>queryWrapper = newQueryWrapper<>();
queryWrapper
        .select("name", "age")
        .like("name", "e")
        .likeRight("email", "5");
List<Map<String, Object>>maps = userMapper.selectMaps(queryWrapper);//返回值是Map列表
maps.forEach(System.out::println);
}

orderBy、orderByDesc、orderByAsc

@Test
public void testSelectListOrderBy() {
QueryWrapper<User>queryWrapper = newQueryWrapper<>();
queryWrapper.orderByDesc("age", "id");
List<User>users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
Mybatis Plus 是 Mybatis 的一个增强工具包,提供了许多实用的功能,例如自动分页、自动填充、逻辑删除、多租户等。下面简单介绍一下使用 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、付费专栏及课程。

余额充值