Mybatis-Plus相关使用

目录

1.快速开始

1.1 导入依赖

1.2 创建Mapper

1.3 增加 @MapperScan扫描注解

1.4 测试CRUD

2.常见注解

@TableName

@TableId

 @TableField

4.核心功能

4.1 条件构造器

4.1.1 QueryWrapper

4.1.2 UpdateWrapper

LambdaQueryWrapper和LambdaUpdateWrapper

条件查询:

in:设置单个字段的 IN 条件,即字段的值在给定的集合中

NULL值处理

if语句控制条件追加

查询投影

逻辑删除

4.2 自定义SQL

4.2.1 基本使用

4.2.2 多表联查

普通实现

Wrapper构建查询条件实现


完整教程资料icon-default.png?t=N7T8https://juejin.cn/post/7010012164731699207

学习资料icon-default.png?t=N7T8https://htmlpreview.github.io/?https://github.com/caolib/note/blob/master/mybatis-plus.htmlMyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1.快速开始

1.1 导入依赖

导入mybatis-plus依赖,包含了mybatis,不用额外再导入mybatis依赖

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

 application.yml配置mysql数据源

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost/mp_plus?serverTimezone=GMT%2B8&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: 123456

1.2 创建Mapper

为了简化单表CRUD,mp已经提供了对于单表的CRUD操作的接口BaseMapper,直接继承BaseMapper接口即可直接使用

// 在对应的Mapper上面继承基本的类 BaseMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
	// 所有的CRUD操作都已经编写完成了
}

1.3 增加 @MapperScan扫描注解

@MapperScan("com.mpstudy.mp.mapper")

1.4 测试CRUD

测试BaseMapper中对单表CRUD操作 (基本是基于主键实现的CRUD操作)

///selectBatchIds接受的是一个list
categoryDao.selectBatchIds(req.getCategory2())
                        .stream()
                        .map(AurrCategory::getName)
                        .collect(Collectors.toList())

@Test
public void testInsert() {
    User user = new User();
    //user.setId(5L);
    user.setUsername("ikun23");
    user.setPassword("123");
    user.setPhone("18688990011");
    user.setBalance(200);
    user.setInfo(UserInfo.of(24,"英语老师","female"));
    user.setCreateTime(LocalDateTime.now());
    user.setUpdateTime(LocalDateTime.now());
    userMapper.insert(user);
}

@Test
public void testSelectById() {
    User user = userMapper.selectById(4L);
    System.out.println(user);
}

@Test
public void testSelectByIds() {
    selectBatchIds接受的是一个list
    List<User> users = userMapper.selectBatchIds(List.of(1, 2, 3));
    users.forEach(System.out::println);
}


@Test
public void testUpdate() {
    User user = new User();
    user.setId(5L);
    user.setBalance(3);
    user.setInfo(UserInfo.of(24,"英语老师","female"));
    user.setCreateTime(LocalDateTime.now());
    user.setUpdateTime(LocalDateTime.now());

    userMapper.updateById(user);
}

@Test
public void testDelete() {
    System.out.println(userMapper.deleteById(5L));
}

总结:只要继承了BaseMapper,就能直接对单表进行CRUD操作

2.常见注解

问题:在刚刚的测试中,我们直接调用BaseMapper中的方法就能对表增删改查,在继承BaseMapper的时候我们只是指定了一个泛型<User>,并没有指定是哪张表,那么mybatis-plus怎么知道我们要操作的是user表呢?它又是怎么知道这张表中的所有字段名呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值