Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性:
-
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
-
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
-
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
-
支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
-
内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
-
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
-
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
1.环境搭建
1 配置druid.datasource数据源,获取数据库连接
pom.xml导入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
yml文件配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/j220501?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: YOGr1hAd5Yi65JlFGKdVv010LaxT1M3SfDdSQ9Gqso6ccHAL5fxfjzlon0B8BPLfzEGz0llw97dXfIK8YDs8qw==
druid:
initial-size: 1
min-idle: 3
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 30000
validation-query: select 'x'
test-while-idle: true
#建议配置为false。获取连接时执行validationQuery检测连接是否有效,这个配置会降低性能
test-on-borrow: false
#建议配置为false。获取连接时执行validationQuery检测连接是否有效,这个配置会降低性能
test-on-return: false
# 打开PSCache,并且指定每个连接上PSCache的大小
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
#配置监控统计拦截的filters,去掉后监控界面sql无法统计
filters: config,stat,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connection-properties: ruid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM1CwAKEYnsOsT44An+pEBJM+uFh7/kmftHZz44ZiwfX6eJX4jxSiR9XF3KJZUwLrdN7twz08y557+yg4/EPaLMCAwEAAQ==
# 合并多个DruidDataSource的监控数据
use-global-data-source-stat: true
#采集数据库监控的数据
web-stat-filter:
url-pattern: /*
enabled: true
exclusions: .js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
#展示监控信息
stat-view-servlet:
url-pattern: /druid/*
allow: 127.0.0.1
reset-enable: false
#是否开启,设置为true才能打开监控页面
enabled: true
#druid监控登录页面的用户名
login-username: root
#druid监控登录页面的密码
login-password: root
2.配置mybatis-plus
pom.xml引入依赖
<!--mybatis-plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 代码生成器需要读取模板文件,所以要引入freemaker依赖 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
yml文件配置
mybatis-plus:
mapper-locations: classpath*:com/hqyj/*/*/mapper/*Mapper.xml
type-aliases-package: com.hqyj.*.*.entity
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ASSIGN_ID:"mybatisplus分配ID,Long,Integer,String", ASSIGN_UUID:"分配UUID,String";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
#filed-strategy在高版本被废弃(3.1.2以后)
#filed-strategy: NOT_EMPTY
insert-strategy: not_empty
select-strategy: not_empty
update-strategy: not_empty
#刷新mapper 调试神器
refresh-mapper: true
#逻辑删除配置
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
#驼峰下划线转换
map-underscore-to-camel-case: true
cache-enabled: false
3.代码生成器
创建CodeGenerator类:
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (!StringUtils.isEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
// String projectPath = System.getProperty("user.dir");
// gc.setOutputDir(projectPath + "/src/main/java");
String projectPath = "D://mybatisCode";
gc.setOutputDir(projectPath);
gc.setAuthor("sdx2009");
//设置完之后是否打开资源管理器
gc.setOpen(false);
gc.setSwagger2(false);
gc.setIdType(IdType.AUTO);
//设置是否覆盖原始生成的文件
gc.setFileOverride(true);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false