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

网友投稿 350 2023-07-03


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

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

开始的时候

@Configuration

@EnableJpaRepositories("com.xxx.xxx.repository")

class JpaConfig

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

下面直接上配置文件:

1、spring的配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://wwwDPYXIZ.springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/task http://springframework.org/schema/task/spring-task.xsd">

lazy-init="false">

class="org.springframework.beans.factory.config.PropertiesFactoryBean">

classpath*:jdbc.properties

classpath*:app.properties

class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

destroy-method="close">

value="${ds.maxPoolPreparedStatementPerConnectionSize}"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

expression="execution(* com.xxx.xxx.service..*(..))"/>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://wwwDPYXIZ.springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/task http://springframework.org/schema/task/spring-task.xsd">

lazy-init="false">

lazy-init="false">

class="org.springframework.beans.factory.config.PropertiesFactoryBean">

classpath*:jdbc.properties

classpath*:app.properties

class="org.springframework.beans.factory.config.PropertiesFactoryBean">

classpath*:jdbc.properties

classpath*:app.properties

class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">

destroy-method="close">

value="${ds.maxPoolPreparedStatementPerConnectionSize}"/>

destroy-method="close">

value="${ds.maxPoolPreparedStatementPerConnectionSize}"/>

value="${ds.maxPoolPreparedStatementPerConnectionSize}"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

expression="execution(* com.xxx.xxx.service..*(..))"/>

expression="execution(* com.xxx.xxx.service..*(..))"/>

2、jpa的配置

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

配置如下

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:jpa="http://springframework.org/schema/data/jpa"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/data/jpa http://springframework.org/schema/data/jpa/spring-jpa.xsd" >

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

com.xxx.xxx.jpadomain

org.hibernate.cfg.ImprovedNamingStrategy

update

true

transaction-manager-ref="transactionManager"

entity-manager-factory-ref="entityManagerFactory"/>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:jpa="http://springframework.org/schema/data/jpa"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/data/jpa http://springframework.org/schema/data/jpa/spring-jpa.xsd" >

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

com.xxx.xxx.jpadomain

class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

com.xxx.xxx.jpadomain

org.hibernate.cfg.ImprovedNamingStrategy

update

true

transaction-manager-ref="transactionManager"

entity-manager-factory-ref="entityManagerFactory"/>

transaction-manager-ref="transactionManager"

entity-manager-factory-ref="entityManagerFactory"/>

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java Calendar类的时间操作
下一篇:java书店系统毕业设计 用户模块(3)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~