Springboot jpa多数据源
1.SpringBootApplication
package com.xx.xxx; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.client.RestTemplate; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableTransactionManagement public class Application { @Autowired private RestTemplateBuilder restTemplateBuilder; @Bean public RestTemplate restTemplate(){ return restTemplateBuilder.build(); } @Bean public BCryptPasswordEncoder bCryptPasswordEncoder(){ return new BCryptPasswordEncoder(); } @Bean @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setPersistenceUnitName("default"); return factoryBean; } @Bean @Primary PlatformTransactionManager transactionManager(){ return new JpaTransactionManager(entityManagerFactory().getObject()); } @Bean public LocalContainerEntityManagerFactoryBean boJun(){ LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setPersistenceUnitName("boJun"); return factoryBean; } @Bean PlatformTransactionManager transactionManager2(){ return new JpaTransactionManager(boJun().getObject()); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.resources/META-INF/persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="default" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://192.168.80.174:3306/xx?characterEncoding=UTF-8"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="xxx"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> <persistence-unit name="boJun" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@//192.168.80.128:1521/xxx"/> <property name="hibernate.connection.username" value="xx"/> <property name="hibernate.connection.password" value="xxx"/> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence>
3.dao
package com.xx.xxx.modules.repertory.dao; import org.hibernate.SQLQuery; import org.hibernate.transform.Transformers; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; import java.util.Map;
/** * @author DurantSimpson * @desc * @create 2018-05-23 17:53 **/ @Repository public class ProductDao { @PersistenceContext(unitName = "boJun") private EntityManager entityManager; public List<Map<String,Object>> fuzzyByNo(String productNo){ String sql = "SELECT name productno FROM M_PRODUCT where NAME like '%'||'"+productNo+"'||'%'"; return entityManager.createNativeQuery(sql).getResultList(); } public List<Map<String,Object>> getStoreStock(String productNo){ String sql = "SELECT name productno FROM M_PRODUCT where NAME like '%'||'"+productNo+"'||'%'"; return entityManager.createNativeQuery(sql).unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list(); } }