#=====================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 #格式化输出的json字符串 spring.jackson.serialization.indent_output=true
/**
* 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);
}
}
@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);
}
}
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);
}
@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;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有