SpringBoot项目中的多数据源支持的方法
1.概述
项目中经常会遇到一个应用需要访问多个数据源的情况,本文介绍在springboot项目中利用springdatajpa技术如何支持多个数据库的数据源。
具体的代码参照该
2.建立实体类(entity)
首先,我们创建两个简单的实体类,分别属于两个不同的数据源,用于演示多数据源数据的保存和查询。
test实体类:
package com.example.demo.test.data; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; @entity @table(name = "test") public class test { @id private integer id; public test(){ } public integer getid() { return this.id; } public void setid(integer id){ this.id = id; } }
other实体类:
package com.example.demo.other.data; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; @entity @table(name = "other") public class other { @id private integer id; public integer getid() { return this.id; } public void setid(integer id){ this.id = id; } }
需要注意的是,这两个实体类分属于不同的package,这一点极为重要,spring会根据实体类所属的package来决定用那一个数据源进行操作。
3.建立repository
分别建立两个实体类对应的repository,用于进行数据操作。
testrepository:
package com.example.demo.test.data; import org.springframework.data.jpa.repository.jparepository; public interface testrepository extends jparepository<test, integer> { }
otherrepository:
package com.example.demo.other.data; import org.springframework.data.jpa.repository.jparepository; public interface otherrepository extends jparepository<other, integer> { }
得益于spring-data-jpa优秀的封装,我们只需创建一个接口,就拥有了对实体类的操作能力。
3.对多数据源进行配置
分别对test和other两个实体类配置对应的数据源。配置的内容主要包含三个要素:
- datasource,数据源的连接信息
- entitymanagerfactory,数据处理
- transactionmanager,事务管理
test实体类的数据源配置 testdataconfig:
package com.example.demo.config; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.boot.autoconfigure.jdbc.datasourcebuilder; import org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.boot.orm.jpa.entitymanagerfactorybuilder; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.primary; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.persistence.entitymanagerfactory; import javax.sql.datasource; @configuration @enabletransactionmanagement @enablejparepositories( entitymanagerfactoryref = "entitymanagerfactory", basepackages = {"com.example.demo.test.data"} ) public class testdataconfig { @autowired private jpaproperties jpaproperties; @primary @bean(name = "datasource") @configurationproperties(prefix = "spring.datasource") public datasource datasource() { return datasourcebuilder.create().build(); } @primary @bean(name = "entitymanagerfactory") public localcontainerentitymanagerfactorybean entitymanagerfactory( entitymanagerfactorybuilder builder, @qualifier("datasource") datasource datasource) { return builder .datasource(datasource) .packages("com.example.demo.test.data") .properties(jpaproperties.gethibernateproperties(datasource)) .persistenceunit("test") .build(); } @primary @bean(name = "transactionmanager") public platformtransactionmanager transactionmanager( @qualifier("entitymanagerfactory") entitymanagerfactory entitymanagerfactory) { return new jpatransactionmanager(entitymanagerfactory); } }
代码中的primary注解表示这是默认数据源。
other实体类的数据源配置 otherdataconfig:
package com.example.demo.config; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.boot.autoconfigure.jdbc.datasourcebuilder; import org.springframework.boot.autoconfigure.orm.jpa.jpaproperties; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.boot.orm.jpa.entitymanagerfactorybuilder; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.transaction.platformtransactionmanager; import org.springframework.transaction.annotation.enabletransactionmanagement; import javax.persistence.entitymanagerfactory; import javax.sql.datasource; @configuration @enabletransactionmanagement @enablejparepositories( entitymanagerfactoryref = "otherentitymanagerfactory", transactionmanagerref = "othertransactionmanager", basepackages = {"com.example.demo.other.data"} ) public class otherdataconfig { @autowired private jpaproperties jpaproperties; @bean(name = "otherdatasource") @configurationproperties(prefix = "other.datasource") public datasource otherdatasource() { return datasourcebuilder.create().build(); } @bean(name = "otherentitymanagerfactory") public localcontainerentitymanagerfactorybean otherentitymanagerfactory( entitymanagerfactorybuilder builder, @qualifier("otherdatasource") datasource otherdatasource) { return builder .datasource(otherdatasource) .packages("com.example.demo.other.data") .properties(jpaproperties.gethibernateproperties(otherdatasource)) .persistenceunit("other") .build(); } @bean(name = "othertransactionmanager") public platformtransactionmanager othertransactionmanager( @qualifier("otherentitymanagerfactory") entitymanagerfactory otherentitymanagerfactory) { return new jpatransactionmanager(otherentitymanagerfactory); } }
3.数据操作
我们创建一个service类testservice来分别对两个数据源进行数据的操作。
package com.example.demo.service; import com.example.demo.other.data.other; import com.example.demo.other.data.otherrepository; import com.example.demo.test.data.test; import com.example.demo.test.data.testrepository; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.component; @component public class testservice { @autowired private testrepository testrepository; @autowired private otherrepository otherrepository; @value("${name:world}") private string name; public string gethellomessage() { test test = new test(); test.setid(1); test = testrepository.save(test); other other = new other(); other.setid(2); other = otherrepository.save(other); return "hello " + this.name + " : test's value = " + test.getid() + " , other's value = " + other.getid(); } }
对test和other分别进行数据插入和读取操作,程序运行后会打印出两个数据源各自的数据。 数据库采用的mysql,连接信息在application.yml进行配置。
spring: datasource: url: jdbc:mysql://localhost:3306/test?characterencoding=utf-8&usessl=false testwhileidle: true validationquery: select 1 from dual username: test password: 11111111 driverclassname: com.mysql.jdbc.driver jpa: database: mysql show-sql: true hibernate: show-sql: true ddl-auto: create naming-strategy: org.hibernate.cfg.improvednamingstrategy properties: hibernate.dialect: org.hibernate.dialect.mysql5dialect other: datasource: url: jdbc:mysql://localhost:3306/other?characterencoding=utf-8&usessl=false testwhileidle: true validationquery: select 1 username: other password: 11111111 driverclassname: com.mysql.jdbc.driver jpa: database: mysql show-sql: true hibernate: show-sql: true ddl-auto: create naming-strategy: org.hibernate.cfg.improvednamingstrategy properties: hibernate.dialect: org.hibernate.dialect.mysql5dialect
test实体对应的是主数据源,采用了spring-boot的默认数据源配置项,other实体单独配置数据源连接。具体应该读取哪一段配置内容,是在配置类otherdataconfig中这行代码指定的。
@configurationproperties(prefix = "other.datasource")
本示例需要建立的数据库用户和库可以通过以下命令处理:
create user 'test'@'localhost' identified by '11111111'; grant all privileges on *.* to 'test'@'localhost'; create user 'other'@'localhost' identified by '11111111'; grant all privileges on *.* to 'other'@'localhost'; create database test; create database other;
4.总结
spring-data-jpa极大的简化了数据库操作,对于多数据源的支持,也只是需要增加一下配置文件和配置类而已。其中的关键内容有3点:
- 配置文件中数据源的配置
- 配置类的编写
- 实体类所在的package必须与配置类中指定的package一致,如otherdataconfig中指定的basepackages = {"com.example.demo.other.data"}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。