Spring jpa和mybatis整合遇到的問(wèn)題解析
前一陣子接手了一個(gè)使用SpringBoot 和spring-data-jpa開(kāi)發(fā)的項(xiàng)目,后期新加入一個(gè)小伙伴,表示jpa相比mybatis太難用,多表聯(lián)合的查詢寫(xiě)起來(lái)也比較費(fèi)勁,所以便加入了mybatis的支持
開(kāi)始的時(shí)候
@Configuration @EnableJpaRepositories("com.xxx.xxx.repository") class JpaConfig
使用這種方式去配置的jpa,遇到一個(gè)問(wèn)題,就是能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}"/> <!-- 配置獲取連接等待超時(shí)的時(shí)間 --> <property name="maxWait" value="${ds.maxWait}"/> <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/> <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> <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}"/> <!-- 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 --> <property name="poolPreparedStatements" value="${ds.poolPreparedStatements}"/> <property name="maxPoolPreparedStatementPerConnectionSize" value="${ds.maxPoolPreparedStatementPerConnectionSize}"/> <!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters --> <property name="filters" value="${ds.filters}"/> <!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 --> <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"/> <!-- 自動(dòng)掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapper/**/*.xml"/> </bean> <!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xxx.xxx.dao"/> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事務(wù)管理 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 對(duì)insert,update,delete 開(kāi)頭的方法進(jìn)行事務(wù)管理,只要有異常就回滾 --> <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開(kāi)頭的方法,開(kāi)啟只讀,提高數(shù)據(jù)庫(kù)訪問(wèn)性能 --> <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"/> <!-- 對(duì)其他方法 使用默認(rèn)的事務(wù)管理 --> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事務(wù) 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"/> <!-- 事務(wù)注解支持 --> <tx:annotation-driven transaction-manager="transactionManager"/> <import resource="applicationContext-jpa.xml"/> </beans>
2、jpa的配置
初始時(shí)遇到一個(gè)問(wèn)題: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"> <!-- 指定數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"/> <!-- 指定Entity實(shí)體類包路徑 --> <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"/> <!-- 必要的數(shù)據(jù)庫(kù)庫(kù)使用的詳細(xì)信息 --> <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配置 --> <!-- 配置 啟用掃描并自動(dòng)創(chuàng)建代理的功能 factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定義的bean注解方式,可以不寫(xiě),直接注解所有包下的 --> <jpa:repositories base-package="com.xxx.xxx.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> <!-- Jpa 事務(wù)配置 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- 開(kāi)啟注解事務(wù) --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> </beans>
以上所述是小編給大家介紹的Spring jpa和mybatis整合遇到的問(wèn)題解析,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解Java中NullPointerException異常的原因和解決辦法
本文主要介紹了詳解Java中NullPointerException異常的原因和解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07java基于JDBC連接Oracle 11g Release2實(shí)例分析
這篇文章主要介紹了java基于JDBC連接Oracle 11g Release2的方法,實(shí)例分析了JDBC連接Oracle 11g Release2容易出現(xiàn)的異常與解決方法,需要的朋友可以參考下2015-06-06使用maven實(shí)現(xiàn)redis與idea的連接問(wèn)題
這篇文章主要介紹了使用maven實(shí)現(xiàn)redis與idea的連接問(wèn)題,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07Java將Date日期類型字段轉(zhuǎn)換成json字符串的方法
這篇文章主要給大家介紹了關(guān)于Java將Date日期類型字段轉(zhuǎn)換成json字符串的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳解Spring依賴注入的三種方式使用及優(yōu)缺點(diǎn)
這篇文章主要介紹了spring依賴注入的三種方式的使用方法,以及優(yōu)缺點(diǎn)的介紹,通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考一下2023-04-04Java如何使用spire進(jìn)行word文檔的替換詳解
創(chuàng)作一份文案經(jīng)常會(huì)高頻率地使用某些詞匯,如地名、人名、人物職位等,若表述有誤,就需要整體撤換,下面這篇文章主要給大家介紹了關(guān)于Java如何使用spire進(jìn)行word文檔的替換的相關(guān)資料,需要的朋友可以參考下2023-01-01IDEA 當(dāng)前在線人數(shù)和歷史訪問(wèn)量的示例代碼
這篇文章主要介紹了IDEA 當(dāng)前在線人數(shù)和歷史訪問(wèn)量的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式
這篇文章主要介紹了Mybatis(ParameterType)傳遞多個(gè)不同類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04