因爲MyBatis-Plus是在MyBatis的基礎上只作加強不作改變,所以其與Spring的整合很是簡單。只需把MyBatis的依賴換成MyBatis的依賴,再把sqlSessionFactory換成MyBatis-Plus的便可。下面讓咱們在Spring中快速集成Mybatis-Plus的具體操做:java
-- 建立庫
CREATE DATABASE mp;
-- 使用庫
USE mp;
-- 建立表
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1),
age int
);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@jianshu.com',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@jianshu.com',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@jianshu.com',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@jianshu.com',0,35);
複製代碼
在本身的數據庫中執行上面的sql。mysql
2.1.一、咱們在Idea裏新建一個Maven父項目,用來管理以後咱們全部模塊的代碼。在Idea中選擇File
→ New
→ Project
git
2.1.二、在彈出的頁面中選擇 maven
而後 Next
github
2.1.三、填寫本身喜歡的 groupId
和 artifactId
,我這裏填的是com.demo.mybatis-plus
和mybatis-plus-in-action
,填寫完以後而後Next
spring
2.1.四、繼續點擊 Next
,若是所填路徑不存在,Idea會提示是否穿件一個文件,點擊OK
便可sql
2.1.五、項目建立完畢後刪除src
文件夾,就獲得了咱們想要的空的父工程,以下圖所示數據庫
核心依賴:apache
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
複製代碼
完整依賴:mybatis
<!-- 統一管理項目依賴版本 -->
<properties>
<mybatis.plus.version>3.2.0</mybatis.plus.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<druid.version>1.1.20</druid.version>
<mysql.version>5.1.48</mysql.version>
<spring.version>5.2.0.RELEASE</spring.version>
<lombok.version>1.18.10</lombok.version>
</properties>
<dependencies>
<!-- mp 依賴
mybatis-plus 會自動維護 mybatis 以及 mybatis-spring 相關的依賴
Mybatis 及 Mybatis-Spring 依賴請勿加入項目配置,以避免引發版本衝突!!!Mybatis-Plus 會自動幫你維護!
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- 設置JDK編譯版本 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
複製代碼
2.3.一、咱們在Idea裏新建一個mp01模塊,在Idea中選中當前項目 → New
→ Module
;填寫artifactId,點擊 Next
;肯定 Module name
的名稱,點擊 Finish
完成app
2.3.二、新建完成後的項目以下
2.3.三、添加實體類
在 mp01\src\main\java\com\mp\beans\Employee.java
路徑下添加實體類 Employee.java
:
@Data
@ToString
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Integer age;
}
複製代碼
2.3.四、修改mp01的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mybatis-plus-in-action</artifactId>
<groupId>com.demo.mybatis-plus</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mp01</artifactId>
<dependencies>
<!-- mp 依賴
mybatis-plus 會自動維護mybatis 以及 mybatis-spring相關的依賴
Mybatis 及 Mybatis-Spring 依賴請勿加入項目配置,以避免引發版本衝突!!!Mybatis-Plus 會自動幫你維護!
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<!--junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
複製代碼
注意:這裏在mp01的pom.xml文件中不添加上述依賴以後項目也是能正常運行的,可是咱們加上相關依賴,是爲讓項目的依賴更加清晰一些
2.3.五、在 mp01\src\main\resources
中添加配置文件:
Spring 配置文件——applicationContext.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 數據源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事務管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基於註解的事務管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/>
<!-- 配置 SqlSessionFactoryBean
mybatis提供的:org.mybatis.spring.SqlSessionFactoryBean
mybatis-plus提供的:com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
-->
<bean id="sqlSessionFactoryBean"
class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<!-- 數據源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
<!-- 別名處理 -->
<property name="typeAliasesPackage"
value="com.mp.beans"></property>
</bean>
<!--
配置 mybatis 掃描 mapper 接口的路徑
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"
value="com.mp.mapper"></property>
</bean>
</beans>
複製代碼
JDBC配置文件—— db.properties
:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
複製代碼
日誌文件——log4j.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"/>
</logger>
<logger name="org.apache.ibatis">
<level value="info"/>
</logger>
<root>
<level value="debug"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
複製代碼
mybatis-config.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
複製代碼
注意:mybatis-plus與spring整合,全部mybatis-plus的大部分都寫在spring的配置文件中,這裏定義一個空的mybatis-config.xml便可
2.3.六、測試
在 mp01\src\test\java\com\mp\test\TestMp.java
路徑下添加測試類 TestMp.java
:
public class TestMp {
private ApplicationContext iocContext = new
ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testEnvironment() throws Exception {
DataSource ds = iocContext.getBean("dataSource", DataSource.class);
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
複製代碼
運行 testEnvironment
方法後若是能看到 Connection
就證實咱們的配置能夠了。
完成後的項目結構以下:
綜上,咱們的 Spring
集成 mybatis-plus
的配置和環境搭建就完成了,下面咱們就能夠進入到下一節基本CRUD了。
相關示例完整代碼:mybatis-plus-in-action