sping4 + jpa 多数据源配置, 多种实现

来源:互联网 发布:美容院会计软件 编辑:程序博客网 时间:2024/06/07 17:22
  • persistence-mysql.xml   及   orm-mysql.xml 的配置请参考我另外博文中 xml配置
         搞外包,他们公司用到jpa,可是没有一个人会. 真的是坑爹的一笔. 好歹让我弄出来了.  下面是配置代码
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"xmlns:repository="http://www.springframework.org/schema/data/repository"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><!--没有配置在IOC里的类进行set方法上面  Autowired的注入  --><context:spring-configured />
  
<bean id="dataSource" class="cn.com.taiji.util.DynamicDataSource"><property name="targetDataSources"><map key-type="java.lang.String"><entry key="dataSource1" value-ref="dataSource1" /><entry key="dataSource2" value-ref="dataSource2" /></map></property><property name="defaultTargetDataSource" ref="dataSource1" /></bean><bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost/yw_taiji?useUnicode=true&characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="" /><property name="maxActive" value="20" /><property name="initialSize" value="1" /><property name="maxWait" value="60000" /><property name="minIdle" value="1" /><property name="timeBetweenEvictionRunsMillis" value="60000" /><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" /></bean><bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost/taiji?useUnicode=true&characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="" /><property name="maxActive" value="20" /><property name="initialSize" value="1" /><property name="maxWait" value="60000" /><property name="minIdle" value="1" /><property name="timeBetweenEvictionRunsMillis" value="60000" /><property name="minEvictableIdleTimeMillis" value="300000" /><property name="validationQuery" value="SELECT 'x'" /><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="poolPreparedStatements" value="true" /><property name="maxPoolPreparedStatementPerConnectionSize"value="20" /></bean>

<!-- JDBC配置 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="dataSource" /></property></bean><!-- ***************************************数据源1*****************************************************--><!--允许使用注解方式配置事务 -->  <tx:annotation-driven transaction-manager="transactionManager" />    <!--service端使用inject方式查询扫描 --> <!-- 数据源1的实体放在db1包路径下 --><jpa:repositories base-package="cn.com.king.repository.db1"  entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="transactionManager"></jpa:repositories> <!-- JPA 事务管理 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="dataSource" ref="dataSource1" /> <property name="entityManagerFactory" ref="entityManagerFactory" /><qualifier value="dataSource1EM"/> </bean><!-- JPA 实体管理工厂 --> <bean id="entityManagerFactory"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource1" /><!-- 对应存放db2的配置   及  表实体 --><property name="persistenceXmlLocation" value="classpath:META-INF/persistence-mysql.xml" /><!-- 必须  --><property name="persistenceUnitName" value="db1"></property></bean><!-- *******************************************数据源2****************************************************** --><tx:annotation-driven transaction-manager="transactionManager2" /> <!-- 数据源2的实体放在db2包路径下 --><jpa:repositories base-package="cn.com.king.repository.db2"  entity-manager-factory-ref="entityManagerFactory2"transaction-manager-ref="transactionManager2"/><bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="dataSource" ref="dataSource2" /> <property name="entityManagerFactory" ref="entityManagerFactory2" /><qualifier value="dataSource2EM"/> </bean><bean id="entityManagerFactory2"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource2" /><property name="persistenceXmlLocation" value="classpath:META-INF/persistence-mysql2.xml" /> <!-- 必须  --><property name="persistenceUnitName" value="db2"></property></bean>

下面事例下多种写法

 第一种  (unitName="db1")  对应上面配置中 <property name="persistenceUnitName" value="db2"></property>  而他对应与            persistence-mysql2.xml中的 <persistence-unit name="db1" transaction-type="RESOURCE_LOCAL">         当配置了多数据源的情况下  这个不能省略,  单数据源可以不写@PersistenceContext(unitName="db1")  private EntityManager em;                                                                                                                  很多情况下 这种写法只能使用第一个数据源, 那是由于你                    <jpa:repositories base-package="cn.com.king.repository.db2"  扫描包的时候都扫描到了,并且mysql2.xml  里面和 mysql.xml 都是一样的实体, 这里注意要分别对应上就行,  我开始这里就没有mysql2.xml全部用的mysql.xml而且都写这里面,总报找不到表的错误@InjectMonitorRepository monitorRepository;
  上面2个获取到dao层的方法,  至于使用可以 参考 这个里面的讲的很好
   jpa的查询方式   
  这里有个方法,  em.createNativeQuery(sqlString);  可以直接写sql查询,   这样在多表查询的情况下非常适用, 纯sql写法,  但是在分页上还是用的jpa, 自动封装 limit或者 rowNum
     这个 取决于 persistence-mysql2.xml 中 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> 你使用的数据库方言自动转换

  jpa查询的时候别忘了加注解
@Transactionalpublic interface LogZJTaskInfoRepository  extends JpaRepository<LogZJTaskInfo, String>, JpaSpecificationExecutor<LogZJTaskInfo>{}
 这里如果没有注解 在modify操作的时候  会有空指针异常
 相关@modify注解同理,  这个问题刚使用的时候困扰了好久,哎,公司用这个框架尽然没有一个人写个crud的例子出来就随便让用, 遇到问题都只能自己百度摸索.
百度的多数据源配置大多都是spring3的  在4这里根本用不了.好了   配置如上,  可以先写个单一数据源的jpa  进行crud后,  然后使用如上配置就能轻松改为多数据源了.
      祝大家好运!!!!!!!!!!!!!!!!!!!!!!




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 江苏高考物理考d怎么办 高二化学学不好怎么办 中考最后一次月考下滑怎么办 物联网卡网速慢怎么办 机械表长时间不带不走了怎么办 高中档案有涂改痕迹怎么办 大学平时成绩为0怎么办 电大英语考试成绩取消了怎么办 网贷评分不足要怎么办 学业水平广东1c怎么办 绣花机速度太慢怎么办 娃脖子有点烂了怎么办 7月省内流量套餐怎么办 qq手游授权失败怎么办 钉钉不够6人创建怎么办 钉钉 不够6个人怎么办 plsql删错了表怎么办 吊兰长出来的茎怎么办 防水台鞋跟太高怎么办 证件照头部比例过大怎么办 特岗照片传错了怎么办 打印报名表照片不显示怎么办 刚买的床有味道怎么办 雨刷器角度太小怎么办 四个月宝宝闹觉怎么办 怀孕六个月睡不好觉怎么办? 婴儿睡不好觉总是吵闹怎么办 婴儿鼻塞睡不好觉怎么办 玩英雄联盟鼠标变亮白怎么办 练芭蕾脚受伤了怎么办 高三了英语30分怎么办 要上高中了英语不好怎么办 高二了数学不好怎么办 高二函数不好怎么办啊 输乳怎么办腺病有什么妇症状 屁股上坐的发黑怎么办 车屁股撞了个洞怎么办 腰压迫的腿疼怎么办 肩胛提肌固定不住怎么办 胳膊粗后背肉多怎么办 2岁儿童 肋骨边缘外翻怎么办