您现在的位置是:首页 >技术杂谈 >springmvc jpa 多数据源网站首页技术杂谈

springmvc jpa 多数据源

正怒月神 2023-06-22 00:00:03
简介springmvc jpa 多数据源

本次使用Mysql 和 sqlServer

一 POM

版本大家自己换一下

<!--  JPA  -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
                <version>1.13.13.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-jpa</artifactId>
                <version>1.13.13.RELEASE</version>
            </dependency>
<!--  JPA  -->

二 application.xml

我将jdbc和jpa单独放在了另一个spring-business.xml中

1 applicationContext.xml

引用了spring-business.xml

    <import resource="spring-business.xml"/>

2 spring-business.xml

    <!--mysql 配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />  
        <property name="jdbcUrl" value="jdbc:mysql://192.168.1.1:3306/test1" />     
        <property name="user" value="root" />   
        <property name="password" value="root" />   
        <property name="maxPoolSize" value="20" />      
        <property name="minPoolSize" value="3" />       
        <property name="initialPoolSize" value="3" />      
        <property name="maxIdleTime" value="20" />  
    </bean>

<!--  hibernate  -->
    <!--配置session工厂-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.test1.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">none</prop> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
                <prop key="hibernate.show_sql">false</prop>     
                <prop key="hibernate.format_sql">true</prop>     
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>  
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
            </props>
        </property>
    </bean>


<!--  JPA mysql  -->
    <bean id="entityManagerFactory" name="jpaEntityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
        <!-- mysql的实体类包 -->
        <property name="packagesToScan" value="com.test1.model" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop><!-- validate/update/create -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            </props>
        </property>
    </bean>

    <!-- 设置JPA实现厂商的特定属性 -->
    <bean id="hibernateJpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <!-- 注意:MySQLDialect -->
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
    </bean>

    <!-- Jpa 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <!-- Spring Data Jpa配置 -->
    <jpa:repositories 
        base-package="com.test1.dao"  <!-- mysql dao包 -->
        transaction-manager-ref="transactionManager" 
        entity-manager-factory-ref="entityManagerFactory"/>

    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!--  JPA mysql  -->


<!-- JPA sqlserver -->
    <bean id="sqlserverDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="jdbcUrl" value="jdbc:sqlserver://192.168.1.1;DatabaseName=test2" /> 
        <property name="user" value="root" />   
        <property name="password" value="root" />   
        <property name="maxPoolSize" value="20" />      
        <property name="minPoolSize" value="3" />       
        <property name="initialPoolSize" value="3" />      
        <property name="maxIdleTime" value="20" /> 
    </bean>

    <!-- 整合sqlserverjpa -->
    <bean id="sqlserverEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="sqlserverDataSource"></property>
        <!-- sqlserver model包 -->
        <property name="packagesToScan" value="com.test2.model"></property>
        <property name="persistenceUnitName" value="sqlserverdb"></property>
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter2" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">18</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
    </bean>
    <!-- 设置JPA实现厂商的特定属性 -->
    <bean id="hibernateJpaVendorAdapter2"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <!-- 注意:SQLServerDialect-->
        <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect"/>
    </bean>
    <bean id="sqlservertransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="sqlserverEntityManagerFactory" />
        <qualifier value="sqlserverEM"/>
    </bean>
    <jpa:repositories 
        <!-- sqlserver dao包 -->
        base-package="com.test2.dao"  
        transaction-manager-ref="sqlservertransactionManager" 
        entity-manager-factory-ref="sqlserverEntityManagerFactory"/>
    <tx:annotation-driven transaction-manager="sqlservertransactionManager" proxy-target-class="false"/>
<!-- JPA sqlserver -->
    
</beans>

如果运行报错:The server selected protocol version TLS10 is not accepted by client preferences

找到jdk下的java.security(idea的在config下,jdk的在jdk目录中的jre/lib/security下)文件,然后搜索jdk.tls.disabledAlgorithms

改为:jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA,
    DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL,
    include jdk.disabled.namedCurves

三 对应的Repository和model创建完

(根据上面配置,创建在指定的dao和model包中!)

直接就可以调用了

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。