当前位置:首页>正文

如何在springboot里面配置多个数据源,并且配置多个连接池持久层用的是springdatajpa请大神们回答一下 怎么在一个spring里面配置多个数据库连接池

2023-05-21 14:15:26 互联网 未知

如何在springboot里面配置多个数据源,并且配置多个连接池?持久层用的是springdatajpa请大神们回答一下

1.首先在创建应用对象时引入autoConfig


package com
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args)
}
}


2.其次配置文件

######primary#############
datasource.primary.url=jdbc:sqlserver://xx.xx.xx.xx:1433DatabaseName=PlayNowLog
datasource.primary.username=sa
datasource.primary.password=xxxxxx
datasource.primary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

######secondary#############
datasource.secondary.url=jdbc:sqlserver://xx.xx.xx.xx:1433DatabaseName=PlayNow_New
datasource.secondary.username=sa
datasource.secondary.password=xxxxxx
datasource.secondary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

3.再其次是数据源的创建

@Configuration
public class GlobalDataConfiguration {
@Bean(name="primaryDataSource")

怎么在一个spring里面配置多个数据库连接池

配置多个数据源信息,比如dataSource1, dataSource2, dataSource3等等。
自定义一个动态数据源类,同时注入以上这些数据源以及相应的标志,一般用Map区分
利用动态数据源类来拿到相应的数据源操作

spring boot怎么连接多种数据库

新建Spring Boot项目,依赖选择JPA(spring-boot-starter-data-jpa)和Web(spring-bootstarter-web)。

配置基本属性 在application.properties里配置数据源和jpa的相关属性
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=12345spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

定义映射实体类

定义Controller类
@RestControllerpublic class PersonCtroller {
@Autowired PersonServer personServer
@RequestMapping("/rollback")
public Person rollback(Person person){
return personServer.savePersonWithRollBack(person)
}
@RequestMapping("/norollback")
public Person noRollback(Person person){
return personServer.savePersonWithOutRollBack(person)
}
}
定义数据访问层
public interface PersonRepository extends JpaRepository{} 定义Server层 @Servicepublic class PersonServerImp implements PersonServer { @Autowired PersonRepository personRepository @Transactional(rollbackFor = {IllegalArgumentException.class}) @Override public Person savePersonWithRollBack(Person person) { Person p = personRepository.save(person) if (p.getName().equals("xxx")){ throw new IllegalArgumentException("用户已存在,数据会回滚") } return p } } 7 浏览器访问

springboot 中druid怎么连接多个数据库

spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性。
而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据。

springboot如何配置多个jndi数据源

代码:

/**
* 获取数据库连接方法
*/
public Connection getConn() throws
Exception {
Connection conn = null
try {

//connection原始的连接方式可以转换为OracleConnection
Connection con =
this.commonDao.getCurrentConnection()
Connection conWas = null

if(this.wasConnection){//使用tomcat请把biz-context-finance.xml中的wasConnection的值设置为false
注释掉biz-context-core.xml中的id="websphereForOracleConnection"内容

WebSphereNativeJdbcExtractor websphereForOracleConnection =
(WebSphereNativeJdbcExtractor)SpringUtils.getSpringBean("websphereForOracleConnection")

conWas = websphereForOracleConnection.getNativeConnection(con)
}else{

conWas = con
}

conWas.setAutoCommit(false)
conn =
conWas.getMetaData().getConnection()
} catch (DaoException e) {

e.printStackTrace()
}
return conn
}

spring boot怎么配置mongodb的连接池

public MongoClient createMongoClient(MongoClientOptions options)
throws UnknownHostException {
try {
if (hasCustomAddress() || hasCustomCredentials()) {
if (options == null) {
options = MongoClientOptions.builder().build()
}
Listcredentials = null if (hasCustomCredentials()) { String database = this.authenticationDatabase == null ? getMongoClientDatabase() : this.authenticationDatabase credentials = Arrays.asList(MongoCredential.createMongoCRCredential( this.username, database, this.password)) } String host = this.host == null ? "localhost" : this.host int port = this.port == null ? DEFAULT_PORT : this.port return new MongoClient(Arrays.asList(new ServerAddress(host, port)), credentials, options) } // The options and credentials are in the URI return new MongoClient(new MongoClientURI(this.uri, builder(options))) } finally { clearPassword() } }