MyBatisPlus的基本使用

概述

MyBatisPlus是为了简化我们代码书写而诞生了,使用它之后我们可以减少大量SQL语句的书写,提升开发效率(就是让我们变得更懒),它内置了许多常用的SQL操作,我们在配置好之后就可以使用了。

使用

1. 建表

DROP TABLE IF EXISTS 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');
-- 真实开发中,version(乐观锁)、deleted(逻辑删除)、gmt_create、gmt_modified

2.创建项目并引入依赖

  • 目录结构
    在这里插入图片描述
  • pom.xml
        <!--   mp 依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

在引入了MyBatisPlus之后,就不需要引入MyBatis了,但是还要引入mysql-connector-java

3.在启动类中添加包扫描

@SpringBootApplication
@MapperScan("com.example.mp.mapper") // 指定需要扫描的包
public class MpApplication {

    public static void main(String[] args) {
        SpringApplication.run(MpApplication.class, args);
    }

}

4.配置数据源

spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

5.创建与表对应的实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user") // 指定表名
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String email;
}

6.创建Mapper

// 在对应的Mapper上面继承基本的类 BaseMapper
@Repository
public interface UserMapper extends BaseMapper<User> {
	// 所有的CRUD操作都已经编写完成了
	// 你不需要像以前的配置一大堆文件了!
}

7.测试

@SpringBootTest
class MybatisPlusApplicationTests {
	// 继承了BaseMapper,所有的方法都来自己父类
	// 我们也可以编写自己的扩展方法!
	@Autowired
	private UserMapper userMapper;
	@Test
	void contextLoads() {
	// 参数是一个 Wrapper ,条件构造器,这里我们先不用 null
	// 查询全部用户
	List<User> users = userMapper.selectList(null);
	users.forEach(System.out::println);
	}
}

8.安装插件

这里以安装分页为例子

  • 在配置类中配置
@Configuration
@EnableTransactionManagement

public class MybatisPlusConfig {
    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}
  • 测试

分页查询除了使用selectPage()之外,还有其他操作,可以百度自行了解

@SpringBootTest
class MpApplicationTests {

    @Resource
    private UserMapper userMapper;
    @Test
    void contextLoads() {

        IPage<User> userPage = new Page<>(2, 2);//参数一是当前页,参数二是每页个数
         userPage = userMapper.selectPage(userPage, null);
         List<User> list = userPage.getRecords();
          for(User user : list){
              System.out.println(user);
         }
    }

}

本文只是介绍了MyBatisPlus最基本的使用,开发中用的知识还有好多:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值