詳解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)行查詢數(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類中的信息,如下:修改完成后,異常也不會(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ì)腳本之家的支持。
- spring boot使用sharding jdbc的配置方式
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)
- SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
- Spring?JDBC配置與使用的實(shí)現(xiàn)
相關(guān)文章
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過(guò)示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07Java中的時(shí)間日期API知識(shí)點(diǎn)總結(jié)
本文給大家總結(jié)了Java中的時(shí)間日期API知識(shí)點(diǎn)以及相關(guān)的實(shí)例代碼分享,有興趣的朋友參考學(xué)習(xí)下。2018-04-04簡(jiǎn)單講解奇偶排序算法及在Java數(shù)組中的實(shí)現(xiàn)
這篇文章主要介紹了奇偶排序算法及Java數(shù)組的實(shí)現(xiàn),奇偶排序的時(shí)間復(fù)雜度為O(N^2),需要的朋友可以參考下2016-04-04mybatis-xml映射文件及mybatis動(dòng)態(tài)sql詳解
XML映射文件的名稱與Mapper接口名稱一致,并且將XML映射文件和Mapper接口放置在相同包下(同包同名),這篇文章主要介紹了mybatis-xml映射文件及mybatis動(dòng)態(tài)sql的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2024-12-12MySQL如何設(shè)置自動(dòng)增長(zhǎng)序列SEQUENCE的方法
本文主要介紹了MySQL如何設(shè)置自動(dòng)增長(zhǎng)序列SEQUENCE的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12springboot中JSONObject遍歷并替換部分json值
這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)
下面小編就為大家?guī)?lái)一篇JAVA 反射 動(dòng)態(tài)調(diào)用不同類的靜態(tài)方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08