溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Spring Boot 中怎么配置MyBatis多數(shù)據(jù)源

發(fā)布時(shí)間:2021-07-30 13:45:35 來(lái)源:億速云 閱讀:172 作者:Leah 欄目:大數(shù)據(jù)

今天就跟大家聊聊有關(guān)Spring Boot 中怎么配置MyBatis多數(shù)據(jù)源,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)建數(shù)據(jù)庫(kù) db_test

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `user_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用戶(hù)姓名',
  `user_sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '用戶(hù)性別',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t_user
-- ----------------------------
BEGIN;
INSERT INTO `t_user` VALUES (1, '劉備', '男');
INSERT INTO `t_user` VALUES (2, '孫尚香', '女');
INSERT INTO `t_user` VALUES (3, '周瑜', '男');
INSERT INTO `t_user` VALUES (4, '小喬', '女');
INSERT INTO `t_user` VALUES (5, '諸葛亮', '男');
INSERT INTO `t_user` VALUES (6, '黃月英', '女');
INSERT INTO `t_user` VALUES (7, '關(guān)羽', '男');
INSERT INTO `t_user` VALUES (8, '張飛', '男');
INSERT INTO `t_user` VALUES (9, '趙云', '男');
INSERT INTO `t_user` VALUES (10, '黃總', '男');
INSERT INTO `t_user` VALUES (11, '曹操', '男');
INSERT INTO `t_user` VALUES (12, '司馬懿', '男');
INSERT INTO `t_user` VALUES (13, '貂蟬', '女');
INSERT INTO `t_user` VALUES (14, '呂布', '男');
INSERT INTO `t_user` VALUES (15, '馬超', '男');
INSERT INTO `t_user` VALUES (16, '魏延', '男');
INSERT INTO `t_user` VALUES (17, '孟獲', '男');
INSERT INTO `t_user` VALUES (18, '大喬', '女');
INSERT INTO `t_user` VALUES (19, '劉嬋', '男');
INSERT INTO `t_user` VALUES (20, '姜維', '男');
INSERT INTO `t_user` VALUES (21, '廖化', '男');
INSERT INTO `t_user` VALUES (22, '關(guān)平', '男');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

dbb_test2

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_hero
-- ----------------------------
DROP TABLE IF EXISTS `t_hero`;
CREATE TABLE `t_hero` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `hero_code` varchar(32) DEFAULT NULL COMMENT '英雄編碼',
  `hero_name` varchar(20) DEFAULT NULL COMMENT '英雄名稱(chēng)',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_hero
-- ----------------------------
BEGIN;
INSERT INTO `t_hero` VALUES (1, '001', '德瑪西亞');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

構(gòu)建項(xiàng)目,項(xiàng)目目錄結(jié)構(gòu) Spring Boot 中怎么配置MyBatis多數(shù)據(jù)源

pom 文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.zwqh</groupId>
	<artifactId>spring-boot-mybatis-mulidatasource</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-mybatis-mulidatasource</name>
	<description>spring-boot-mybatis-mulidatasource</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</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-jdbc</artifactId>
		</dependency>
		
		<!-- 熱部署模塊 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional> <!-- 這個(gè)需要為 true 熱部署才有效 -->
		</dependency>
				
		<!-- mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng). -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		
		<!-- mybaits -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
		
		<!-- alibaba的druid數(shù)據(jù)庫(kù)連接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.20</version>
		</dependency>
		
		<!-- pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.12</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

這里使用了alibaba的druid數(shù)據(jù)庫(kù)連接池,Druid 能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能。這里我們暫時(shí)只做簡(jiǎn)單的應(yīng)用。

配置文件

#master 數(shù)據(jù)源配置
master.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
master.datasource.url=jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8&useSSL=true
master.datasource.username=root
master.datasource.password=zwqh@0258
#slave 數(shù)據(jù)源配置
slave.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
slave.datasource.url=jdbc:mysql://127.0.0.1:3306/db_test2?useUnicode=true&characterEncoding=UTF-8&useSSL=true
slave.datasource.username=root
slave.datasource.password=zwqh@0258
#mybatis
mybatis.mapper-locations=classpath:/mapper/**/*Mapper.xml

數(shù)據(jù)源配置

MasterDataSourceConfig 對(duì)應(yīng)數(shù)據(jù)庫(kù) db_test

