当前位置: 移动技术网 > IT编程>开发语言>Java > Spring jpa和mybatis整合遇到的问题解析

Spring jpa和mybatis整合遇到的问题解析

2019年07月22日  | 移动技术网IT编程  | 我要评论

街拍肉丝,仙醉园,万博亚洲安全吗

前一阵子接手了一个使用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

配置如下

<?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>

以上所述是小编给大家介绍的spring jpa和mybatis整合遇到的问题解析,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网