Java培訓_Spring整合MyBatisPlus
一:簡介
MyBatisPlus 簡稱MP 是MyBatis的增強工具,在MyBatis的基礎上只做增強不做改變,為簡化開發,提高效率而生。MP有著眾多特性,主要變現在:無侵入 依賴少 損耗小 預防sql注入 通用mapper操作 支持多種主鍵策略 支持熱加載 支持ActiveRecord 支持代碼生成 內置分頁插等件;
二:MP快速入門
2.1:新建數據庫
-- 創建數據庫
CREATE DATABASE mybatisplus;
-- 使用數據庫
USE mybatisplus;
-- 創建表
CREATE TABLE t_user(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(50),
age int );
INSERT INTO t_user(name,email ,age) VALUES('李白','libai@togogo.net',17);
INSERT INTO t_user(name,email ,age) VALUES('亞瑟','yase@togogo.net',18);
INSERT INTO t_user(name,email ,age) VALUES('妲己','daji@togogo.net',19);
2.2:構建Maven聚合工程
2.2.1:構建Maven父工程
<!—集中定義版本號-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<spring.version>5.1.2.RELEASE</spring.version>
<mysql.version>8.0.13</mysql.version>
<druid.version>1.1.10</druid.version>
<lombok.version>1.16.12</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mybatis-plus.version>2.3</mybatis-plus.version>
</properties>
<dependencyManagement>
<dependencies>
<!--單元測試-->
<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>
<!--spring ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid數據源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2.2:構建Maven子工程并導入必須的依賴
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencys>
注意:不需要加入mybatis與spring的依賴,MyBatisPlus會自行維護
2.2.3:編寫配置文件
1.編寫db.properties配置文件
jdbc.user=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.108.129.95:3306/mybatisplus?characterEncoding=utf8
2.編寫log4j.properties配置文件
#Global logging configuration
log4j.rootLogger=DEBUG, stdout
#Console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3.編寫Spring-context.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="db.properties"/>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.user}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 事務管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 基于注解的事務管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/>
<!--配置SqlSessionFactory-->
<!--注意:整合mybatisplus框架時只需要調整SqlSessionFactory為MyBatis-plus的SqlSessionFactory即可-->
<bean name="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--mybatis原生配置 配置日志輸出-->
<property name="configuration">
<bean class="com.baomidou.mybatisplus.MybatisConfiguration">
<property name="logImpl" value="org.apache.ibatis.logging.log4j.Log4jImpl"/>
</bean>
</property>
<!--注入MybatisPlus的全局策略配置-->
<property name="globalConfig" ref="globalConfiguration"></property>
</bean>
<!-- 定義MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在2.3版本以后,dbColumnUnderline 默認值就是true -->
<!--<property name="dbColumnUnderline" value="true"></property>-->
<!-- Mysql 全局的主鍵策略 -->
<!--<property name="idType" value="0"></property>-->
<!--全局表前綴策略配置-->
<!--<property name="tablePrefix" value="t_"></property>-->
</bean>
<!--配置mapper接口掃描器-->
<bean name="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.togogo.mapper"/>
</bean>
</beans>
2.3:編寫測試代碼
2.3.1:編寫與數據庫對應的實體類
@Data
@TableName("t_user")
public class User {
private Integer id;
private String name;
private String email;
private Integer age;
}
2.3.2:編寫mapper接口
public interface UserMapper extends BaseMapper<User> {
}
注意:BaseMapper<T>:范型當前mapper接口所操作的實體類類型
2.3.3:編寫測試類
public class HelloWordTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
//數據源測試
@Test
public void DataSourceTest() throws Exception{
DataSource ds = context.getBean("dataSource", DataSource.class);
System.out.println("獲取到的數據源是:" + ds);
Connection connection = ds.getConnection();
System.out.println("獲取到的連接信息是:" + connection);
}
//insert操作
@Test
public void mpInsert(){
UserMapper userMapper = context.getBean(UserMapper.class);
User user = new User();
//user.setId(15);//mysql已設置為id自增長 mp主鍵策略選擇
user.setLastname("孫策");//@TableField 標記了帶有下劃線的字段
user.setMoney(200.0);//數據庫中不存在此字段
user.setEmail("sunce@qq.com");
user.setAge(20);
int result = userMapper.insert(user);
System.out.println("result = " + result);
//獲取當前插入數據在數據庫中的主鍵值
Integer key = user.getId();
System.out.println("key = " + key);
}
}
三:MP入門開發流程與總結
MP操作數據庫只需要創建*Mapper接口,繼承BaseMapper接口即可,不需要創建對應的sql映射文件,BaseMapper默認提供了一系列的增刪查改方法,開發者對于這些簡單的操作只需要直接調用對應的方法即可;
總結:MP在MyBatis的基礎上大大簡化了xml配置文件,使得MP更加簡潔,并且提供了豐富的crud操作。