企业分布式微服务云SpringCloud SpringBoot mybatis (十)Spring Boot多数据源配置与使用Spring-data-jpa支持...
Spring-data-jpa支持
对于数据源的配置可以沿用上例中DataSourceConfig的实现。
新增对第一数据源的JPA配置,注意两处注释的地方,用于指定数据源对应的Entity实体和Repository定义位置,用@Primary区分主数据源。
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactoryPrimary", transactionManagerRef="transactionManagerPrimary", basePackages= { "com.didispace.domain.p" }) //设置Repository所在位置 public class PrimaryConfig {
@Autowired @Qualifier("primaryDataSource")
private DataSource primaryDataSource;@Primary
@Bean(name = "entityManagerPrimary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}@Primary
@Bean(name = "entityManagerFactoryPrimary")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {return builder.dataSource(primaryDataSource).properties(getVendorProperties(primaryDataSource)).packages("com.didispace.domain.p") //设置实体类所在位置.persistenceUnit("primaryPersistenceUnit").build();
}@Autowired
private JpaProperties jpaProperties;private Map<String, String> getVendorProperties(DataSource dataSource) {return jpaProperties.getHibernateProperties(dataSource);
}@Primary
@Bean(name = "transactionManagerPrimary")
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
复制代码
} 新增对第二数据源的JPA配置,内容与第一数据源类似,具体如下:
@Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackages= { "com.didispace.domain.s" }) //设置Repository所在位置 public class SecondaryConfig {
@Autowired @Qualifier("secondaryDataSource")
private DataSource secondaryDataSource;@Bean(name = "entityManagerSecondary")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}@Bean(name = "entityManagerFactorySecondary")
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {return builder.dataSource(secondaryDataSource).properties(getVendorProperties(secondaryDataSource)).packages("com.didispace.domain.s") //设置实体类所在位置.persistenceUnit("secondaryPersistenceUnit").build();
}@Autowired
private JpaProperties jpaProperties;private Map<String, String> getVendorProperties(DataSource dataSource) {return jpaProperties.getHibernateProperties(dataSource);
}@Bean(name = "transactionManagerSecondary")
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
复制代码
} 完成了以上配置之后,主数据源的实体和数据访问对象位于:com.didispace.domain.p,次数据源的实体和数据访问接口位于:com.didispace.domain.s。
分别在这两个package下创建各自的实体和数据访问接口
主数据源下,创建User实体和对应的Repository接口 @Entity public class User {
@Id
@GeneratedValue
private Long id;@Column(nullable = false)
private String name;@Column(nullable = false)
private Integer age;public User(){}public User(String name, Integer age) {this.name = name;this.age = age;
}// 省略getter、setter
复制代码
} public interface UserRepository extends JpaRepository<User, Long> {
}
从数据源下,创建Message实体和对应的Repository接口 @Entity public class Message {
@Id
@GeneratedValue
private Long id;@Column(nullable = false)
private String name;@Column(nullable = false)
private String content;public Message(){}public Message(String name, String content) {this.name = name;this.content = content;
}// 省略getter、setter
复制代码
} public interface MessageRepository extends JpaRepository<Message, Long> {
} 接下来通过测试用例来验证使用这两个针对不同数据源的配置进行数据操作。
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class ApplicationTests {
@Autowired
private UserRepository userRepository;
@Autowired
private MessageRepository messageRepository;@Test
public void test() throws Exception {userRepository.save(new User("aaa", 10));userRepository.save(new User("bbb", 20));userRepository.save(new User("ccc", 30));userRepository.save(new User("ddd", 40));userRepository.save(new User("eee", 50));Assert.assertEquals(5, userRepository.findAll().size());messageRepository.save(new Message("o1", "aaaaaaaaaa"));messageRepository.save(new Message("o2", "bbbbbbbbbb"));messageRepository.save(new Message("o3", "cccccccccc"));Assert.assertEquals(3, messageRepository.findAll().size());}
复制代码
}
源码来源:http://minglisoft.cn/honghu/technology.html
企业分布式微服务云SpringCloud SpringBoot mybatis (十)Spring Boot多数据源配置与使用Spring-data-jpa支持...相关推荐
- 企业分布式微服务云SpringCloud SpringBoot mybatis - 服务消费者(Feign)
一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (九)服务链路追踪(Spring Cloud Sleuth)...
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Add sleuth to the classpath of a Spring Bo ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (七)高可用的分布式配置中心(Spring Cloud Config)...
讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下: 一. ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十一)docker部署spring cloud项目
一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (二)服务消费者(rest+ribbon)
一.ribbon简介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (二)Spring Boot属性配置文件详解...
相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (十二)断路器监控(Hystrix Dashboard)...
在我的第四篇文章断路器讲述了如何使用断路器,并简单的介绍了下Hystrix Dashboard组件,这篇文章更加详细的介绍Hystrix Dashboard. 一.Hystrix Dashboard简 ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (九)Spring Boot多数据源配置与使用(JdbcTemplate支持)...
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...
最新文章
- ZOJ 3329 One Person Game 带环的概率DP
- 零件库管理信息系统设计--part03:管理员登录部分设计
- gazebo卡了_ardupilot gazebo打开卡死解决办法
- 凝思系统改时间_国产操作系统往事:四十年激变,终再起风云
- GARFIELD@04-02-2005
- 接口和抽象类有什么区别?
- Sugar Bytes WOW2 for Mac - 多功能滤波效果器
- Android NDK学习(1) 简介
- Java项目文件目录结构介绍
- windows安装mysql-community-8.0.13.0
- 语音合成商业化:科大讯飞向左,魔音工坊向右
- JZ73 翻转单词序列
- VLAN应用篇系列:(1)华为 H3C交换机多种划分VLAN方式配置
- MySQL的幻读是怎么被解决的?
- 电脑硬件入门基础知识——看完就会选电脑啦
- fedora 16 linux 配置 MP3 RMVB 解码器
- python-docx 设置表格边框
- 2020.04.01愚人节与大家见面啦!
- 设计模式系列:搞懂组合模式,单对象与组合对象对外统一接口
- 计蒜客 T1895切蛋糕(单调队列)
热门文章
- ldo和dcdc功耗_ldo与dcdc区别、原理及应用详解
- Sql Server中实现Mysql中的group_concat函数效果
- 驱动下通过进程PID获得进程名 (动态获取ImageFileName在EPROCESS结构体中的相对偏移)...
- JVM——Java内存区域相关3
- python基础15 ---面像对象的程序设计
- python使用xlrd模块读写excel
- ATS (apache traffic server) http_ui 设置与使用
- 红旗桌面版本最新运用要领和结果解答100例-3
- Linux安装QTCreator问题解决汇总
- sql数据库简单增删改查