多数据源配置问题

配置多个数据源情况

public class BaseDao {

    //默认数据源
    @Autowired
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    protected NamedParameterJdbcTemplate npjt;

    //第二个数据源
    @Autowired
    @Qualifier("xrJdbcTemplate")
    protected JdbcTemplate jdbcTemplate1;
    
}

继承上面的类 实现多个数据源增删改查
配置数据源(一)

package com.cudatec.xrtools.datasource;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.cudatec.xrtools.dao",
entityManagerFactoryRef = "localEntityManagerFactory",
transactionManagerRef="localTransactionManager")
public class LocalDatasourceConfig {
/**
* 数据源配置对象
* Primary 表示默认的对象,Autowire可注入,不是默认的得明确名称注入
* @return
*/
@Bean(name = "localDataSourceProperties")
@Primary
@ConfigurationProperties(prefix = "local.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
/**
* 数据源对象
* @return
*/
@Bean(name = "localDataSource")
@Primary
@ConfigurationProperties("local.datasource")
public DataSource dataSource() {
return dataSourceProperties().initializeDataSourceBuilder().build();
}
/**
* 实体管理对象
* @param builder 由spring注入这个对象,首先根据type注入(多个就取声明@Primary的对象),否则根据name注入
* @return
*/
@Bean(name = "localEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource()).packages("com.cudatec.xrtools.bean").build();
}
/**
* 事务管理对象
* @return
*/
@Bean(name = "localTransactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
@Primary
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(dataSource());
}
@Bean
@Primary
public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager){
return new TransactionTemplate(platformTransactionManager);
}

}

配置数据源(二)

package com.cudatec.xrtools.datasource;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.cudatec.xrtools.xrDao",
entityManagerFactoryRef = "xrEntityManagerFactory",
transactionManagerRef="xrTransactionManager")
public class XRDatasourceConfig {
/**
* 数据源配置对象
* @return
*/
@Bean(name = "xrDataSourceProperties")
@ConfigurationProperties(prefix = "xr.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
/**
* 数据源对象
* @return
*/
@Bean(name = "xrDataSource")
@ConfigurationProperties("xr.datasource")
public DataSource dataSource() {
return dataSourceProperties().initializeDataSourceBuilder().build();
}
/**
* 实体管理对象
* @return
*/
@Bean(name = "xrEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource()).packages("com.cudatec.xrtools.xrBean").build();
}
/**
* 事务管理对象
* @return
*/
@Bean(name = "xrTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean(name = "xrJdbcTemplate")
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(dataSource());
}
@Bean(name = "xrTransactionTemplate")
public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager){
return new TransactionTemplate(platformTransactionManager);
}

}

数据库配置

customConfig:
msRootPath: /usr/local/xrendertools_maxscript/
userSetsRootPath: D:/xrtoolsUserSets/
ucenter: http://10.23.100.94:8084/ucenter
smsUrl: http://10.23.100.94:89/sms/smsInfo.htm
activityUrl: http://10.23.100.145:8080
webUrl: http://10.23.100.118:8080
uckey: GR5BTy9HWJ
ucappid: 3
smsSize: 6
smsSendSize: 10
savePath: D:\workSpace\download\
# two database config
local:
datasource:
#url: jdbc:mysql://10.23.100.95:3306/xrendertools?useUnicode=true&characterEncoding=utf-8
url: jdbc:mysql://10.23.100.95:3306/xrendertools?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
username: root
#password: Render.123
password: Render.123
driver-class-name: com.mysql.jdbc.Driver
type: org.apache.tomcat.jdbc.pool.DataSource
initial-size: 10
max-wait: 10000
max-active: 200
min-idle: 5
max-idle: 100
validation-query: SELECT 1
test-on-borrow: true
test-while-idle: true
time-between-eviction-runs-millis: 27800
jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=0)

xr:
datasource:
url: jdbc:mysql://10.23.100.95:3306/xrender2017?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
#url: jdbc:mysql://10.23.140.81:3306/render0109?useUnicode=true&characterEncoding=utf-8
username: root
#password: Zxc.1234
password: Render.123
driver-class-name: com.mysql.jdbc.Driver
type: org.apache.tomcat.jdbc.pool.DataSource
initial-size: 10
max-wait: 10000
max-active: 200
min-idle: 5
max-idle: 100
validation-query: SELECT 1
test-on-borrow: true
test-while-idle: true
time-between-eviction-runs-millis: 27800
jdbc-interceptors: ConnectionState;SlowQueryReport(threshold=0)

spring:
# thymeleaf:
# mode: HTML5
# encoding: UTF-8
# content-type: text/html
# cache: false
# prefix: classpath:/templates/web_resource/view/
# datasource:
# url: jdbc:mysql://10.23.100.64:3306/xrendertools?useUnicode=true&characterEncoding=utf-8
# username: root
# password: render.123
# driver-class-name: com.mysql.jdbc.Driver
# type: org.apache.tomcat.jdbc.pool.DataSource
# tomcat:
# initial-size: 10
# max-wait: 10000
# max-active: 200
http:
multipart:
maxFileSize: 5120MB
maxRequestSize: 5120MB


jpa:
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
jackson:
serialization:
indent-output: true
resources:
static-locations: classpath:/templates/web_resource/,classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

server:
port: 4000
session:
timeout: 600000
cookie:
name: xrtools-session
tomcat:
uri-encoding: UTF-8
min-spare-threads: 100
max-threads: 200
accept-count: 500

logging:
level:
root: INFO
file: xrtoolsLogs/xrtools.log
pattern:
file: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n'

yidun:
captchaid: bb486567c8974cbaa8447d7573a832cf
secretid: f4456af49aa106b94d7874b325ff5ecd
secretkey: df6af264005c65cb8978904d4af2fcdf

xneo:
registerurl: http://dev1.cudatec.com:8800/uc/register/xrender
checknameurl: http://dev1.cudatec.com:8800/uc/public/loginName
checkmobileurl: http://dev1.cudatec.com:8800/uc/public/mobile
appname: tWeb
sign: 22c041b6d78f0ccd9bbed0a0a9709789
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值