MyBatisPlus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
特性:
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类询
条件,无需再担心字段写错
**支持主键自动生成:**支持多达 4 种主键策略(内含分布式唯一 ID 生成器 Sequence),可自由配置,完美解决主键问题
**支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作支持自定义全局通用操作:**支持全局通用方法注入(Write once, useanywhere )
**内置代码生成器:**采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
**内置分页插件:**基于 MyBatis 物理分页,开发者无需关心具体操作, 配置好插件之后,写分页等同于普通 List 查询
**分页插件支持多种数据库:**支持 MySQL、MariaDB、Oracle、DB2、 H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
**内置性能分析插件:**可输出 Sql 语句以及其执行时间,建议开发测试
时启用该功能,能快速揪出慢查询
**内置全局拦截插件:**提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
1、mybatisplus环境搭建
Emp.java
1 package cn.tulingxueyuan.bean;
2
3 import java.util.Date;
4
5 public class Emp {
6
7 private Integer empno;
8 private String eName;
9 private String job;
10 private Integer mgr;
11 private Date hiredate;
12 private Double sal;
13 private Double comm;
14 private Integer deptno;
15
16 public Emp() {
17 }
18
19 public Integer getEmpno() {
20 return empno;
21 }
22
23 public void setEmpno(Integer empno) {
24 this.empno = empno;
25 }
2627 public String geteName() {
28 return eName;
29 }
30
31 public void seteName(String eName) {
32 this.eName = eName;
33 }
34
35 public String getJob() {
36 return job;
37 }
38
39 public void setJob(String job) {
40 this.job = job;
41 }
42
43 public Integer getMgr() {
44 return mgr;
45 }
46
47 public void setMgr(Integer mgr) {
48 this.mgr = mgr;
49 }
50
51 public Date getHiredate() {
52 return hiredate;
53 }
54
55 public void setHiredate(Date hiredate) {
56 this.hiredate = hiredate;
57 }
58
59 public Double getSal() {
60 return sal;
61 }
62
63 public void setSal(Double sal) {
64 this.sal = sal;
65 }
66
67 public Double getComm() {
68 return comm;
69 }
70
71 public void setComm(Double comm) {
72 this.comm = comm;
73 }
74
75 public Integer getDeptno() {
76 return deptno;
77 }
78
79 public void setDeptno(Integer deptno) {
80 this.deptno = deptno;
81 }
82
83 @Override
84 public String toString() {
85 return "Emp{" +
86 "empno=" + empno +
87 ", ename='" + eName + '\'' +
88 ", job='" + job + '\'' +
89 ", mgr=" + mgr +
90 ", hiredate=" + hiredate +
91 ", sal=" + sal +
92 ", comm=" + comm +
93 ", deptno=" + deptno +
94 '}';
95 }
96 }
数据库表sql语句
1 CREATE TABLE `tbl_emp` (
2 `EMPNO` int(4) NOT NULL AUTO_INCREMENT,
3 `E_NAME` varchar(10) DEFAULT NULL,
4 `JOB` varchar(9) DEFAULT NULL,
5 `MGR` int(4) DEFAULT NULL,
6 `HIREDATE` date DEFAULT NULL,
7 `SAL` double(7,2) DEFAULT NULL,
8 `COMM` double(7,2) DEFAULT NULL,
9 `DEPTNO` int(4) DEFAULT NULL,
10 PRIMARY KEY (`EMPNO`)11 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
pom.xml
1 <?xml version="1.0" encoding="UTF‐8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apach
e.org/xsd/maven‐4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>cn.tulingxueyuan</groupId>
8 <artifactId>mybatis_plus</artifactId>
9 <version>1.0‐SNAPSHOT</version>
10 <dependencies>
11 <!‐‐ https://mvnrepository.com/artifact/com.baomidou/mybatis‐plus ‐‐>
12 <dependency>
13 <groupId>com.baomidou</groupId>
14 <artifactId>mybatis‐plus</artifactId>
15 <version>3.3.1</version>
16 </dependency>
17 <!‐‐ https://mvnrepository.com/artifact/junit/junit ‐‐>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>4.13</version>
22 <scope>test</scope>
23 </dependency>
24 <!‐‐ https://mvnrepository.com/artifact/log4j/log4j ‐‐>
25 <dependency>
26 <groupId>log4j</groupId>
27 <artifactId>log4j</artifactId>
28 <version>1.2.17</version>
29 </dependency>
30 <!‐‐ https://mvnrepository.com/artifact/com.alibaba/druid ‐‐>
31 <dependency>
32 <groupId>com.alibaba</groupId>
33 <artifactId>druid</artifactId>
34 <version>1.1.21</version>
35 </dependency>
36 <!‐‐ https://mvnrepository.com/artifact/mysql/mysql‐connector‐java ‐‐>
37 <dependency>38 <groupId>mysql</groupId>
39 <artifactId>mysql‐connector‐java</artifactId>
40 <version>8.0.19</version>
41 </dependency>
42
43 <!‐‐ https://mvnrepository.com/artifact/org.springframework/spring‐cont
ext ‐‐>
44 <dependency>
45 <groupId>org.springframework</groupId>
46 <artifactId>spring‐context</artifactId>
47 <version>5.2.3.RELEASE</version>
48 </dependency>
49 <!‐‐ https://mvnrepository.com/artifact/org.springframework/spring‐orm
‐‐>
50 <dependency>
51 <groupId>org.springframework</groupId>
52 <artifactId>spring‐orm</artifactId>
53 <version>5.2.3.RELEASE</version>
54 </dependency>
55
56 </dependencies>
57
58 </project>
mybatisconfig.xml
1 <?xml version="1.0" encoding="UTF‐8" ?>
2 <!DOCTYPE configuration
3 PUBLIC "‐//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis‐3‐config.dtd">
5 <configuration>
6 <settings>
7 <setting name="logImpl" value=