欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法

 更新時(shí)間:2018年10月12日 14:13:12   投稿:laozhang  
在本篇文章中我們給大家詳細(xì)分享了springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法,有需要的朋友們可以學(xué)習(xí)參考下。

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)文章

  • JAVA?POI設(shè)置EXCEL單元格格式用法舉例

    JAVA?POI設(shè)置EXCEL單元格格式用法舉例

    這篇文章主要給大家介紹了關(guān)于JAVA?POI設(shè)置EXCEL單元格格式用法的相關(guān)資料,POI中可能會(huì)用到一些需要設(shè)置EXCEL單元格格式的操作,需要的朋友可以參考下
    2023-08-08
  • 一文詳解SpringBoot中CommandLineRunner接口

    一文詳解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-10
  • Maven依賴(lài)junit?@Test報(bào)錯(cuò)的解決方案

    Maven依賴(lài)junit?@Test報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了Maven依賴(lài)junit?@Test報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Jenkins Host key verification failed問(wèn)題解決

    Jenkins Host key verification failed問(wèn)題解決

    這篇文章主要介紹了Jenkins Host key verification failed問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java Applet查找素?cái)?shù)小程序代碼實(shí)例

    Java Applet查找素?cái)?shù)小程序代碼實(shí)例

    這篇文章主要介紹了Java Applet查找素?cái)?shù)小程序代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 深入講解Java中的多態(tài)和抽象類(lèi)

    深入講解Java中的多態(tài)和抽象類(lèi)

    這篇文章主要介紹了深入講解Java中的多態(tài)和抽象類(lèi),有時(shí)候,設(shè)計(jì)一個(gè)數(shù)組或方法的參數(shù),返回值類(lèi)型時(shí),無(wú)法確定具體的類(lèi)型,只能確定是某個(gè)系列的類(lèi)型,這時(shí)就引入了多態(tài),需要的朋友可以參考下
    2023-08-08
  • SpringBoot主鍵ID傳到前端后精度丟失的問(wèn)題解決

    SpringBoot主鍵ID傳到前端后精度丟失的問(wèn)題解決

    這篇文章主要通過(guò)示例為大家詳細(xì)介紹一些SpringBoot如何解決雪花算法主鍵ID傳到前端后精度丟失問(wèn)題,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-05-05
  • hadoop?全面解讀自定義分區(qū)

    hadoop?全面解讀自定義分區(qū)

    Hadoop是一個(gè)由Apache基金會(huì)所開(kāi)發(fā)的分布式系統(tǒng)基礎(chǔ)架構(gòu)。用戶(hù)可以在不了解分布式底層細(xì)節(jié)的情況下,開(kāi)發(fā)分布式程序。充分利用集群的威力進(jìn)行高速運(yùn)算和存儲(chǔ)
    2022-02-02
  • springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼

    這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶(hù)信息的示例

    Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶(hù)信息的示例

    本篇文章主要介紹了Spring Boot中使用LDAP來(lái)統(tǒng)一管理用戶(hù)信息的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評(píng)論