在配置文件中的配置如下:
datasource:
primary:
jdbc-url: jdbc:sqlserver://115.28.107.219:1433;database=EnterpriseManufacturingSystem;
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: 89883365aA
initial-size: 2 #定义初始连接数
min-idle: 1 #定义最小连接数
max-active: 2 #定义最大连接数
max-wait: 60000 #配置获取连接等待超时的时间
time-between-eviction-runs-millis: 6000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
min-evictable-idle-time-millis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
validation-query: SELECT 1 FROM DUAL #验证使用的SQL语句
test-while-idle: true #指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
test-on-borrow: false #借出连接时不要测试,否则很影响性能
test-on-return: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
pool-prepared-statements: false #打开PSCache,并且指定每个连接上PSCache的大小
max-pool-prepared-statement-per-connection-size: 20
filters: stat #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
secondary:
jdbc-url: jdbc:sqlserver://115.28.107.219:1433;database=JzdOADB;
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: 89883365aA
initial-size: 2 #定义初始连接数
min-idle: 1 #定义最小连接数
max-active: 2 #定义最大连接数
max-wait: 60000 #配置获取连接等待超时的时间
time-between-eviction-runs-millis: 6000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
min-evictable-idle-time-millis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
validation-query: SELECT 1 FROM DUAL #验证使用的SQL语句
test-while-idle: true #指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
test-on-borrow: false #借出连接时不要测试,否则很影响性能
test-on-return: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
pool-prepared-statements: false #打开PSCache,并且指定每个连接上PSCache的大小
max-pool-prepared-statement-per-connection-size: 20
filters: stat #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
third:
jdbc-url: jdbc:sqlserver://115.28.107.219:1433;database=JzdOADB;
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
username: sa
password: 89883365aA
initial-size: 2 #定义初始连接数
min-idle: 1 #定义最小连接数
max-active: 2 #定义最大连接数
max-wait: 60000 #配置获取连接等待超时的时间
time-between-eviction-runs-millis: 6000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
min-evictable-idle-time-millis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
validation-query: SELECT 1 FROM DUAL #验证使用的SQL语句
test-while-idle: true #指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
test-on-borrow: false #借出连接时不要测试,否则很影响性能
test-on-return: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
pool-prepared-statements: false #打开PSCache,并且指定每个连接上PSCache的大小
max-pool-prepared-statement-per-connection-size: 20
filters: stat #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
pom.xml添加如下jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
在启动类中添加如下代码:
@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);
}
@Bean(name = "thirdJdbcTemplate")
public JdbcTemplate thirdJdbcTemplate(@Qualifier("thirdDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
新建一个类DataSourceConfig,代码如下:
package com.sxl.config;
import javax.sql.DataSource;
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;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "thirdDataSource")
@Qualifier("thirdDataSource")
@ConfigurationProperties(prefix="spring.datasource.third")
public DataSource thirdryDataSource() {
return DataSourceBuilder.create().build();
}
}
添加声明:
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate2;
@Autowired
@Qualifier("thirdJdbcTemplate")
protected JdbcTemplate jdbcTemplate3;
切换数据库连接,代码如下:
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:sqlserver://115.28.107.219:1433;database="+user.getCompanyID()+"");
dataSource.setUsername("sa");
dataSource.setPassword("89883365aA");
jdbcTemplate3.setDataSource(dataSource);
jdbcTemplate2.update("insert into TB_UserInfo(CompanyID,UserID,Pwd,UserName,Phone,RoleID) values(?, ?,?, ?,?, ?)",
user.getCompanyID(), user.getUserID(), user.getPassword(), user.getUserName(), user.getUserAccount(),user.getRoleIDs());
jdbcTemplate3.update("insert into TB_UserInfo(CompanyID,UserID,Pwd,UserName,Phone,RoleID) values(?, ?,?, ?,?, ?)",
user.getCompanyID(), user.getUserID(), user.getPassword(), user.getUserName(), user.getUserAccount(),user.getRoleIDs());
创建数据库代码如下:
public BaseDto RegisterCompany(OAMainCompany oAMainCompany) {
try {
String sql="create database "+oAMainCompany.getCompanyid()+" on primary( name='cpDB_data',"
+ "filename='D:\\stuDB_data.mdf',size=5mb,maxsize=100mb,filegrowth=15%)log on(name='cpDB_log',"
+ "filename='D:\\stuDB_log.ldf',size=2mb,maxsize=20mb,filegrowth=1mb)";
jdbcTemplate2.execute(sql);
jdbcTemplate2.update("insert into TB_MainCompany(CompanyID,CompanyName,CompanyType,Size,OwnerID,CreateUserID,"
+ "DataServer,UserServer,PwdServer,NameServer,Logo,Location,State) values(?, ?,?, ?,?, ?,?, ?,?, ?,?, ?,?)",
oAMainCompany.getCompanyid(), oAMainCompany.getCompanyname(),oAMainCompany.getCompanytype(),oAMainCompany.getSize(),
oAMainCompany.getOwnerid(),oAMainCompany.getCreateuserid(),oAMainCompany.getDataserver(),oAMainCompany.getUserserver(),
oAMainCompany.getPwdserver(),oAMainCompany.getNameserver(),oAMainCompany.getLogo(),oAMainCompany.getLocation(),oAMainCompany.getState());
Map<String, Object> map = new HashMap<String, Object>();
map.put("result", true);
return new BaseDto(ResponseCode.SUCCESS.code,"创建成功!",map);
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new BaseDto(ResponseCode.EXPECTATION_FAILED.code,"创建过程中出现异常!");
}
}