當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
学习Spring Boot:(二十四)多数据源配置与使用
生活随笔
收集整理的這篇文章主要介紹了
学习Spring Boot:(二十四)多数据源配置与使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
隨著業務量增大,可能有些業務不是放在同一個數據庫中,所以系統有需求使用多個數據庫完成業務需求,我們需要配置多個數據源,從而進行操作不同數據庫中數據。
正文
JdbcTemplate 多數據源
配置
需要在 Spring Boot 中配置多個數據庫連接,當然怎么設置連接參數的 key 可以自己決定,
需要注意的是 Spring Boot 2.0 的默認連接池配置參數好像有點問題,由于默認連接池已從 Tomcat 更改為 HikariCP,以前有一個參數 url,已經改成 hikari.jdbcUrl ,不然無法注冊。我下面使用的版本是 1.5.9。
server:port: 8022 spring:datasource:url: jdbc:mysql://localhost:3306/learn?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driversecond-datasource:url: jdbc:mysql://localhost:3306/learn1?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123457driver-class-name: com.mysql.jdbc.Driver注冊 DataSource
注冊兩個數據源,分別注冊兩個 JdbcTemplate,
@Configuration public class DataSourceConfig {/*** 注冊 data source** @return*/@ConfigurationProperties(prefix = "spring.datasource")@Bean("firstDataSource")@Primary // 有相同實例優先選擇public DataSource firstDataSource() {return DataSourceBuilder.create().build();}@ConfigurationProperties(prefix = "spring.second-datasource")@Bean("secondDataSource")public DataSource secondDataSource() {return DataSourceBuilder.create().build();}@Bean("firstJdbcTemplate")@Primarypublic JdbcTemplate firstJdbcTemplate(DataSource dataSource) {return new JdbcTemplate(dataSource);}@Bean("secondJdbcTemplate")public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDataSource") DataSource dataSource) {return new JdbcTemplate(dataSource);} }測試
@SpringBootTest @RunWith(SpringRunner.class) public class TestJDBC {@Autowiredprivate JdbcTemplate jdbcTemplate;@Autowired@Qualifier("secondJdbcTemplate")private JdbcTemplate jdbcTemplate1;@Beforepublic void before() {jdbcTemplate.update("DELETE FROM employee");jdbcTemplate1.update("DELETE FROM employee");}@Testpublic void testJDBC() {jdbcTemplate.update("insert into employee(id,name,age) VALUES (1, 'wuwii', 24)");jdbcTemplate1.update("insert into employee(id,name,age) VALUES (1, 'kronchan', 23)");Assert.assertThat("wuwii", Matchers.equalTo(jdbcTemplate.queryForObject("SELECT name FROM employee WHERE id=1", String.class)));Assert.assertThat("kronchan", Matchers.equalTo(jdbcTemplate1.queryForObject("SELECT name FROM employee WHERE id=1", String.class)));} }使用 JPA 支持多數據源
配置
相比使用 jdbcTemplate,需要設置下 JPA 的相關參數即可,沒多大變化:
server:port: 8022 spring:datasource:url: jdbc:mysql://localhost:3306/learn?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driversecond-datasource:url: jdbc:mysql://localhost:3306/learn1?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverjpa:show-sql: truedatabase: mysqlhibernate:# update 更新表結構# create 每次啟動刪除上次表,再創建表,會造成數據丟失# create-drop: 每次加載hibernate時根據model類生成表,但是sessionFactory一關閉,表就自動刪除。# validate :每次加載hibernate時,驗證創建數據庫表結構,只會和數據庫中的表進行比較,不會創建新表,但是會插入新值。ddl-auto: updateproperties:hibernate:dialect: org.hibernate.dialect.MySQLDialect首先一樣的是我們要注冊相應的 DataSource,還需要指定相應的數據源所對應的實體類和數據操作層 Repository的位置:
* firstDataSource
- secondDataSource:
測試
@SpringBootTest @RunWith(SpringRunner.class) public class TestDemo {@Autowiredprivate EmployeeDao employeeDao;@Autowiredprivate UserDao userDao;@Beforepublic void before() {employeeDao.deleteAll();userDao.deleteAll();}@Testpublic void test() {Employee employee = new Employee(null, "wuwii", 24);employeeDao.save(employee);User user = new User(null, "KronChan", 24);userDao.save(user);Assert.assertThat(employee, Matchers.equalTo(employeeDao.findOne(Example.of(employee))));Assert.assertThat(user, Matchers.equalTo(userDao.findOne(Example.of(user))));} } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的学习Spring Boot:(二十四)多数据源配置与使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第四范式受邀成为5G消息工作组成员
- 下一篇: 直播预告 | 第四范式2021发布会技术