MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为 简化开发、提高效率而生。
创建步骤:
1.创建Spring Boot工程
2.添加依赖
引入 Spring Boot Starter 父工程:
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.1.7.RELEASEversion>
- <relativePath/>
- parent>
引入相关其他依赖
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.1.3version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
mybatis-plus相关依赖
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.4.0version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-jdbcartifactId>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
配置application.yml:
添加mysql相关配置
- mybatis-plus:
- type-aliases-package: com.hz.entity #类型别名所在的包
- #控制台打印sql语句
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- map-underscore-to-camel-case: false #驼峰映射
- global-config:
- db-config:
- logic-delete-field: delFlag #全局逻辑删除字段值 3.3.0开始支持,详情看下面。
- logic-delete-value: 1 #逻辑已删除值(默认为 1)
- logic-not-delete-value: 0 #逻辑未删除值(默认为 0)
- #数据库链接
- spring:
- datasource:
- url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
- username: root
- driver-class-name: com.mysql.cj.jdbc.Driver
- password: 123456
- #静态资源
- resources:
- static-locations: classpath:/templates,classpath:/static/
在 Spring Boot 启动类中添加 @MapperScan 注解

编码:
在mapper类中继承Basemapper

测试:

注意:
1.数据库字段若为驼峰命名,则需要开启
mybatis-plus:configuration:map-underscoreto-camel-case: false #驼峰映射
2. UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper ,所以 不填写就是无任何条件
3.若需要自定义DAO接口,则需要在yml中读取mapper文件,
mybatis-plus:mapperlocations: classpath:mappers/*.xml
CRUD是指在做计算处理时的增加(Create)、检索(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。CRUD主要被用在描述软件系统中数据库或者持久层的基本操作功能。
service层
如 UserService 用户业务接口有自定义的方法,可创建 UserService 并继承 IService ,接口实现类 UserServiceImpl 可继承 ServiceImpl
继承 IService 接口

1.分页插件
配置插件
- //Spring boot方式
- @EnableTransactionManagement
- @Configuration
- public class MybatisPlusConfig {
- /**
- * 分页插件定义
- * @return
- */
- @Bean
- public PaginationInterceptor paginationInterceptor() {
- PaginationInterceptor paginationInterceptor = new
- PaginationInterceptor();
- // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
- // paginationInterceptor.setOverflow(false);
- // 设置最大单页限制数量,默认 500 条,-1 不受限制
- // paginationInterceptor.setLimit(500);
- return paginationInterceptor;
- }
- }
使用分页查询
- @Test
- public void testPageList() {
- Page
page = new Page(1, 2); - userMapper.selectPage(page, null);
- // 输出page对象分页查询信息
- System.out.println("总条数:" + page.getTotal());
- System.out.println("每页显示条数:" + page.getSize());
- System.out.println("总页数:" + page.getPages());
- System.out.println("当前页:" + page.getCurrent());
- System.out.println("是否有上一页:" + page.hasPrevious());
- System.out.println("是否有下一页:" + page.hasNext());
- System.out.println("查询结果:" + page.getRecords());
- }
1. application.yml 加入配置(如果你的默认值和mp默认的一样,该配置可无):
- #以下为mybatis-plus配置
- mybatis-plus:
- global-config:
- db-config:
- logic-delete-field: flag #全局逻辑删除字段值 3.3.0开始支持,详情看下面。
- logic-delete-value: 1 # 逻辑已删除值(默认为 1)
- logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
3. 在数据库中加入逻辑删除的字段,并且对应实体类中,该映射字段需要加入注解 @TableLogic
- @Data
- @TableName(value = "user") // 对应数据库表名
- public class User {
- ......省略
- /**
- * 逻辑删除字段
- */
- @TableField(value="delete_flag")
- @TableLogic
- private Integer deleteFlag;
- }