spring jpa和mybatis整合

前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目

后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查询写起来也比较费劲,所以便加入了mybatis的支持

开始的时候

@Configuration
@EnableJpaRepositories("com.xxx.xxx.repository")
class JpaConfig

  

使用这种方式去配置的jpa,遇到一个问题,就是能select 但是不能save,所以就修改为配置文件的方式:

下面直接上配置文件:

1、spring的配置

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 扫描注解文件 -->
<context:component-scan base-package="com.xxx"/>
<task:annotation-driven/> <bean id="springContextHolder" class="com.xxx.common.config.SpringContextHolder"
lazy-init="false"></bean> <bean id="configProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:jdbc.properties</value>
<value>classpath*:app.properties</value>
</list>
</property>
</bean> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="fileEncoding" value="UTF-8"/>
<property name="properties" ref="configProperties"/>
</bean> <!-- dataSource 配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<!-- 基本属性 url、user、password -->
<!-- <property name="driverClassName" value="${ds.driverClassName}" /> -->
<property name="url" value="${ds.url}"/>
<property name="username" value="${ds.username}"/>
<property name="password" value="${ds.password}"/> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${ds.initialSize}"/>
<property name="minIdle" value="${ds.minIdle}"/>
<property name="maxActive" value="${ds.maxActive}"/> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${ds.maxWait}"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${ds.minEvictableIdleTimeMillis}"/> <property name="validationQuery" value="${ds.validationQuery}"/>
<property name="testWhileIdle" value="${ds.testWhileIdle}"/>
<property name="testOnBorrow" value="${ds.testOnBorrow}"/>
<property name="testOnReturn" value="${ds.testOnReturn}"/> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="${ds.poolPreparedStatements}"/>
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${ds.maxPoolPreparedStatementPerConnectionSize}"/> <!-- 配置监控统计拦截的filters -->
<property name="filters" value="${ds.filters}"/> <!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${ds.logAbandoned}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.xxx.culture.domain"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.xxx.dao"/> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务管理 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 对insert,update,delete 开头的方法进行事务管理,只要有异常就回滚 -->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="flush*" propagation="REQUIRED"/>
<!-- select,count,get,find开头的方法,开启只读,提高数据库访问性能 -->
<tx:method name="select*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<!-- 对其他方法 使用默认的事务管理 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice> <!-- 事务 aop 配置 com.xxx.smp.service..*Impl.*(..)) -->
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* com.xxx.xxx.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<!-- 配置使Spring采用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 事务注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/> <import resource="applicationContext-jpa.xml"/>
</beans>

2、jpa的配置

初始时遇到一个问题:jpa org.hibernate.LazyInitializationException: could not initialize proxy - no Session

在这儿找到了答案http://my.oschina.net/alexgaoyh/blog/313541

配置如下

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" > <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 指定数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 指定Entity实体类包路径 -->
<property name="packagesToScan">
<list>
<value>com.xxx.xxx.jpadomain</value>
</list>
</property>
<!-- 指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等 -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 是否生成ddl文件 -->
<property name="generateDdl" value="true"/>
<!-- 是否展示sql -->
<property name="showSql" value="false"/>
<!-- 必要的数据库库使用的详细信息 -->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<!-- mysql,自行选择 -->
<property name="database" value="MYSQL"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
</bean> <!-- Spring Data Jpa配置 --> <!-- 配置 启用扫描并自动创建代理的功能 factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定义的bean注解方式,可以不写,直接注解所有包下的 -->
<jpa:repositories base-package="com.xxx.xxx.repository"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory"/> <!-- Jpa 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean> <!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

此文章转自:https://www.cnblogs.com/modprobe/p/5675809.html

感谢modprobe的分享

spring jpa和mybatis整合的更多相关文章

  1. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  2. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  3. Mybaits-从零开始-Spring、Spring MVC、MyBatis整合(未万待续)

    Spring.Spring MVC.MyBatis整合(未万待续)

  4. Spring、Spring MVC、MyBatis整合文件配置详解

    原文  http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...

  5. 【转】Spring、Spring MVC、MyBatis整合文件配置详解

    见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...

  6. Spring、Spring MVC、MyBatis整合文件配置详解2

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  7. Spring、Spring MVC、MyBatis 整合文件配置详解

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  8. Spring Cloud部署+Mybatis整合

    一:架构简介 Spring Cloud是微服务思想的体现.每个项目单独部署,我只需要知道你服务的name就能直接调用你,而不关心你的ip和端口的变化.当接口服务不可用的时候,我能感知到你无法用了,就不 ...

  9. Spring SpringMVC和Mybatis整合

    1.引入所要的jar包 2.创建Mybatis的sqlMapConfig.xml配置文件,该文件中可以配置mybaits的相关参数,数据源不在这里配置. <?xml version=" ...

随机推荐

  1. 关于win10 链接安卓设备报错winusb.sys未经签名的解决办法

    很简单,各位,我找了一个签过名的winusb.sys替换原来的文件即可. 操作系统win10 64位专业版(更新到最新版本了) 网盘地址 安装好以后,就没有那个惊叹号咯!

  2. nginx 场景业务汇总 (初)

    本文链接:http://www.cnblogs.com/zhenghongxin/p/8891385.html 在下面的测试中,建议每次修改nginx配置文件后,都用此命令检查一下语法是否正确: [r ...

  3. (3)视觉里程计 Visual Odometry

    首先分析include头文件下的slamBase.h文件 # pragma once // 各种头文件 // C++标准库 #include <fstream> #include < ...

  4. 用node.js写一个简单爬虫,并将数据导出为 excel 文件

    引子 最近折腾node,最开始像无头苍蝇一样到处找资料,然而多数没什么卵用,都在瞎比比.在一阵瞎搞后,我来分享一下初步学习node的三个过程: 1 撸一遍NODE入门,对其有个基本的了解: 2 撸一遍 ...

  5. eclipse 使用prolog编程

    第一步:在电脑上安装swi-prolog 相应环境下载地址http://www.swi-prolog.org/download/stable 第二步: eclipse-help-install new ...

  6. css实现带箭头的流程条

    直接上效果图: <ul class="navs"> <li>1</li> <li>2</li> <li>3& ...

  7. CPM(Cluster Percolation method)派系过滤算法

    一.概念 (1)完全子图/全耦合网络/k-派系:所有节点全部两两相连 图1 这些全耦合网络也成为派系,k-派系表示该全耦合网络的节点数目为k 1)k-派系相邻:两个不同的k-派系共享k-1个节点,认为 ...

  8. 搭建互联网架构学习--005--框架初步拆分ssm单一框架

    经过前边的准备步骤,服务器基本搭建完毕,接下来就开始一步步搭建框架了. 拆分单一结构:拆分的目的是为下一步引入dubbo做准备的. 把下边这个单一maven框架进行拆分 这个就是一个简单的maven项 ...

  9. Postman—测试脚本

    前言 对于Postman中的每个请求,我们都可以使用JavaScript语言来开发测试脚本.这也就好比单元测试.我们先看看Postman的相关界面: 编写测试脚本 Postman测试脚本本质上是在发送 ...

  10. mac 系统安装VM虚拟机打开时报错,提示不是虚拟磁盘的解决方式。

    最近刚买的苹果系统,不太会用,装了个虚拟机vmware fusion,好不容易把需要的软件装好,然后不知道是我操作了哪里,今天再次打开虚拟机的时候打不开了,报错提示找不到磁盘文件(虚拟磁盘-00000 ...