六、spring boot 1.5.4 配置多数据源
spring boot 已经支持多数据源配置了,无需网上好多那些编写什么类的,特别麻烦,看看如下解决方案,官方的,放心!
1.首先定义数据源配置
#=====================multiple database config============================
#ds1
first.datasource.url=jdbc:mysql://localhost/test?characterEncoding=utf8&useSSL=true
first.datasource.username=root
first.datasource.password=123456
first.datasource.driver-class-name=com.mysql.jdbc.Driver
first.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
first.datasource.max-wait=10000
first.datasource.max-active=200
first.datasource.test-on-borrow=true
first.datasource.initial-size=10 #ds2
second.datasource.url=jdbc:mysql://localhost/test2?characterEncoding=utf8&useSSL=true
second.datasource.username=root
second.datasource.password=123456
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
second.datasource.max-wait=10000
second.datasource.max-active=200
second.datasource.test-on-borrow=true
second.datasource.initial-size=10
#=====================jpa config================================
#实体类维护数据库表结构的具体行为:update/create/create-drop/validate/none
spring.jpa.hibernate.ddl-auto=none
#打印sql语句
spring.jpa.show-sql=true #=============jackson serialize config =========================
#格式化输出的json字符串
spring.jackson.serialization.indent_output=true 2.配置ds1的相关注入对象和启用jpa支持
/**
* Created by hdwang on 2017-06-16.
* 第一个数据源配置
* If you are using Spring Data, you need to configure @EnableJpaRepositories
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.hdwang.dao.datajpa.firstDs",entityManagerFactoryRef = "firstEntityManagerFactory",transactionManagerRef="firstTransactionManager")
public class FirstDsConfig { /**
* 数据源配置对象
* Primary 表示默认的对象,Autowire可注入,不是默认的得明确名称注入
* @return
*/
@Bean
@Primary
@ConfigurationProperties("first.datasource")
public DataSourceProperties firstDataSourceProperties() {
return new DataSourceProperties();
} /**
* 数据源对象
* @return
*/
@Bean
@Primary
@ConfigurationProperties("first.datasource")
public DataSource firstDataSource() {
return firstDataSourceProperties().initializeDataSourceBuilder().build();
} /**
* 实体管理对象
* @param builder 由spring注入这个对象,首先根据type注入(多个就取声明@Primary的对象),否则根据name注入
* @return
*/
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(firstDataSource())
.packages("com.hdwang.entity.dbFirst")
.persistenceUnit("firstDs")
.build();
} /**
* 事务管理对象
* @return
*/
@Bean(name = "firstTransactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
} @Bean
@Primary
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(firstDataSource());
} @Bean
@Primary
public TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager){
return new TransactionTemplate(platformTransactionManager);
}
}
相关知识点:
1.使用@Bean可以创建一个bean对象交给spring容器管理
2.@Bean创建的bean对象的名称默认为方法名,也可以指定
3.@Bean方法参数表示,接收一个bean对象,默认按照type类型接收注入的对象,若要修改为byName方式,可以使用@Qualifier注解注入准确的对象
4.@Primary表示该bean为此类型的默认bean,在其他地方引用的时候用@Autowired即可按照类型注入,不受同类型多个对象影响
5.EnableJpaRepositories表示启用spring data jpa的支持,也就是jpa的新使用方式,注意basePackages指的是 @Repository接口的所在包位置,可配置多个
其他注解就不清楚了!
2.配置ds2的相关注入对象和启用jpa支持
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.hdwang.dao.datajpa.secondDs", entityManagerFactoryRef = "secondEntityManagerFactory",transactionManagerRef = "secondTransactionManager")
public class SecondDsConfig { @Bean
@ConfigurationProperties("second.datasource")
public DataSourceProperties secondDataSourceProperties() {
return new DataSourceProperties();
} @Bean
@ConfigurationProperties("second.datasource")
public DataSource secondDataSource() {
return secondDataSourceProperties().initializeDataSourceBuilder().build();
} /**
* 实体管理对象
* @param builder 由spring注入这个对象,首先根据type注入(多个就取声明@Primary的对象),否则根据name注入
* @return
*/
@Bean
public LocalContainerEntityManagerFactoryBean secondEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondDataSource())
.packages("com.hdwang.entity.dbSecond")
.persistenceUnit("secondDs")
.build();
} /**
* 事物管理对象
* @param secondEntityManagerFactory 实体管理工厂对象(按照名称注入)
* @return 平台事物管理器
*/
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager transactionManager(@Qualifier("secondEntityManagerFactory")LocalContainerEntityManagerFactoryBean secondEntityManagerFactory){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(secondEntityManagerFactory.getObject());
return transactionManager;
} @Bean(name="jdbcTemplate2")
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(secondDataSource());
} @Bean(name = "transactionTemplate2")
public TransactionTemplate transactionTemplate(@Qualifier("secondTransactionManager")PlatformTransactionManager transactionManager){
return new TransactionTemplate(transactionManager);
}
}
3.Repository数据持久层
package com.hdwang.dao.datajpa.firstDs; @Repository
public interface UserRepository extends JpaRepository<User, Integer> {
/**
* spring data jpa 会自动注入实现(根据方法命名规范)
* @return
*/
User findByNumber(String number); @Modifying
@Query("delete from User u where u.id = :id")
void deleteUser(@Param("id")int id);
}
package com.hdwang.dao.datajpa.secondDs; @Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
/**
* spring data jpa 会自动注入实现(根据方法命名规范)
* @return
*/
User findByNumber(String number); @Modifying
@Query("delete from Order o where o.id = :id")
void deleteUser(@Param("id") int id);
}
上面两个接口分属两个数据源,在@EnableJpaRepositories配置好后,这里就可以正确操作相应的数据源了 4.Service服务层,注意事物(接口我就不贴了)
@Service
@Transactional("firstTransactionManager")
public class UserServiceImpl implements UserService { @Autowired
private UserRepository userRepository; @Override
public User findById(int id) {
return this.userRepository.findOne(id);
} @Override
public User findByNumber(String number) {
return this.userRepository.findByNumber(number);
} @Override
public List<User> findAllUserByPage(int page,int size) {
Pageable pageable = new PageRequest(page, size);
Page<User> users = this.userRepository.findAll(pageable);
return users.getContent();
} @Override
public User updateUser(User user,boolean throwEx) {
User userNew = this.userRepository.save(user);
if(throwEx){
throw new RuntimeException("throw a ex");
}
return userNew;
} @Override
public void deleteUser(int id) {
this.userRepository.deleteUser(id);
}
}
@Service
@Transactional("secondTransactionManager")
public class OrderServiceImpl implements OrderService { @Autowired
private OrderRepository orderRepository; @Override
public Order findById(int id) {
return this.orderRepository.findOne(id);
} @Override
public Order updateOrder(Order order, boolean throwEx) {
Order orderNew = this.orderRepository.save(order);
if(throwEx){
throw new RuntimeException("throw a ex");
}
return orderNew;
}
}
知识扩展 1.如果采用传统jpa方式,@EnableJpaRepositories无需配置,配置了也无影响。实现方式如下: ds1相关DaoImpl
@PersistenceContext
private EntityManager entityManager; ds2相关DaoImpl
@PersistenceContext(unitName = "secondDs")
private EntityManager entityManager; 因为ds1的entityManger声明了@Primary,所以无需指明unitName,ds2必须指明。注入了准确的entityManager,就可以直接拿来操作数据库了。service层和上面一样的,@Transactional("xxxManager")指明事物管理器即可! 2.采用jdbcTemplate方式,直接注入到Service层对象即可,so easy!
@Autowired
private JdbcTemplate jdbcTemplate; @Autowired
private TransactionTemplate transactionTemplate; @Resource(name="jdbcTemplate2")
private JdbcTemplate jdbcTemplate2; @Resource(name="transactionTemplate2")
private TransactionTemplate transactionTemplate2; 好了,spring boot 多数据源,完美解决! 而且三种数据库操作方法均支持,包括事物。已经经过实践证明了! 这是官方给出的最佳实践,只是官方文档没写细而已。 项目源码:https://github.com/hdwang123/springboottest
其它问题:
1. jpa配置无法生效,实体属性无法自动转换成数据库字段名,例如 userName -> user_name
解决办法:
entityManagerFactoryBuiler中指明jpa配置
builder.properties(new JpaProperties().getHibernateProperties(firstDataSource()))
六、spring boot 1.5.4 配置多数据源的更多相关文章
- Spring Boot系列学习文章(二) -- 配置多数据源
前言: 在上一章中,我们已经搭建好项目,现在来讲一下如何配置数据源. 由于在有的项目中,用的数据源可能会涉及多个,且是不同类型的,我们接下来就讲解多数据源的配置. 情景描述: 现有项目需要访问不同的数 ...
- Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置
Actuator 简介 Actuator 是 Spring Boot 提供的对应用系统的自省和监控功能.通过 Actuator,可以使用数据化的指标去度量应用的运行情况,比如查看服务器的磁盘.内存.C ...
- 学记:为spring boot写一个自动配置
spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
- Spring Boot之实现自动配置
GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...
- Spring Boot 2.1.1.RELEASE 多数据源配置与使用
有时候,一个系统的功能,需要两个或两个以上的数据库, 在Spring Boot 中要如何配置? How to? #primary primary.spring.datasource.jdbc-url= ...
- spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志
spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
随机推荐
- 【CobaltStrike】CobaltStrike简介与使用
CobaltStrike简介与使用 0x00 简介 1. 什么是CobaltStrike?可以用来做什么? CobatStrike是一款基于java编写的全平台多方协同后渗透攻击框架.CobaltSt ...
- Spark笔记(一):错误总结
1.转义字符: 常见的replaceAll,split,mkstring中涉及到特殊字符的都要加上转义字符,比如str.split("\\|"),str.replaceAll(&q ...
- XSS跨站攻击(二)
本人最近在学习XSS,想总结一下常见的XSS攻击的几种情况,刚好看到<防御 XSS 的七条原则>这篇文章,里面讲的七条防御原则不正是针对XSS的几种利用方式吗?于是,借来学习一下. 原则1 ...
- c# update check
public class UpdateChecker { public static event EventHandler completeCheck; private static bool isC ...
- 20155318 《网络攻防》Exp3 免杀原理与实践
20155318 <网络攻防>Exp3 免杀原理与实践 基础问题 杀软是如何检测出恶意代码的? 基于特征来检测:恶意代码中一般会有一段有较明显特征的代码也就是特征码,如果杀毒软件检测到有程 ...
- 【调试】Core Dump是什么?Linux下如何正确永久开启?
[调试]Core Dump是什么?Linux下如何正确永久开启?
- Boyer and Moore Fast majority vote algorithm(快速选举算法)
问题来来自于leetcode上的一道题目,https://leetcode.com/problems/majority-element/,大意是是找出一个数组中,出现次数超过一个半的数字,要求是O(n ...
- chrome浏览器插件 Octotree 让你浏览GitHub的时候像IDE 一样提供项目目录
GitHub 作为代码托管平台,竟然没有提供项目目录,方便用户在线快速浏览项目结构.所以,在线分析项目源码就会变得很繁琐,必须一层一层点击,然后再一次一次地向上返回.要知道,本来 GitHub 网站在 ...
- python程序出现No module named '_socket' 解决方法
首先看一下这个错误,错误显示没有这个_socket这个模块 看一个简单的程序理解这个错误是怎么出现的 这个程序就是像浏览器发起请求发开一个链接然后关闭,一直循环,运行之后产生这个错误,产生这个错误的原 ...
- 20190215面试-C#操作外设-多线程-shocket
百度了下,ic卡读卡器 文章;C# 读IC卡程序这个文章还不错. 从北京金木雨电子有限公司下载了,兼容IC卡 身份证阅读器的SDK资料,里面有介绍如何连接ic读卡器,对卡进行一些操作. MasterR ...