目前,業界流行的數據操做框架是 Mybatis,那 Druid 是什麼呢? Druid 是 Java 的數據庫鏈接池組件。Druid 可以提供強大的監控和擴展功能。好比能夠監控 SQL ,在監控業務能夠查詢慢查詢 SQL 列表等。Druid 核心主要包括三部分: 1. DruidDriver 代理 Driver,可以提供基於 Filter-Chain 模式的插件體系。 2. DruidDataSource 高效可管理的數據庫鏈接池 3. SQLParser 當業務數據量達到了必定程度,DBA 須要合理配置數據庫資源。即配置主庫的機器高配置,把核心高頻的數據放在主庫上;把次要的數據放在低配置的從庫。開源節流嘛,把數據放在不一樣的數據庫裏,就須要經過不一樣的數據源進行操做數據。這裏咱們舉個 springboot-mutil-datasource 工程案例: user 用戶表在主庫 master 上,地址表 city 在從庫 slave上。java
一、主庫mastermysql
CREATE DATABASE master; DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用戶編號', `user_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用戶名稱', `description` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '描述', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT user VALUES (1 ,'程序員','他有一個小網站 tom.com.cn');
二、從庫slave程序員
CREATE DATABASE slave; DROP TABLE IF EXISTS `city`; CREATE TABLE `city` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '城市編號', `province_id` int(10) UNSIGNED NOT NULL COMMENT '省份編號', `city_name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '城市名稱', `description` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '描述', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT city VALUES (1 ,1,'北京市','tom的家在北京。');
一、項目總體結構web
com.carry.springboot.config.ds 包下包含了多數據源的配置,一樣能夠有第三個數據源,按照前幾個複製便可 resources/mapper 下面有兩個模塊,分別是 Mybatis 不一樣數據源須要掃描的 mapper xml 目錄spring
二、pom.xml依賴sql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.carry.elk</groupId> 7 <artifactId>springboot-mutil-datasource</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>springboot-mutil-datasource</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.4.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <druid>1.0.31</druid> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.mybatis.spring.boot</groupId> 35 <artifactId>mybatis-spring-boot-starter</artifactId> 36 <version>1.3.2</version> 37 </dependency> 38 <!-- Druid 數據鏈接池依賴 --> 39 <dependency> 40 <groupId>com.alibaba</groupId> 41 <artifactId>druid</artifactId> 42 <version>${druid}</version> 43 </dependency> 44 45 <dependency> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-devtools</artifactId> 48 <scope>runtime</scope> 49 </dependency> 50 <dependency> 51 <groupId>mysql</groupId> 52 <artifactId>mysql-connector-java</artifactId> 53 <scope>runtime</scope> 54 </dependency> 55 <dependency> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-starter-test</artifactId> 58 <scope>test</scope> 59 </dependency> 60 </dependencies> 61 62 <build> 63 <plugins> 64 <plugin> 65 <groupId>org.springframework.boot</groupId> 66 <artifactId>spring-boot-maven-plugin</artifactId> 67 </plugin> 68 </plugins> 69 </build> 70 71 72 </project>
三、application.properties 配置兩個數據源配置數據庫
## master 數據源配置 master.datasource.url=jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=utf8 master.datasource.username=root master.datasource.password=123456 master.datasource.driverClassName=com.mysql.jdbc.Driver ## slave 數據源配置 slave.datasource.url=jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=utf8 slave.datasource.username=root slave.datasource.password=123456 slave.datasource.driverClassName=com.mysql.jdbc.Driver #熱部署 spring.devtools.restart.enabled=true
四、主數據源MasterDataSourceConfig 配置apache
1 package com.carry.springboot.config.ds; 2 3 import javax.sql.DataSource; 4 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.mybatis.spring.SqlSessionFactoryBean; 7 import org.mybatis.spring.annotation.MapperScan; 8 import org.springframework.beans.factory.annotation.Qualifier; 9 import org.springframework.beans.factory.annotation.Value; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.context.annotation.Primary; 13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 16 import com.alibaba.druid.pool.DruidDataSource; 17 18 @Configuration 19 // 掃描 Mapper 接口並容器管理 20 @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") 21 public class MasterDataSourceConfig { 22 23 // 精確到 master 目錄,以便跟其餘數據源隔離 24 static final String PACKAGE = "com.carry.springboot.dao.master"; 25 static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml"; 26 27 @Value("${master.datasource.url}") 28 private String url; 29 30 @Value("${master.datasource.username}") 31 private String user; 32 33 @Value("${master.datasource.password}") 34 private String password; 35 36 @Value("${master.datasource.driverClassName}") 37 private String driverClass; 38 39 @Bean(name = "masterDataSource") 40 @Primary 41 public DataSource masterDataSource() { 42 DruidDataSource dataSource = new DruidDataSource(); 43 dataSource.setDriverClassName(driverClass); 44 dataSource.setUrl(url); 45 dataSource.setUsername(user); 46 dataSource.setPassword(password); 47 return dataSource; 48 } 49 50 @Bean(name = "masterTransactionManager") 51 @Primary 52 public DataSourceTransactionManager masterTransactionManager() { 53 return new DataSourceTransactionManager(masterDataSource()); 54 } 55 56 @Bean(name = "masterSqlSessionFactory") 57 @Primary 58 public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) 59 throws Exception { 60 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 61 sessionFactory.setDataSource(masterDataSource); 62 sessionFactory.setMapperLocations( 63 new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION)); 64 return sessionFactory.getObject(); 65 } 66 }
五、從數據源SlaveDataSourceConfig配置api
1 package com.carry.springboot.config.ds; 2 3 import javax.sql.DataSource; 4 5 import org.apache.ibatis.session.SqlSessionFactory; 6 import org.mybatis.spring.SqlSessionFactoryBean; 7 import org.mybatis.spring.annotation.MapperScan; 8 import org.springframework.beans.factory.annotation.Qualifier; 9 import org.springframework.beans.factory.annotation.Value; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 14 15 import com.alibaba.druid.pool.DruidDataSource; 16 17 @Configuration 18 // 掃描 Mapper 接口並容器管理 19 @MapperScan(basePackages = SlaveDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "slaveSqlSessionFactory") 20 public class SlaveDataSourceConfig { 21 22 // 精確到 slave 目錄,以便跟其餘數據源隔離 23 static final String PACKAGE = "com.carry.springboot.dao.slave"; 24 static final String MAPPER_LOCATION = "classpath:mapper/slave/*.xml"; 25 26 @Value("${slave.datasource.url}") 27 private String url; 28 29 @Value("${slave.datasource.username}") 30 private String user; 31 32 @Value("${slave.datasource.password}") 33 private String password; 34 35 @Value("${slave.datasource.driverClassName}") 36 private String driverClass; 37 38 @Bean(name = "slaveDataSource") 39 public DataSource slaveDataSource() { 40 DruidDataSource dataSource = new DruidDataSource(); 41 dataSource.setDriverClassName(driverClass); 42 dataSource.setUrl(url); 43 dataSource.setUsername(user); 44 dataSource.setPassword(password); 45 return dataSource; 46 } 47 48 @Bean(name = "slaveTransactionManager") 49 public DataSourceTransactionManager slaveTransactionManager() { 50 return new DataSourceTransactionManager(slaveDataSource()); 51 } 52 53 @Bean(name = "slaveSqlSessionFactory") 54 public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) 55 throws Exception { 56 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 57 sessionFactory.setDataSource(slaveDataSource); 58 sessionFactory.setMapperLocations( 59 new PathMatchingResourcePatternResolver().getResources(SlaveDataSourceConfig.MAPPER_LOCATION)); 60 return sessionFactory.getObject(); 61 } 62 }
六、業務層代碼瀏覽器
1 package com.carry.springboot.service.impl; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.carry.springboot.dao.master.UserDao; 7 import com.carry.springboot.dao.slave.CityDao; 8 import com.carry.springboot.domain.City; 9 import com.carry.springboot.domain.User; 10 import com.carry.springboot.service.UserService; 11 12 @Service 13 public class UserServiceImpl implements UserService { 14 15 @Autowired 16 private UserDao userDao; // 主數據源 17 18 @Autowired 19 private CityDao cityDao; // 從數據源 20 21 @Override 22 public User findByName(String userName) { 23 User user = userDao.findByName(userName); 24 City city = cityDao.findByName("北京市"); 25 user.setCity(city); 26 return user; 27 } 28 29 }
寫個controller和request方法,我這裏的是http://localhost:8080/api/user
在瀏覽器中輸入http://localhost:8080/api/user?userName=程序員獲得結果