springboot:jpa 如何做多数据源

service:

1.配置里配置多个数据源

application.properties

spring.datasource.primary.url=
spring.datasource.primary.username=
spring.datasource.primary.password=
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.primary.max-idle=10
spring.datasource.primary.maximum-pool-size=100
spring.datasource.primary.max-wait=10000
spring.datasource.primary.min-idle=5
spring.datasource.primary.initial-size=5

2.不同数据源对应多个文件,以下举一个数据源例子作为说明 如

primary

上代码:

注意:只有一个主数据源,如果不是主数据源,配置下面这个文件时去掉primary那个标签即可。

package com.***.***.web.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryPrimary",
        transactionManagerRef = "transactionManagerPrimary",
        basePackages = {"com.***.***.dao.**.repository"})          //指定dao路径,数据库操作
public class PrimaryDBConf {

    /**
     * 构建主数据源
     *
     * @return
     */
    @Bean
    @Primary  //只有一个主数据源,如果不是主数据源,这个标签要去掉!!!
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")             //选择主数据源
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary  //只有一个主数据源,如果不是主数据源,这个标签要去掉!!!
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")            //选择主数据源
    public DataSource primaryDataSource() {
        return primaryDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Autowired
    private JpaProperties jpaProperties;

    /**
     * HibernateSettings为配置列名生成策略,在yml中已经配置
     * spring boot1.5.* 中此方法有区别。返回的是DataSource,详见spring-boot-examples下的spring-boot-multi-datasource示例
     *
     * @return
     */
    public Map<String, Object> getVerdorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * entity管理工厂,指定entity位置
     *
     * @param builder
     * @return
     */
    @Primary   //只有一个主数据源,如果不是主数据源,这个标签要去掉!!!
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .packages("com.***.***.dao.**.entity")          //指定entity路径,字段映射
                .properties(getVerdorProperties())
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Bean
    @Primary  //只有一个主数据源,如果不是主数据源,这个标签要去掉!!!
    public EntityManager entityManagerPrimary(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }

    @Primary  //只有一个主数据源,如果不是主数据源,这个标签要去掉!!!
    @Bean
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}

另一个参考:

package com.***.***.service.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryResume",
        transactionManagerRef = "transactionManagerResume",
        basePackages = {"com.***.***.dao.resume.repository"})          //指定dao路径,数据库操作
public class ResumeDBConf {