@Configuration
@MapperScan(basePackages = "cn.zwqh.springboot.dao.master", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
	@Value("${master.datasource.driver-class-name}")
	private String driverClassName;

	@Value("${master.datasource.url}")
	private String url;

	@Value("${master.datasource.username}")
	private String username;

	@Value("${master.datasource.password}")
	private String password;

	@Bean(name = "masterDataSource")
	@Primary
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(this.driverClassName);
		dataSource.setUrl(this.url);
		dataSource.setUsername(this.username);
		dataSource.setPassword(this.password);
		return dataSource;
	}

	@Bean(name = "masterSqlSessionFactory")
	@Primary
	public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/master/*Mapper.xml"));
		return bean.getObject();
	}

	@Bean(name = "masterTransactionManager")
	@Primary
	public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "masterSqlSessionTemplate")
	@Primary
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}

SlaveDataSourceConfig 對(duì)應(yīng)數(shù)據(jù)庫(kù) db_test2

@Configuration
@MapperScan(basePackages = "cn.zwqh.springboot.dao.slave", sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveDataSourceConfig {
	@Value("${slave.datasource.driver-class-name}")
	private String driverClassName;

	@Value("${slave.datasource.url}")
	private String url;

	@Value("${slave.datasource.username}")
	private String username;

	@Value("${slave.datasource.password}")
	private String password;

	@Bean(name = "slaveDataSource")
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(this.driverClassName);
		dataSource.setUrl(this.url);
		dataSource.setUsername(this.username);
		dataSource.setPassword(this.password);
		return dataSource;
	}

	@Bean(name = "slaveSqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/slave/*Mapper.xml"));
		return bean.getObject();
	}

	@Bean(name = "slaveTransactionManager")
	public DataSourceTransactionManager transactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "slaveSqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}

多個(gè)數(shù)據(jù)源在使用的過(guò)程中必須指定主庫(kù),不然會(huì)報(bào)錯(cuò)。 @MapperScan(basePackages = "cn.zwqh.springboot.dao.slave") 指定對(duì)應(yīng) Dao 層的掃描路徑。

dao 層和 xml 層

db_test 數(shù)據(jù)庫(kù)的 dao 層在 cn.zwqh.springboot.dao.master 包下,db_test2 數(shù)據(jù)庫(kù)的 dao 層在 cn.zwqh.springboot.dao.slave 包下。

UserDao

public interface UserDao {

	List<UserEntity> getAll();

}

HeroDao

public interface HeroDao {

	List<Hero> getAllHero();

}

db_test 數(shù)據(jù)庫(kù)的 xml 層在 /mapper/master/ 文件路徑下,db_test2 數(shù)據(jù)庫(kù)的 xml 層在 /mapper/slave/ 文件路徑下。

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zwqh.springboot.dao.master.UserDao">
	<resultMap type="cn.zwqh.springboot.model.UserEntity" id="user">
		<id property="id" column="id"/>
		<result property="userName" column="user_name"/>
		<result property="userSex" column="user_sex"/>
	</resultMap>
	<!-- 獲取所有用戶(hù) -->
	<select id="getAll" resultMap="user">
		select * from t_user
	</select>
</mapper>

HeroMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zwqh.springboot.dao.slave.HeroDao">
	<resultMap type="cn.zwqh.springboot.model.Hero" id="hero">
		<id property="id" column="id"/>
		<result property="heroCode" column="hero_code"/>
		<result property="heroName" column="hero_name"/>
	</resultMap>
	<!-- 獲取所有用戶(hù) -->
	<select id="getAllHero" resultMap="hero">
		select * from t_hero
	</select>
</mapper>

測(cè)試

測(cè)試可以使用 SpringBootTest,也可以放到 Controller中,個(gè)人習(xí)慣用 Controller。

@RestController
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private HeroDao heroDao;
	
	/**
	 *  查找所有用戶(hù)
	 * @return
	 */
	@RequestMapping("/getAllUser")
	public List<UserEntity> getAllUser(){
		return userDao.getAll(); 
	}
	/**
	 *  查找所有英雄
	 * @return
	 */
	@RequestMapping("/getAllHero")
	public List<Hero> getAllHero(){
		return heroDao.getAllHero();
	}
	
}

瀏覽器直接訪問(wèn):http://127.0.0.1:8080/test/ 加上相關(guān)測(cè)試路徑即可。

看完上述內(nèi)容,你們對(duì)Spring Boot 中怎么配置MyBatis多數(shù)據(jù)源有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI