【mybatisplus使用示例】

适合阅读对象:没接触过mybatisplus 和 mplus 初学者

集成mybatisplus

1. 引入依赖

首先,我们需要在Maven或Gradle项目中引入MyBatis-Plus的依赖。可以在pom.xml(Maven)或build.gradle(Gradle)文件中添加如下依赖:

<!-- Maven -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>最新版本</version>
</dependency>

2. 配置数据源和MyBatis-Plus

在Spring Boot项目的配置文件(application.ymlapplication.properties)中配置数据源和MyBatis-Plus的相关配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydatabase
    username: root
    password: password
    driver-class-name: com.mysql.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:mapper/**/*.xml
  type-aliases-package: com.example.model

在上述示例中,我们配置了MySQL数据库的连接信息,并指定了MyBatis-Plus的Mapper文件位置和实体类的包路径。

3. 创建实体类和Mapper接口

接下来,我们需要创建实体类和Mapper接口。实体类代表数据库表中的一条记录,而Mapper接口用于定义对数据库的操作。示例代码如下:

// 实体类
@Data
@TableName("user")
public class User {
   
    @TableId
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

// Mapper接口
public interface UserMapper extends BaseMapper<User> {
   
}

在上述示例中,我们使用了Lombok库的@Data注解简化了实体类的定义。使用@TableName注解指定了实体类对应的数据库表名。@TableId注解表示该字段为主键。

UserMapper接口继承了BaseMapper接口,这个接口提供了一组通用的CRUD操作方法,无需手动编写SQL语句。

基本功能

1. 使用通用CRUD操作

MyBatis-Plus提供了一组通用的CRUD操作方法,可以直接在Mapper接口中使用,无需手动编写SQL语句。示例代码如下:

@Autowired
private UserMapper userMapper;

// 添加用户
User user = new User();
user.setName("John");
user.setAge(25);
user.setEmail("john@example.com");
userMapper.insert(user);

// 根据ID查询用户
User queriedUser = userMapper.selectById(1L);

// 更新用户
queriedUser.setName("John Smith");
userMapper.updateById(queriedUser);

// 删除用户
userMapper.deleteById(1L);

在上述示例中,我们通过自动注入UserMapper对象来进行CRUD操作。可以使用insert方法插入新的用户记录,selectById方法根据ID查询用户,updateById方法更新用户信息,deleteById方法根据ID删除用户记录。

2. QueryWrapper的简单使用

QueryWrapper是用于构建查询条件的Wrapper类。它提供了一系列方法来设置查询条件,例如eq、ne、like、in等。以下是常用的QueryWrapper方法:

  • eq:等于某个字段值
  • ne:不等于某个字段值
  • like:模糊查询,包含某个字段值
  • notLike:模糊查询,不包含某个字段值
  • in:某个字段值在给定的集合中
  • notIn:某个字段值不在给定的集合中
  • isNull:某个字段为空
  • isNotNull:某个字段不为空
  • orderBy:设置排序规则

下面是一个使用QueryWrapper进行条件查询的示例:

QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25)
           .like("name", "John")
           .in("status", Arrays.asList("ACTIVE", "INACTIVE"))
           .orderByAsc("create_time");

List<User> userList = userMapper.selectList(queryWrapper);

基本的条件查询和分页查询功能。示例代码如下:

// 条件查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25)
            .like("name", "John");
List<User> userList = userMapper.selectList(queryWrapper);

// 分页查询
Page<User> page = new Page<>(1, 10); // 第1页,每页10条记录
IPage<User> userPage = userMapper.selectPage(page, null);
List<User> records = userPage.getRecords();

and链接子条件

QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25)
           .and(wrapper -> wrapper.like("name", "John").or().like("name", "Doe"))
           .in("status", Arrays.asList("ACTIVE", "INACTIVE"))
           .orderByAsc("create_time");

List<User> userList = userMapper.selectList(queryWrapper);

分组查询

QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.between("age", 18, 30)
           .in("status", Arrays.asList("ACTIVE", "INACTIVE"))
           .nested(i -> i.like("name", "John").or().like("name", "Doe"))
           .groupBy("gender")
           .having("count(*) > 2");

List<User> userList = userMapper.selectList(queryWrapper);

3.UpdateWrapper进行条件更新的示例:

UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("age", 25)
             .like("name", "John")
             .set("status", "ACTIVE");
userMapper.update(null, updateWrapper);

4 LambdaQueryWrapper(常用)

LambdaQueryWrapper 写法和QueryWrapper的区别就是,字段用lambda的写法,不用自己写字符串,实际开发中基本也用这种
简单查询

LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQueryWrapper(User.class);
lambdaQueryWrapper.eq(User::getAge, 25)
                 .like(User::getName, "John")
                 .orderByAsc(User::getCreateTime);

List<User> userList = userMapper.selectList(lambdaQueryWrapper);

复杂查询

LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQueryWrapper(User.class);
lambdaQueryWrapper.between(User::getAge, 18, 30)
                 .in(User::getStatus, Arrays.asList("ACTIVE", "INACTIVE"))
                 .nested(i -> i.like(User::getName, "John").or().like(User::getName, "Doe"))
                 .groupBy(User::getGender)
                 .having("count(*) > 2");

List<User> userList = userMapper.selectList(lambdaQueryWrapper);

本篇文章就简单介绍一下,望各位大佬点个赞吧!

相关推荐

  1. mybatisplus使用示例

    2024-01-13 06:16:05       58 阅读
  2. MyBatisPlusMyBatisPlus介绍与使用

    2024-01-13 06:16:05       31 阅读
  3. MyBatisPlus---使用limit查询

    2024-01-13 06:16:05       58 阅读
  4. MybatisPlus

    2024-01-13 06:16:05       72 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-13 06:16:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 06:16:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 06:16:05       87 阅读
  4. Python语言-面向对象

    2024-01-13 06:16:05       96 阅读

热门阅读

  1. Eclipse、IntelliJ IDEA、PyCharm三种IDE的区别

    2024-01-13 06:16:05       62 阅读
  2. TIDB: 元数据查询语句

    2024-01-13 06:16:05       48 阅读
  3. Whisper——部署fast-whisper中文语音识别模型

    2024-01-13 06:16:05       61 阅读
  4. 19. 蒙特卡洛强化学习之策略控制

    2024-01-13 06:16:05       58 阅读
  5. 商务外语MR混合现实仿真情景实训教学

    2024-01-13 06:16:05       66 阅读