配置多个数据源情况
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