欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解springboot采用多数据源对JdbcTemplate配置的方法

程序员文章站 2024-03-31 12:50:58
springboot多数据源配置,代码如下 datasourceconfig package com.rookie.bigdata.config; im...

springboot多数据源配置,代码如下

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;
 
 /**
  * 采用第一个暑假源进行插入数据
  * @param student
  */
 @override
 public void create(student student) {
 
  jdbctemplate.update("insert into student(stu_no,name,age)value (?,?,?)", student.getstuno(), student.getname(), student.getage());
 
 }
 
 /**
  * 第一个数据源进行插入数据
  * @param stuno
  */
 @override
 public void deletebyno(integer stuno) {
  jdbctemplate.update("delete from student where stu_no=?", stuno);
 }
 
 /**
  * 第二个数据源进行查询数据
  * @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));
 
 }
}

在运行的时候会出现如下异常问题,运行失败,报出java.lang.illegalargumentexception: jdbcurl is required with driverclassname.异常

详解springboot采用多数据源对JdbcTemplate配置的方法

后来经过查资料,发现出现该问题的原因是由于springboot版本的问题,本实例采用的springboot版本为2.0.5,如果将版本改为1.5以前的版本就不会出现如上的问题,其实解决上面的异常主要有如下两种解决方案

方案一:

按照如下方案进行修改application.properties配置文件,如下:修改完成后,上面的异常不会再出现

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类中的信息,如下:修改完成后,异常也不会再出现能够正常运行

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 采用多数据源对jdbctemplate进行配置完美解决,感谢大家对的支持。