SpringBoot多資料來源
很多業務場景都需要使用到多資料庫,本文介紹springboot對多資料來源的使用。
這次先說一下application.properties檔案,分別連線了2個資料庫test和test1。完整程式碼如下:
##埠號 server.port=8888 ##資料庫url spring.datasource.test.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false ##資料庫使用者名稱 spring.datasource.test.username=root ##資料庫密碼 spring.datasource.test.password=root ##資料庫驅動 spring.datasource.test.driver-class-name=com.mysql.jdbc.Driver ##資料庫url spring.datasource.test2.url=jdbc:mysql://localhost:3306/test2?characterEncoding=utf8&useSSL=false ##資料庫使用者名稱 spring.datasource.test2.username=root ##資料庫密碼 spring.datasource.test2.password=root ##資料庫驅動 spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=create ##控制檯列印sql spring.jpa.show-sql=true
然後說一下處理多資料來源的DataSourceConfig,其中@ConfigurationProperties註解對應剛才的資料庫,而且這個重點是一定要有一個主資料來源,並且在上面加上@Primary,程式碼如下:
package com.dalaoyang.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import javax.sql.DataSource; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.config * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @Configuration public class DataSourceConfig { @Bean(name = "testDataSource") @Qualifier("testDataSource") @ConfigurationProperties(prefix="spring.datasource.test") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "test2DataSource") @Qualifier("test2DataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.test2") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }
接下來是對應test資料庫的配置,其中需要說一下的是@EnableJpaRepositories註解裡面的basePackages屬性對應的是這個資料來源對應的repository(因為本文使用的是jpa), @Qualifier註解內的value要和DataSourceConfig的值一致即可。
程式碼如下:
package com.dalaoyang.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.persistence.EntityManager; import javax.sql.DataSource; import java.util.Map; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.config * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactoryPrimary", transactionManagerRef="transactionManagerPrimary", basePackages= { "com.dalaoyang.repository.datasource" }) public class TestDataSourceConfig { @Autowired @Qualifier("testDataSource") private DataSource dataSource; @Primary @Bean(name = "entityManagerPrimary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Primary @Bean(name = "entityManagerFactoryPrimary") public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) { return builder .dataSource(dataSource) .properties(getVendorProperties(dataSource)) .packages("com.dalaoyang.entity.datasource") //設定實體類所在位置 .persistenceUnit("primaryPersistenceUnit") .build(); } @Autowired private JpaProperties jpaProperties; private MapgetVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Primary @Bean(name = "transactionManagerPrimary") public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
Test2DataSourceConfig就不多說了,和TestDataSourceConfig原理一致,程式碼如下:
package com.dalaoyang.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.persistence.EntityManager; import javax.sql.DataSource; import java.util.Map; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.config * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackages= { "com.dalaoyang.repository.datasource2" }) //設定Repository所在位置 public class Test2DataSourceConfig { @Autowired @Qualifier("test2DataSource") private DataSource dataSource; @Bean(name = "entityManagerSecondary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactorySecondary(builder).getObject().createEntityManager(); } @Bean(name = "entityManagerFactorySecondary") public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) { return builder .dataSource(dataSource) .properties(getVendorProperties(dataSource)) .packages("com.dalaoyang.entity.datasource2") //設定實體類所在位置 .persistenceUnit("secondaryPersistenceUnit") .build(); } @Autowired private JpaProperties jpaProperties; private MapgetVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } }
下面是對應的model和repository
City類
package com.dalaoyang.entity.datasource; import javax.persistence.*; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.Entity * @email 397600342@qq.com * @date 2018/4/7 */ @Entity @Table(name="city") public class City { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int cityId; private String cityName; private String cityIntroduce; public City(int cityId, String cityName, String cityIntroduce) { this.cityId = cityId; this.cityName = cityName; this.cityIntroduce = cityIntroduce; } public City(String cityName, String cityIntroduce) { this.cityName = cityName; this.cityIntroduce = cityIntroduce; } public City() { } public int getCityId() { return cityId; } public void setCityId(int cityId) { this.cityId = cityId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getCityIntroduce() { return cityIntroduce; } public void setCityIntroduce(String cityIntroduce) { this.cityIntroduce = cityIntroduce; } }
House類
package com.dalaoyang.entity.datasource2; import javax.persistence.*; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.entity * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @Entity @Table(name="house") public class House { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int houseId; private String houseName; private String houseIntroduce; public int getHouseId() { return houseId; } public void setHouseId(int houseId) { this.houseId = houseId; } public String getHouseName() { return houseName; } public void setHouseName(String houseName) { this.houseName = houseName; } public String getHouseIntroduce() { return houseIntroduce; } public void setHouseIntroduce(String houseIntroduce) { this.houseIntroduce = houseIntroduce; } public House(String houseName, String houseIntroduce) { this.houseName = houseName; this.houseIntroduce = houseIntroduce; } }
CityRepository
package com.dalaoyang.repository.datasource; import com.dalaoyang.entity.datasource.City; import org.springframework.data.jpa.repository.JpaRepository; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.Repository * @email 397600342@qq.com * @date 2018/4/7 */ public interface CityRepository extends JpaRepository{ }
HouseRepository
package com.dalaoyang.repository.datasource2; import com.dalaoyang.entity.datasource2.House; import org.springframework.data.jpa.repository.JpaRepository; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.repository.datasource2 * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ public interface HouseRepository extends JpaRepository{ }
pom檔案
4.0.0 com.dalaoyang springboot_datasources 0.0.1-SNAPSHOT jar springboot_datasources springboot_datasources org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE <!-- lookup parent from repository --> UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
因為上面這些都沒什麼可說的,都是和正常的寫法沒什麼區別,最後是TestController測試類
package com.dalaoyang.controller; import com.dalaoyang.entity.datasource.City; import com.dalaoyang.entity.datasource2.House; import com.dalaoyang.repository.datasource.CityRepository; import com.dalaoyang.repository.datasource2.HouseRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email yangyang@dalaoyang.cn * @date 2018/4/10 */ @RestController public class TestController { @Autowired CityRepository cityRepository; @Autowired HouseRepository houseRepository; @GetMapping("/testDataSource") public String testDataSource(){ City city = new City("北京","中國首都"); cityRepository.save(city); return "success"; } @GetMapping("/testDataSource2") public String testDataSource2(){ House house = new House("豪宅","特別大的豪宅"); houseRepository.save(house); return "success"; } }
啟動專案可以看到test資料庫中新建了city表,test2資料庫中新建了house表。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2618/viewspace-2810753/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SpringBoot配置多資料來源Spring Boot
- springBoot 多資料來源配置Spring Boot
- springboot多資料來源配置Spring Boot
- SpringBoot 的多資料來源配置Spring Boot
- SpringBoot 配置多資料來源 MyBatisSpring BootMyBatis
- SpringBoot 整合多資料來源(MyBatis + Druid)Spring BootMyBatisUI
- springboot liquibase整合mysql與clickhouse多資料來源Spring BootUIMySql
- springboot 配置多個資料來源,@MapperScan方式繫結Spring BootAPP
- springboot 多資料來源,最簡單的整合方式Spring Boot
- Springboot 多資料來源配置,結合tk-mybatisSpring BootMyBatis
- springboot-jta-atomikos多資料來源事務管理Spring Boot
- springboot+druid+mybatis plus的多資料來源配置Spring BootUIMyBatis
- Springboot 配置多資料來源Mybatis的UnderScore不生效Spring BootMyBatis
- SpringBoot整合MyBatis-Plus實現多資料來源操作Spring BootMyBatis
- SpringBoot2.x Data JPA 多資料來源爬坑Spring Boot
- SpringBoot | 3.1 配置資料來源Spring Boot
- SpringBoot整合Druid資料來源Spring BootUI
- Springboot通過AOP整合多資料來源,分析@Transaction切換資料來源不生效問題Spring Boot
- springboot整合Druid資料來源[springboot8]Spring BootUI
- 多資料來源配置
- SpringBoot2 整合JTA元件,多資料來源事務管理Spring Boot元件
- springboot 2 Hikari 多資料來源配置問題(dataSourceClassName or jdbcUrl is required)Spring BootJDBCUI
- SpringBoot資料來源相關配置Spring Boot
- 資料來源(DataSource)是什麼以及SpringBoot中資料來源配置Spring Boot
- SpringBoot 2.x quartz多資料來源定時任務配置Spring Bootquartz
- SpringBoot2.x 多資料來源切換、druid監控、tkSpring BootUI
- Springboot整合mybatis實現多資料來源所遇到的問題Spring BootMyBatis
- 全程解析,MyBatis在SpringBoot中的動態多資料來源配置MyBatisSpring Boot
- 基於SpirngBoot2.0+ 的 SpringBoot+Mybatis 多資料來源配置Spring BootMyBatis
- MyBatis配置多資料來源MyBatis
- web 配置多資料來源Web
- Spring多資料來源配置Spring
- 多資料來源與動態資料來源的權衡
- springboot 配置DRUID資料來源的方法Spring BootUI
- springboot啟動提示缺少資料來源Spring Boot
- springboot+ mybatisplus+druid 實現多資料來源+分散式事務Spring BootMyBatisUI分散式
- SpringBoot2 基礎案例(06):引入JdbcTemplate,和多資料來源配置Spring BootJDBC
- SpringBoot+Mybatis+ Druid+PageHelper 實現多資料來源並分頁Spring BootMyBatisUI