詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
springboot多數(shù)據(jù)源配置,代碼如下
DataSourceConfig
package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; /** * @author * @date 2018/10/10 */ @Configuration public class DataSourceConfig { @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix="spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @Primary @ConfigurationProperties(prefix="spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "primaryJdbcTemplate") public JdbcTemplate primaryJdbcTemplate( @Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate( @Qualifier("secondaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
StudentServiceImpl
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; /** * @author * @date 2018/10/9 */ @Service public class StudentServiceImpl implements StudentService { @Autowired @Qualifier("primaryJdbcTemplate") private JdbcTemplate jdbcTemplate; @Autowired @Qualifier("secondaryJdbcTemplate") private JdbcTemplate jdbcTemplate2; /** * 采用第一個(gè)暑假源進(jìn)行插入數(shù)據(jù) * @param student */ @Override public void create(Student student) { jdbcTemplate.update("INSERT INTO student(stu_no,name,age)VALUE (?,?,?)", student.getStuNo(), student.getName(), student.getAge()); } /** * 第一個(gè)數(shù)據(jù)源進(jìn)行插入數(shù)據(jù) * @param stuNo */ @Override public void deleteByNo(Integer stuNo) { jdbcTemplate.update("DELETE FROM student WHERE stu_no=?", stuNo); } /** * 第二個(gè)數(shù)據(jù)源進(jìn)行查詢(xún)數(shù)據(jù) * @param stuNo * @return */ @Override public Integer queryByStuNo(Integer stuNo) { return jdbcTemplate2.queryForObject("select count(1) from student", Integer.class); } }
配置文件 application.properties
spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.url=jdbc:mysql://localhost:3306/springboot spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
測(cè)試代碼如下
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * @author liuxili * @date 2018/10/10 */ @RunWith(SpringRunner.class) @SpringBootTest public class StudentServiceImplTest { @Autowired private StudentServiceImpl studentService; @Test public void create1() throws Exception { Student student = new Student(); student.setStuNo(1L); student.setName("張三"); student.setAge(23); studentService.create(student); } @Test public void deleteByName1() throws Exception { studentService.deleteByNo(1); } @Test public void queryByStuNo1() throws Exception { System.out.println(studentService.queryByStuNo(1)); } }
在運(yùn)行的時(shí)候會(huì)出現(xiàn)如下異常問(wèn)題,運(yùn)行失敗,報(bào)出java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.異常
后來(lái)經(jīng)過(guò)查資料,發(fā)現(xiàn)出現(xiàn)該問(wèn)題的原因是由于springboot版本的問(wèn)題,本實(shí)例采用的springboot版本為2.0.5,如果將版本改為1.5以前的版本就不會(huì)出現(xiàn)如上的問(wèn)題,其實(shí)解決上面的異常主要有如下兩種解決方案
方案一:
按照如下方案進(jìn)行修改application.properties配置文件,如下:修改完成后,上面的異常不會(huì)再出現(xiàn)
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/springboot spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/springboot spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
方案二:
原有的application.properties配置文件不進(jìn)行修改,修改DataSourceConfig類(lèi)中的信息,如下:修改完成后,異常也不會(huì)再出現(xiàn)能夠正常運(yùn)行
application.properties
spring.datasource.primary.url=jdbc:mysql://localhost:3306/springboot spring.datasource.primary.username=root spring.datasource.primary.password=root spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver spring.datasource.secondary.url=jdbc:mysql://localhost:3306/springboot spring.datasource.secondary.username=root spring.datasource.secondary.password=root spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
DataSourceConfig
package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 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 org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; /** * @author * @date 2018/10/10 */ @Configuration public class DataSourceConfig { @Bean(name = "primaryJdbcTemplate") public JdbcTemplate primaryJdbcTemplate( @Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate( @Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Primary @Bean(name = "primaryDataSourceProperties") @Qualifier("primaryDataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSourceProperties primaryDataSourceProperties() { return new DataSourceProperties(); } @Bean(name = "secondaryDataSourceProperties") @Qualifier("secondaryDataSourceProperties") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSourceProperties secondaryDataSourceProperties() { return new DataSourceProperties(); } @Primary @Bean(name = "primaryDataSource") @Qualifier("primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return primaryDataSourceProperties().initializeDataSourceBuilder().build(); } @Bean(name = "secondaryDataSource") @Qualifier("secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return primaryDataSourceProperties().initializeDataSourceBuilder().build(); } }
至此,springboot 采用多數(shù)據(jù)源對(duì)JdbcTemplate進(jìn)行配置完美解決,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
一文詳解SpringBoot中CommandLineRunner接口
Spring Boot的CommandLineRunner接口是一個(gè)函數(shù)式接口,用于在Spring Boot應(yīng)用程序啟動(dòng)后執(zhí)行一些初始化操作,它提供了一個(gè)run方法,該方法在應(yīng)用程序啟動(dòng)后被調(diào)用,本文給大家詳細(xì)介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下2023-10-10Maven依賴(lài)junit?@Test報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Maven依賴(lài)junit?@Test報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Jenkins Host key verification failed問(wèn)題解決
這篇文章主要介紹了Jenkins Host key verification failed問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Java Applet查找素?cái)?shù)小程序代碼實(shí)例
這篇文章主要介紹了Java Applet查找素?cái)?shù)小程序代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot主鍵ID傳到前端后精度丟失的問(wèn)題解決
這篇文章主要通過(guò)示例為大家詳細(xì)介紹一些SpringBoot如何解決雪花算法主鍵ID傳到前端后精度丟失問(wèn)題,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-05-05springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶(hù)信息的示例
本篇文章主要介紹了Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶(hù)信息的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01