----------------------------------------------------------------------------------------------------------------
springboot 详解 (一) helloworld 下载demo
springboot 详解 (四)redis & filter 下载demo
springboot 详解 (五)interceptor 下载demo
springboot 详解 (六)servlet & scheduled & listener 下载demo
springboot 详解(七) dubbox & zookeeper 下载(productor) 下载(constumser)
springboot 详解(八) springboot & springcloud
----------------------------------------------------------------------------------------------------------------
package com.curiousby.cn.datasources; import javax.sql.DataSource; 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; /** * @Type DataSourceConfig.java * @Desc * @author cmcc-B100036 * @date 2016年11月15日 下午5:38:44 * @version */ @Configuration public class DataSourceConfig { @Bean(name = "primaryDS") @Qualifier("primaryDS") @Primary @ConfigurationProperties(prefix = "datasource.one.mysql") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDS") @Qualifier("secondaryDS") @ConfigurationProperties(prefix = "datasource.two.mysql") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } }
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = { "com.curiousby.cn.one.dao" }) //设置dao public class RepositoryPrimaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("primaryDS") private DataSource primaryDS; @Bean(name = "entityManagerPrimary") @Primary public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Bean(name = "entityManagerFactoryPrimary") @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { return builder.dataSource(primaryDS).properties(getVendorProperties(primaryDS)) .packages("com.curiousby.cn.one.entity") //设置实体类所在位置 .persistenceUnit("primaryPersistenceUnit").build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerPrimary") @Primary PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); } }
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = { "com.curiousby.cn.two.dao" }) public class RepositorySecondaryConfig { @Autowired private JpaProperties jpaProperties; @Autowired @Qualifier("secondaryDS") private DataSource secondaryDS; @Bean(name = "entityManagerSecondary") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactorySecondary(builder).getObject().createEntityManager(); } @Bean(name = "entityManagerFactorySecondary") public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) { return builder.dataSource(secondaryDS).properties(getVendorProperties(secondaryDS)) .packages("com.curiousby.cn.two.entity") .persistenceUnit("secondaryPersistenceUnit").build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } }
#multi datasources datasource.one.mysql.url=jdbc:mysql://localhost:3306/database datasource.one.mysql.username=root datasource.one.mysql.password=root datasource.one.mysql.driverClassName=com.mysql.jdbc.Driver #DataSource settings for mysql datasource.two.mysql.url=jdbc:mysql://127.0.0.1:3306/database2 datasource.two.mysql.username=root datasource.two.mysql.password=root datasource.two.mysql.driverClassName=com.mysql.jdbc.Driver #one datasource #spring.datasource.url=jdbc:mysql://localhost:3306/database #spring.datasource.username=root #spring.datasource.password=root #spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!