----------------------------------------------------------------------------------------------------------------
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/
谢谢您的赞助,我会做的更好!
相关推荐
本篇文章主要介绍了详解SpringBoot和Mybatis配置多数据源,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了SpringBoot2整合Redis多数据源步骤详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本篇文章主要介绍了详解springboot+mybatis多数据源最简解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了Spring Boot 2.0多数据源配置方法实例详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
主要介绍了SpringBoot整合Druid数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Springboot整合多数据源代码示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
以数据源配置为例: 使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationPropertiesz注解到启动类中定义为Bean。 这里User对象并没有使用@Component相关注解。 @...
主要介绍了Spring boot 连接多数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在本篇文章中我们给大家详细分享了springboot采用多数据源对JdbcTemplate配置的方法,有需要的朋友们可以学习参考下。
SpringBoot Demo http://knight-black-bob.iteye.com/blog/2338320 springboot 详解 ...http://knight-black-bob.iteye.com/blog/2338608 springboot 详解 (三) 多数据源 以及其他 博客 的外链 在本 博客中存在
本篇文章主要介绍了详解基于Spring Boot与Spring Data JPA的多数据源配置,非常具有实用价值,需要的朋友可以参考下
主要介绍了Spring-boot集成pg、mongo多数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本篇文章主要介绍了详解spring boot实现多数据源代码实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
6.2 整合Durid数据源 117 6.3 整合Mybatis 122 6.3.1 注解版 123 6.3.2 配置文件版 124 6.4 整合SpringData JPA 125 6.4.1 SpringData简介 125 6.4.2 整合 126 7 Spring Boot启动配置原理 128 7.1 启动流程...
本篇文章主要介绍了详解利用Spring的AbstractRoutingDataSource解决多数据源的问题。具有一定的参考价值,有兴趣的可以了解一下。
基础数据验证](http://www.mkeeper.club/2018/07/24/SpringBoot%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B2-1-7%20%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81-%E5%9F%BA%E7%A1%80%E6%95%B0%E6%8D%AE%E9%AA%8C%E8%AF%81/) ...
Druid是目前Java语言中最好的数据库连接池之一。结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控...Spring Boot 2.0 以上默认使用 Hikari 数据源,可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源。
包含springboot框架介绍、配置文件详解、数据库访问集成、多数据源配置与使用、事务管理、日志管理、AOP、定时器、使用spring security等
spring-cloud-alibaba+dubbo+nacos+quartz以yml配置方式实现
RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题。