    /**
     * 构建简历数据源
     *
     * @return
     */
    @Bean
    @Qualifier("resumeDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.resume")             //选择主数据源
    public DataSourceProperties resumeDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Qualifier("resumeDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.resume")            //选择主数据源
    public DataSource resumeDataSource() {
        return resumeDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Autowired
    @Qualifier("resumeDataSource")
    private DataSource resumeDataSource;

    @Autowired
    private JpaProperties jpaProperties;

    /**
     * HibernateSettings为配置列名生成策略,在yml中已经配置
     * spring boot1.5.* 中此方法有区别。返回的是DataSource,详见spring-boot-examples下的spring-boot-multi-datasource示例
     *
     * @return
     */
    public Map<String, Object> getVerdorProperties() {
        return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    /**
     * entity管理工厂,指定entity位置
     *
     * @param builder
     * @return
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryResume(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(resumeDataSource)
                .packages("com.***.***.dao.resume.entity")          //指定entity路径,字段映射
                .properties(getVerdorProperties())
                .persistenceUnit("resumePersistenceUnit")
                .build();
    }

    @Bean
    public EntityManager entityManagerResume(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryResume(builder).getObject().createEntityManager();
    }

    @Bean
    public PlatformTransactionManager transactionManagerResume(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryResume(builder).getObject());
    }
}

 

以上即可,但是自己新增第二个数据源时遇到一个问题

因为第二个数据源的表名为大写,而hibernate做映射只识别小写,报错:

 

解决方式为:

1.配置文件增加如下

//如果配置为这个,可以识别表,但是不能将createTime-->create_time
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

2.新增类

代码:

package com.huayong.bi.service.dao;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;

//如果需要映射字段,需要添加实现类
public class MySQLUpperCaseStrategy extends SpringPhysicalNamingStrategy {
    private static final long serialVersionUID = 1383021413247872469L;

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        //将小写表名转换为大写表名
        return Identifier.toIdentifier(name.getText().toUpperCase(),name.isQuoted());
    }
}

参考:https://www.jianshu.com/p/a2a7c1c26fb3

注意:可以新增多个数据源

web服务与service服务一样,都需要配置!

 

 

----------

遇到问题:

报错如下

 

 

 

找到问题,多个空格:

 

发布了284 篇原创文章 · 获赞 150 · 访问量 65万+
展开阅读全文

spring整合hiberante如何取得设置的hibernateProperties

07-15

Spring2.5 整合 Hibernate3 时,我想在dao中取得配置文件中设置的hibernateProperties,找了很久都没找到方法. 配置如下: [code="java"] <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="lobHandler" ref="lobHandler" /> <property name="mappingDirectoryLocations"><!-- mappingResources --> <list> <value>WEB-INF/classes/com/etone/solid/po/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SybaseDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">0</prop> <prop key="use_streams_for_binary">true</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </prop> <prop key="hibernate.query.factory_class"> org.hibernate.hql.classic.ClassicQueryTranslatorFactory </prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.connection.release_mode"> auto </prop> <!-- prop key="hibernate.hbm2ddl.auto">create-drop</prop--> <!-- prop key="hibernate.cglib.use_reflection_optimizer">true</prop> <prop key="hibernate.proxool.xml">/WEB-INF/proxool.xml</prop> <prop key="hibernate.proxool.pool_alias">spring</prop--> </props> </property> <property name="eventListeners"> <map> <entry key="merge"> <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" /> </entry> </map> </property> </bean>[/code] 谢谢哪位帮忙回答一下 [b]问题补充:[/b] 我拿到的sessionFactory是org.hibernate.SessionFactory, 我看了下,org.springframework.orm.hibernate3.LocalSessionFactoryBean才有getHibernateProperties方法. 我怎么才能得到org.springframework.orm.hibernate3.LocalSessionFactoryBean实例呢? [b]问题补充:[/b] to lovewhzlq: [i]得到工厂本身的话,就注入"&sessionFactory"[/i] 我这样写: [code="java"] <bean id="daoSupport" class="com.etone.common.dao.impl.DaoSupportHibernate3Impl"> <property name="sessionFactory"> <ref local="&sessionFactory" /> </property> </bean>[/code] 报错: [code="java"] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 207 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The reference to entity "sessionFactory" must end with the ';' delimiter. [/code] [color=red]请问这个注入怎么写?[/color] ---------------------------- to xuzhfa123: [i]Hibernate具体版本号?再查看Spring2.5支持你那个版本不[/i] 整合成功,使用的版本是spring2.5.6 + hibernate3.3.2 + struts 2.1.6 ----------------------------- to rain2005: 强制转化应该不行的吧?两个Bena不同. 由于在Dao中使用,我不想通过getBean("sessionFactory")形式来取. [b]问题补充:[/b] to lovewhzlq: 我这要写了 [code="java"] <bean id="daoSupport" class="com.etone.common.dao.impl.DaoSupportHibernate3Impl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> <property name="localSessionFactoryBean"> <ref local="&sessionFactory" /> </property> </bean>[/code] 报另一个错误: [code="java"] org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 210 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: '&sessionFactory' is not a valid value for 'NCName'. [/code] [b]问题补充:[/b] 我刚才试了一下,找到了一个可以的方法,但感觉不太好.. 如下: 在DaoSupportHibernate3Impl中定义增加: [quote] private SessionFactoryImpl sessionFactoryImpl; public SessionFactoryImpl getSessionFactoryImpl() { return sessionFactoryImpl; } public void setSessionFactoryImpl(SessionFactoryImpl sessionFactoryImpl) { this.sessionFactoryImpl = sessionFactoryImpl; }[/quote] XML配置: [code="java"] <bean id="daoSupport" class="com.etone.common.dao.impl.DaoSupportHibernate3Impl"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> <property name="sessionFactoryImpl"> <ref local="sessionFactory" /> </property> </bean> [/code] 在DaoSupportHibernate3Impl中这样使用: [code="java"]sessionFactoryImpl.getSettings().isScrollableResultSetsEnabled();[/code] 不知道有无更好的方法? [b]问题补充:[/b] 谢谢各位的热心解答! 我想应该就是各位所说的方法了: 1,注入sessionFactory到org.hibernate.impl.SessionFactoryImpl实例变量 2.通过ApplicationContext来得到. 暂时我是用第1种方法. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览