Spring jpa和mybatis整合遇到的问题解析_Java教程-查字典教程网
Spring jpa和mybatis整合遇到的问题解析
Spring jpa和mybatis整合遇到的问题解析
发布时间:2016-12-28 来源:查字典编辑
摘要:前一阵子接手了一个使用SpringBoot和spring-data-jpa开发的项目,后期新加入一个小伙伴,表示jpa相比mybatis太难...

前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目,后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查询写起来也比较费劲,所以便加入了mybatis的支持

开始的时候

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

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

下面直接上配置文件:

1、spring的配置

<"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" lazy-init="false"></bean> <bean id="configProperties" > <property name="locations"> <list> <value>classpath*:jdbc.properties</value> <value>classpath*:app.properties</value> </list> </property> </bean> <bean id="propertyConfigurer" > <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="fileEncoding" value="UTF-8"/> <property name="properties" ref="configProperties"/> </bean> <> <bean id="dataSource" init-method="init" destroy-method="close"> <> <> <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}"/> <> <property name="poolPreparedStatements" value="${ds.poolPreparedStatements}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${ds.maxPoolPreparedStatementPerConnectionSize}"/> <> <property name="filters" value="${ds.filters}"/> <> <property name="logAbandoned" value="${ds.logAbandoned}"/> </bean> <> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="typeAliasesPackage" value="com.xxx.culture.domain"/> <> <property name="mapperLocations" value="classpath:mapper/**/*.xml"/> </bean> <> <bean> <property name="basePackage" value="com.xxx.xxx.dao"/> </bean> <> <bean id="transactionManager" > <property name="dataSource" ref="dataSource"/> </bean> <> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <> <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"/> <> <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:config> <aop:pointcut id="serviceMethods" expression="execution(* com.xxx.xxx.service..*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/> </aop:config> <> <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

配置如下

<"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" > <> <property name="dataSource" ref="dataSource"/> <> <property name="packagesToScan"> <list> <value>com.xxx.xxx.jpadomain</value> </list> </property> <> <property name="jpaVendorAdapter"> <bean> <> <property name="generateDdl" value="true"/> <> <property name="showSql" value="false"/> <> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/> <> <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> <> <> <jpa:repositories base-package="com.xxx.xxx.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> <> <bean id="transactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> </beans>

以上所述是小编给大家介绍的Spring jpa和mybatis整合遇到的问题解析,希望对大家有所帮助,如果大家有任何疑问请给我们留言,小编会及时回复大家的。在此也非常感谢大家对查字典教程网的支持!

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Java学习
    热门Java学习
    编程开发子分类