spring:
datasource:
aps:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://pig-mysql:3306/myaps?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
mes:
driver-class-name: oracle.jdbc.driver.OracleDriver
username: MES
password: MESTEST
url: jdbc:oracle:thin:@*.*.*.*:1523/orayofc
配置类(一)
package com.yofc.aps.config;
import com.alibaba.druid.pool.DruidDataSource;
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;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "apsDataSource")
@Qualifier("apsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.aps")
public DataSource apsDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "iotDataSource")
@Qualifier("iotDataSource")
@ConfigurationProperties(prefix = "spring.datasource.iot")
public DataSource iotDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "mesDataSource")
@Qualifier("mesDataSource")
@ConfigurationProperties(prefix = "spring.datasource.mes")
public DataSource mesDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "fisDataSource")
@Qualifier("fisDataSource")
@ConfigurationProperties(prefix = "spring.datasource.fis")
public DataSource fisDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "apsJdbcTemplate")
public JdbcTemplate apsJdbcTemplate(@Qualifier(value = "apsDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "iotJdbcTemplate")
public JdbcTemplate iotJdbcTemplate(@Qualifier(value = "iotDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "mesJdbcTemplate")
public JdbcTemplate mesJdbcTemplate(@Qualifier(value = "mesDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "fisJdbcTemplate")
public JdbcTemplate fisJdbcTemplate(@Qualifier(value = "fisDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
配置类(二)
package com.yofc.aps.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
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.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryAps",
transactionManagerRef = "transactionManagerAps",
basePackages = {"com.yofc.aps.dao.aps"})
public class ApsConfig {
@Autowired
private Environment env;
@Autowired
@Qualifier("apsDataSource")
private DataSource apsDataSource;
@Primary
@Bean(name = "transactionManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryAps(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryAps")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryAps(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(apsDataSource)
.properties(getVendorProperties())
.packages("com.yofc.aps.domain.aps")
.persistenceUnit("aps")
.build();
}
private Map getVendorProperties() {
Map jpaProperties = new HashMap<>(16);
jpaProperties.put("hibernate.hbm2ddl.auto", "update");
jpaProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.aps.show-sql"));
jpaProperties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
jpaProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.aps.properties.hibernate.format_sql"));
jpaProperties.put("hibernate.dialect", env.getProperty("spring.jpa.aps.properties.hibernate.naming.aps-dialect"));
jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
return jpaProperties;
}
@Primary
@Bean(name = "transactionManagerAps")
public PlatformTransactionManager transactionManagerAps(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryAps(builder).getObject());
}
}
package com.yofc.aps.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
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.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryIot",
transactionManagerRef = "transactionManagerIot",
basePackages = {"com.yofc.aps.dao.iot"})
public class IotConfig {
@Autowired
private Environment env;
@Autowired
@Qualifier("iotDataSource")
private DataSource iotDataSource;
@Bean(name = "iotTransactionManager")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryAps(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryIot")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryAps(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(iotDataSource)
.properties(getVendorProperties())
.packages("com.yofc.aps.domain.iot")
.persistenceUnit("iot")
.build();
}
private Map getVendorProperties() {
Map iotProperties = new HashMap<>(16);
iotProperties.put("hibernate.hbm2ddl.auto", "update");
iotProperties.put("hibernate.show_sql", env.getProperty("spring.jpa.aps.show-sql"));
iotProperties.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
iotProperties.put("hibernate.format_sql", env.getProperty("spring.jpa.aps.properties.hibernate.format_sql"));
iotProperties.put("hibernate.dialect", env.getProperty("spring.jpa.aps.properties.hibernate.naming.aps-dialect"));
iotProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
return iotProperties;
}
}
代码中nativeQuery用法
@Autowired
@Qualifier("entityManagerFactoryIot")
private EntityManagerFactory entityManagerFactory;
启动类
@EnablePigSwagger2
@EnablePigFeignClients
@EnableFeignClients
@EnablePigResourceServer
@SpringCloudApplication
@EnableScheduling
@EntityScan
@SpringBootApplication
public class ApsApp {
public static void main(String[] args) {
SpringApplication.run(ApsApp.class, args);
}
}



