A while ago, I took over a project developed using SpringBoot and spring-data-jpa. Later, a new friend was added, saying that jpa is too difficult to use than mybatis, and it is also difficult to write a query with multiple tables, so I added mybatis support.
At the beginning
@Configuration@EnableJpaRepositories("com.xxx.xxx.repository")class JpaConfigWhen using this method to configure jpa, one problem encountered is that it can select but cannot save, so it is modified to the configuration file:
The following directly introduces the configuration file:
1. Spring configuration
<?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.or g/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/context/spring-context.xsdhttp ://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><!-- Scan the annotation file--><context:component-scan base-package="com.xxx"/><task:annotation-driven/><bean id="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 configuration --><bean id="dataSource" init-method="init"destroy-method="close"><!-- Basic properties 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}"/><!-- Configure initialization size, minimum and maximum --><property name="initialSize" value="${ds.initialSize}"/><property name="minIdle" value="${ds.minIdle}"/><property name="maxActive" value="${ds.maxActive}"/><!-- Configure the time to wait for the connection to be timed out --><property name="maxWait" value="${ds.maxWait}"/><!-- Configure how long it takes to check once, detect the idle connection that needs to be closed, in milliseconds --><property name="timeBetweenEvictionRunsMillis" value="${ds.timeBetweenEvictionRunsMillis}"/><!-- Configure the minimum time for a connection to survive in the pool in milliseconds--><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}"/><!-- Open PSCache and specify the size of PSCache on each connection --><property name="poolPreparedStatements" value="${ds.poolPreparedStatements}"/><property name="maxPoolPreparedStatementPerConnectionSize" value="${ds.maxPoolPreparedStatementPerConnectionSize}"/><!-- Configure filters for monitoring statistics intercepts --><property name="filters" value="${ds.filters}"/><!-- Output error log when closing the abandoned connection--><property name="logAbandoned" value="${ds.logAbandoned}"/></bean><!-- spring and MyBatis are perfectly integrated, and mybatis configuration mapping file does not require mybatis--><bean id="sqlSessionFactory"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis-config.xml"/><property name="typeAliasesPackage" value="com.xxx.culture.domain"/><!-- Automatically scan the mapping.xml file --><property name="mapperLocations" value="classpath:mapper/**/*.xml"/></bean><!-- The package name where the DAO interface is located, Spring will automatically find the class under it --><bean><property name="basePackage" value="com.xxx.xxx.dao"/></bean><!-- (Transaction Management) transaction manager, use JtaTransactionManager for global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- Transaction Management Notification--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- Transaction Management Methods starting with insert, update, delete, roll back as long as there is an exception--><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"/><!-- The method starting with select,count,get, find, enable read-only to improve database access performance--><tx:method name="select*" read-only="true"/><tx:method name="count*" read-only="true"/><tx:method name="get*" read-only="true"/><tx:method name="get*" read-only="true"/><tx:method name="find*" read-only="true"/><tx:method name="search*" read-only="true"/><!-- Use default transaction management for other methods --><tx:method name="*"/></tx:attributes></tx:advice><!-- Transaction aop configuration 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><!-- Configure Spring to use CGLIB proxy--><aop:aspectj-autoproxy proxy-target-class="true"/><!-- Transaction annotation support--><tx:annotation-driven transaction-manager="transactionManager"/><import resource="applicationContext-jpa.xml"/></beans>2. JPA configuration
I encountered a problem at the beginning: jpa org.hibernate.LazyInitializationException: could not initialize proxy - no Session
The configuration is as follows
<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://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"><!-- Specify the data source--><property name="dataSource" ref="dataSource"/><!-- Specify the Entity entity class package path--><property name="packagesToScan"><list><value>com.xxx.xxx.jpadomain</value></list></property><!-- Specify the JPA attribute; such as whether to display SQL, dialect, etc. in Hibernate--><property name="jpaVendorAdapter"><bean><!-- Whether to generate ddl file--><property name="generateDdl" value="true"/><!-- Whether to display sql --><property name="showSql" value="false"/><!-- Details of the necessary database library use--><property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/><!-- mysql, select by yourself-><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 Configuration--><!-- Configure the function of enabling scanning and automatically creating proxy factories-class="com.monk.base.jpa.PeakJpaRepositoryFactory" defined bean annotation method by itself. You can directly annotate all packages without writing --><jpa:repositories base-package="com.xxx.xxx.repository"transaction-manager-ref="transactionManager"entity-manager-factory-ref="entityManagerFactory"/><!-- Jpa Transaction Configuration-configuration-configuration-><bean id="transactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"/></bean><!-- Turn on annotation transaction--><tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/></beans>
The above is an analysis of the problems encountered in the integration of Spring jpa and mybatis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!