pom.xml 文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<!-- SpringBoot整合JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
DataSourceConfig.java
package ccpiteco.net.chaxun.configuration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig{
@Primary
@Bean(name = "zCoasNetDataSource")
@Qualifier("zCoasNetDataSource")
@ConfigurationProperties(prefix="spring.datasource.zCoasNet")
public DataSource zCoasNetDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "zForm6DataSource")
@Qualifier("zForm6DataSource")
@ConfigurationProperties(prefix="spring.datasource.zForm6")
public DataSource zForm6DataSource() {
return DataSourceBuilder.create().build();
}
}
zCoasNetConfig.java
package ccpiteco.net.chaxun.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryZCoasNet",
transactionManagerRef="transactionManagerZCoasNet",
basePackages= { "ccpiteco.net.chaxun.zcoasnet" }) //设置Repository所在位置
public class zCoasNetConfig {
@Autowired
@Qualifier("zCoasNetDataSource")
private DataSource zCoasNetDataSource;
@Primary
@Bean(name = "entityManagerZCoasNet")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryZCoasNet(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryZCoasNet")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryZCoasNet (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(zCoasNetDataSource)
.properties(getVendorProperties(zCoasNetDataSource))
.packages( "ccpiteco.net.chaxun.zcoasnet") //设置实体类所在位置
.persistenceUnit("zCoasNetPersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerZCoasNet")
public PlatformTransactionManager transactionManagerZCoasNet(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryZCoasNet(builder).getObject());
}
}
zForm6Config.java
package ccpiteco.net.chaxun.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryZForm6",
transactionManagerRef="transactionManagerZForm6",
basePackages= {"ccpiteco.net.chaxun.zform6"}) //设置Repository所在位置
public class zForm6Config {
@Autowired
@Qualifier("zForm6DataSource")
private DataSource zForm6DataSource;
@Bean(name = "entityManagerZForm6")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryZForm6(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryZForm6")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryZForm6 (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(zForm6DataSource)
.properties(getVendorProperties(zForm6DataSource))
.packages("ccpiteco.net.chaxun.zform6") //设置实体类所在位置
.persistenceUnit("zForm6PersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerZForm6")
public PlatformTransactionManager transactionManagerZForm6(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryZForm6(builder).getObject());
}
}
application.yml
spring:
# profiles:
# active: dev
# active: prod
datasource:
zCoasNet:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
url: jdbc:jtds:sqlserver://数据库连接url; DatabaseName=数据库
username: root
password: root
zForm6:
driver-class-name: net.sourceforge.jtds.jdbc.Driver
url: jdbc:jtds:sqlserver://数据库连接url; DatabaseName=数据库
username: root
password: root
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database: default
databasePlatform: org.hibernate.dialect.SQLServer2012Dialect
#logging:
# level:
# ROOT: INFO
# ROOT: ERROR
server:
port: 8888
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /