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环境搭建
1.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 }
2.数据库表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;
3.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>
4.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="LOG4J"/>
8 </settings>
9 </configuration>
5.log4j.properties
1 # 全局日志配置
2 log4j.rootLogger=INFO, stdout
3 # MyBatis 日志配置
4 log4j.logger.cn.tulingxueyuan=truce
5 # 控制台输出
6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] ‐ %m%n
6.db.properties
1 driverClassname=com.mysql.jdbc.Driver
2 username=root
3 password=123456
4 url=jdbc:mysql://localhost:3306/demo
7.spring.xml
1 <?xml version="1.0" encoding="UTF‐8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx
="http://www.springframework.org/schema/tx"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://w
ww.springframework.org/schema/beans/spring‐beans.xsd http://www.springframe
work.org/schema/context https://www.springframework.org/schema/context/spri
ng‐context.xsd http://www.springframework.org/schema/tx http://www.springfr
amework.org/schema/tx/spring‐tx.xsd">
6 <context:property‐placeholder location="classpath:db.properties"></conte
xt:property‐placeholder>
7 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
8 <property name="driverClassName" value="${driverClassname}"></property>
9 <property name="url" value="${url}"></property>
10 <property name="username" value="${username}"></property>
11 <property name="password" value="${password}"></property>
12 </bean>
13 <bean id="transactionManager" class="org.springframework.jdbc.datasourc
e.DataSourceTransactionManager">
14 <property name="dataSource" ref="dataSource"></property>
15 </bean>
16
17 <tx:annotation‐driven transaction‐manager="transactionManager"></tx:ann
otation‐driven>
18 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFa
ctoryBean">
19 <property name="dataSource" ref="dataSource"></property>
20 <property name="configLocation" value="classpath:mybatis‐config.xml">
</property>
21 </bean>
22 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
23 <property name="basePackage" value="cn.tulingxueyuan.dao"></property>
24 </bean>25 </beans>
8.MyTest.java
1 package cn.tulingxueyuan;
2
3
4 import com.alibaba.druid.pool.DruidDataSource;
5 import org.junit.Test;
6 import org.springframework.context.ApplicationContext;
7 import
org.springframework.context.support.ClassPathXmlApplicationContext;
8
9 import java.sql.SQLException;
10
11 public class MyTest {
12
13 ApplicationContext context = new ClassPathXmlApplicationContext("sprin
g.xml");
14
15 @Test
16 public void test01() throws SQLException {
17 DruidDataSource dataSource = context.getBean("dataSource", DruidDataSou
rce.class);
18 System.out.println(dataSource.getConnection());
19 }
20 }
9.在集成mybatisplus的时候非常简单,只需要替换mybatis自己的 sqlSessionFactoryBean对象即可
1 <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extensio
n.spring.MybatisSqlSessionFactoryBean">
2 <property name="dataSource" ref="dataSource"></property>
3 <property name="configLocation" value="classpath:mybatis‐config.xml"></p
roperty>
4 <property name="typeAliasesPackage" value="cn.tulingxueyuan.bean"></prop
erty>
5 </bean>
好了,今天就分享到这里了,文章后续及更多java学习资料,关注我,免费领取!!!