程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring jpa和mybatis整合碰到的成績解析

Spring jpa和mybatis整合碰到的成績解析

編輯:關於JAVA

Spring jpa和mybatis整合碰到的成績解析。本站提示廣大學習愛好者:(Spring jpa和mybatis整合碰到的成績解析)文章只能為提供參考,不一定能成為您想要的結果。以下是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

設置裝備擺設以下

<?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整合碰到的成績解析,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved