MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,爲簡化開發、提高效率而生。
1.創建sb項目
2.添加MyBatis-Plus依賴
<!-- 以上省略sb的相關依賴,下面這個是web項目依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis-plus依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<!-- 連接池依賴+數據庫驅動包依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
3.添加配置類
兩個數據源,我們這裏配置兩個類
配置類1
package com.yanva.dm.common.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan (basePackages = DruidConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory1")
public class DruidConfig {
static final String PACKAGE = "com.yanva.dm.*.mapper";
static final String MAPPER_LOCATION = "classpath:mapper/*/*.xml";
@Bean("db1")
@ConfigurationProperties(prefix = "spring.datasource.druid.master")
public DataSource druidDataSource1() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@Bean("sqlSessionFactory1")
public SqlSessionFactory sqlSessionFactory1(@Qualifier("db1")DataSource db1) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(db1);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DruidConfig.MAPPER_LOCATION));
return sqlSessionFactoryBean.getObject();
}
}
配置類2
package com.yanva.dm.common.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan (basePackages = DruidConfig1.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory2")
public class DruidConfig1 {
static final String PACKAGE = "com.yanva.dm.slavedb.*.mapper";
static final String MAPPER_LOCATION = "classpath:mapper/slavedb/*.xml";
@Bean("db2")
@ConfigurationProperties(prefix = "spring.datasource.druid.slave")
public DataSource druidDataSource1() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@Bean("sqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory1(@Qualifier("db2")DataSource db2) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(db2);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(DruidConfig1.MAPPER_LOCATION));
return sqlSessionFactoryBean.getObject();
}
}
* 注意 PACKAGE 和MAPPER_LOCATION 還有 @ConfigurationProperties(prefix = "spring.datasource.druid.slave")
都需要根據具體情況配置。
4.修改application.yml文件(對數據源的配置和mybatis-plus的配置)
server:
port: 9898
spring:
datasource:
name: druidDataSource
type: com.alibaba.druid.pool.DruidDataSource
druid:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/home?allowMultiQueries=true&useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
username: root
password: root
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/yinzi_database?allowMultiQueries=true&useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
username: root
password: root
initial-size: 10
max-active: 100
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
freemarker:
template-loader-path: classpath:templates/
suffix: .ftl
mybatis-plus:
mapper-locations: classpath:dao/*Mapper.xml
type-aliases-package: com.yanva.dm.entity