這篇文章給大家分享的是有關(guān)springboot如何采用多數(shù)據(jù)源對JdbcTemplate配置的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)公司是專業(yè)的可克達拉網(wǎng)站建設(shè)公司,可克達拉接單;提供網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行可克達拉網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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; /** * 采用第一個暑假源進行插入數(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()); } /** * 第一個數(shù)據(jù)源進行插入數(shù)據(jù) * @param stuNo */ @Override public void deleteByNo(Integer stuNo) { jdbcTemplate.update("DELETE FROM student WHERE stu_no=?", stuNo); } /** * 第二個數(shù)據(jù)源進行查詢數(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
測試代碼如下
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)); } }
在運行的時候會出現(xiàn)如下異常問題,運行失敗,報出java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.異常
后來經(jīng)過查資料,發(fā)現(xiàn)出現(xiàn)該問題的原因是由于springboot版本的問題,本實例采用的springboot版本為2.0.5,如果將版本改為1.5以前的版本就不會出現(xiàn)如上的問題,其實解決上面的異常主要有如下兩種解決方案
方案一:
按照如下方案進行修改application.properties配置文件,如下:修改完成后,上面的異常不會再出現(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配置文件不進行修改,修改DataSourceConfig類中的信息,如下:修改完成后,異常也不會再出現(xià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(); } }
感謝各位的閱讀!關(guān)于“springboot如何采用多數(shù)據(jù)源對JdbcTemplate配置”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!