本文在我的技術博客【鳥不拉屎】同步發佈,詳情可猛戳 亦可掃描文章末尾二維碼關注我的公衆號【鳥不拉屎】html
實際業務場景中,不可能只有一個庫,因此就有了分庫分表,多數據源的出現。實現了讀寫分離,主庫負責增改刪,從庫負責查詢。這篇文章將實現Spring Boot如何實現多數據源,動態數據源切換,讀寫分離等操做。java
快速新建項目spring-boot項目mysql
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
複製代碼
和以前教程同樣,首先配置application.ymlgit
#指定配置文件爲test
spring:
profiles:
active: test
#配置Mybatis
mybatis:
configuration:
# 開啓駝峯命名轉換,如:Table(create_time) -> Entity(createTime)。不須要咱們關心怎麼進行字段匹配,mybatis會自動識別`大寫字母與下劃線`
map-underscore-to-camel-case: true
#打印SQL日誌
logging:
level:
com.niaobulashi.mapper.*: DEBUG
複製代碼
其中打印SQL日誌這塊,由於是多數據源,在mapper包下面區分不一樣的數據庫來源xml文件,因此用*表示。github
配置application-test.yml以下web
spring:
datasource:
#主庫
master:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
#從庫
slave:
jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
複製代碼
從spring.datasource節點開始,區分主庫master,從庫slave。主庫鏈接的數據庫爲test,從庫鏈接的數據庫爲test2。spring
注意:這裏須要注意的是,從Spring Boot2開始,在配置多數據源時有些配置發生了變化,網上許多教程使用的是spring.datasource.url
。會出現jdbcUrl is required with driverClassName.
的問題。sql
解決方法:配置多數據源時,將spring.datasource.url
配置改成spring.datasource.jdbc-url
數據庫
依據知名博主:純潔的微笑,寫的博文咱們來分析一波apache
首先看主庫配置的代碼:
@Configuration
@MapperScan(basePackages = "com.niaobulashi.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class DataSourceMasterConfig {
/** * 是application-test.yml中的spring.datasource.master配置生效 * @return */
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
@Primary
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
/** * 將配置信息注入到SqlSessionFactoryBean中 * @param dataSource 數據庫鏈接信息 * @return * @throws Exception */
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
return bean.getObject();
}
/** * 事務管理器,在實例化時注入主庫master * @param dataSource * @return */
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/** * SqlSessionTemplate具備線程安全性 * @param sqlSessionFactory * @return * @throws Exception */
@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
複製代碼
問題:看這塊masterSqlSessionFactory
,SqlSessionFactoryBean
只獲取了spring.datasource.master
數據庫鏈接信息,並無獲取多數據庫的配置信息mybatis.configuration
致使咱們須要配置駝峯命名規則,配置信息並無注入到SqlSessionFactoryBean
。這樣就致使在查詢是,遇到下劃線沒法解析相應字段user_id,dept_id,create_time
解決方法:在配置中添加Configuration
同時,將配置信息注入到SqlSessionFactoryBean
/** * 將配置信息注入到SqlSessionFactoryBean中 * @param dataSource 數據庫鏈接信息 * @return * @throws Exception */
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 使配置信息加載到類中,再注入到SqlSessionFactoryBean
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml"));
return bean.getObject();
}
複製代碼
和添加主庫配置信息同樣,只不過不一樣的是,不須要添加@Primary
首選註解
代碼以下
@Configuration
@MapperScan(basePackages = "com.niaobulashi.mapper.slave", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
public class DataSourceSlaveConfig {
/** * 是application-test.yml中的spring.datasource.master配置生效 * @return */
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
/** * 將配置信息注入到SqlSessionFactoryBean中 * @param dataSource 數據庫鏈接信息 * @return * @throws Exception */
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 使配置信息加載到類中,再注入到SqlSessionFactoryBean
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/slave/*.xml"));
return bean.getObject();
}
/** * 事務管理器,在實例化時注入主庫master * @param dataSource * @return */
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/** * SqlSessionTemplate具備線程安全性 * @param sqlSessionFactory * @return * @throws Exception */
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
複製代碼
在網上還看到這樣一種配置,單獨經過@ConfigurationProperties註解配置Mybatis的配置信息以下
/** * 試application.yml中的mybatis.configuration配置生效,若是不主動配置,因爲@Order配置順序不一樣,講致使配置不能及時生效 * 使配置信息加載到類中,再注入到SqlSessionFactoryBean * @return */
@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration configuration() {
return new org.apache.ibatis.session.Configuration();
}
複製代碼
其中prefix
,在主庫和從庫中的id是同樣的,必須保持不一樣,不然idea就會提示報錯Duplicate prefix
致使只有主庫能夠執行Mybatis的配置,從庫無效。
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 使配置信息加載到類中,再注入到SqlSessionFactoryBean
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
return bean.getObject();
}
複製代碼
這塊驗證只有主庫有效,從庫的駝峯方法解析無效。後續再來研究下。。。
代碼結構以下
其中SysUserMasterDao代碼
public interface SysUserMasterDao {
/** * 根據userId查詢用戶信息 * @param userId 用戶ID */
List<SysUserEntity> queryUserInfo(Long userId);
/** * 查詢全部用戶信息 */
List<SysUserEntity> queryUserAll();
/** * 根據userId更新用戶的郵箱和手機號 * @return */
int updateUserInfo(SysUserEntity user);
}
複製代碼
SysCodeMasterDao.xml
<mapper namespace="com.niaobulashi.mapper.master.SysUserMasterDao">
<!--查詢全部用戶信息-->
<select id="queryUserAll" resultType="com.niaobulashi.entity.SysUserEntity">
SELECT
ur.*
FROM
sys_user ur
WHERE
1 = 1
</select>
<!--根據用戶userId查詢用戶信息-->
<select id="queryUserInfo" resultType="com.niaobulashi.entity.SysUserEntity">
SELECT
ur.*
FROM
sys_user ur
WHERE
1 = 1
AND ur.user_id = #{userId}
</select>
<!-- 根據UserId,更新郵箱和手機號 -->
<update id="updateUserInfo" parameterType="com.niaobulashi.entity.SysUserEntity">
UPDATE sys_user u
<set>
<if test="email != null">
u.email = #{email},
</if>
<if test="mobile != null">
u.mobile = #{mobile},
</if>
</set>
WHERE
u.user_id = #{userId}
</update>
</mapper>
複製代碼
八、Controller層測試
@RestController
public class SysUserController {
@Autowired
private SysUserMasterDao sysUserMasterDao;
@Autowired
private SysUserSlaveDao sysUserSlaveDao;
/** * 查詢全部用戶信息Master * @return */
@RequestMapping("/getUserMasterAll")
private List<SysUserEntity> getUserMaster() {
System.out.println("查詢主庫");
List<SysUserEntity> userList = sysUserMasterDao.queryUserAll();
return userList;
}
/** * 查詢全部用戶信息Slave * @return */
@RequestMapping("/getUserSlaveAll")
private List<SysUserEntity> getUserSlave() {
System.out.println("查詢從庫");
List<SysUserEntity> userList = sysUserSlaveDao.queryUserAll();
return userList;
}
/** * 根據userId查詢用戶信息Master * @return */
@RequestMapping("/getUserMasterById")
private List<SysUserEntity> getUserMasterById(@RequestParam(value = "userId", required = false) Long userId) {
List<SysUserEntity> userList = sysUserMasterDao.queryUserInfo(userId);
return userList;
}
/** * 根據userId查詢用戶信息Slave * @return */
@RequestMapping("/getUserSlaveById")
private List<SysUserEntity> getUserSlaveById(@RequestParam(value = "userId", required = false) Long userId) {
List<SysUserEntity> userList = sysUserSlaveDao.queryUserInfo(userId);
return userList;
}
}
複製代碼
主庫:http://localhost:8080/getUserMasterAll
從庫:http://localhost:8080/getUserSlaveAll
一、經過多數據源方式實現數據庫層面的讀寫分離
二、多數據源連接數據庫是,使用spring.datasource.jdbc-url
三、多數據源的mybatis.configuration配置注意須要手動注入SqlSessionFactory
鳥不拉屎:一個正在努力Coding的將來架構師