Spring jpa和mybatis整合遇到的问题解析
前一阵子接手了一个使用SpringBoot和spring-data-jpa开发的项目,后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查询写起来也比较费劲,所以便加入了mybatis的支持
开始的时候
@Configuration @EnableJpaRepositories("com.xxx.xxx.repository") classJpaConfig
使用这种方式去配置的jpa,遇到一个问题,就是能select但是不能save,所以就修改为配置文件的方式:
下面直接上配置文件:
1、spring的配置
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsd"> <!--扫描注解文件--> <context:component-scanbase-package="com.xxx"/> <task:annotation-driven/> <beanid="springContextHolder"class="com.xxx.common.config.SpringContextHolder" lazy-init="false"></bean> <beanid="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <propertyname="locations"> <list> <value>classpath*:jdbc.properties</value> <value>classpath*:app.properties</value> </list> </property> </bean> <beanid="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <propertyname="systemPropertiesModeName"value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <propertyname="fileEncoding"value="UTF-8"/> <propertyname="properties"ref="configProperties"/> </bean> <!--dataSource配置--> <beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"> <!--基本属性url、user、password--> <!--<propertyname="driverClassName"value="${ds.driverClassName}"/>--> <propertyname="url"value="${ds.url}"/> <propertyname="username"value="${ds.username}"/> <propertyname="password"value="${ds.password}"/> <!--配置初始化大小、最小、最大--> <propertyname="initialSize"value="${ds.initialSize}"/> <propertyname="minIdle"value="${ds.minIdle}"/> <propertyname="maxActive"value="${ds.maxActive}"/> <!--配置获取连接等待超时的时间--> <propertyname="maxWait"value="${ds.maxWait}"/> <!--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒--> <propertyname="timeBetweenEvictionRunsMillis"value="${ds.timeBetweenEvictionRunsMillis}"/> <!--配置一个连接在池中最小生存的时间,单位是毫秒--> <propertyname="minEvictableIdleTimeMillis"value="${ds.minEvictableIdleTimeMillis}"/> <propertyname="validationQuery"value="${ds.validationQuery}"/> <propertyname="testWhileIdle"value="${ds.testWhileIdle}"/> <propertyname="testOnBorrow"value="${ds.testOnBorrow}"/> <propertyname="testOnReturn"value="${ds.testOnReturn}"/> <!--打开PSCache,并且指定每个连接上PSCache的大小--> <propertyname="poolPreparedStatements"value="${ds.poolPreparedStatements}"/> <propertyname="maxPoolPreparedStatementPerConnectionSize" value="${ds.maxPoolPreparedStatementPerConnectionSize}"/> <!--配置监控统计拦截的filters--> <propertyname="filters"value="${ds.filters}"/> <!--关闭abanded连接时输出错误日志--> <propertyname="logAbandoned"value="${ds.logAbandoned}"/> </bean> <!--spring和MyBatis完美整合,不需要mybatis的配置映射文件--> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <propertyname="configLocation"value="classpath:mybatis-config.xml"/> <propertyname="typeAliasesPackage"value="com.xxx.culture.domain"/> <!--自动扫描mapping.xml文件--> <propertyname="mapperLocations"value="classpath:mapper/**/*.xml"/> </bean> <!--DAO接口所在包名,Spring会自动查找其下的类--> <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"> <propertyname="basePackage"value="com.xxx.xxx.dao"/> </bean> <!--(事务管理)transactionmanager,useJtaTransactionManagerforglobaltx--> <beanid="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <propertyname="dataSource"ref="dataSource"/> </bean> <!--事务管理通知--> <tx:adviceid="txAdvice"transaction-manager="transactionManager"> <tx:attributes> <!--对insert,update,delete开头的方法进行事务管理,只要有异常就回滚--> <tx:methodname="insert*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/> <tx:methodname="update*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/> <tx:methodname="delete*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/> <tx:methodname="remove*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/> <tx:methodname="save*"propagation="REQUIRED"rollback-for="java.lang.Throwable"/> <tx:methodname="add*"propagation="REQUIRED"/> <tx:methodname="flush*"propagation="REQUIRED"/> <!--select,count,get,find开头的方法,开启只读,提高数据库访问性能--> <tx:methodname="select*"read-only="true"/> <tx:methodname="count*"read-only="true"/> <tx:methodname="get*"read-only="true"/> <tx:methodname="find*"read-only="true"/> <tx:methodname="search*"read-only="true"/> <!--对其他方法使用默认的事务管理--> <tx:methodname="*"/> </tx:attributes> </tx:advice> <!--事务aop配置com.xxx.smp.service..*Impl.*(..))--> <aop:config> <aop:pointcutid="serviceMethods" expression="execution(*com.xxx.xxx.service..*(..))"/> <aop:advisoradvice-ref="txAdvice"pointcut-ref="serviceMethods"/> </aop:config> <!--配置使Spring采用CGLIB代理--> <aop:aspectj-autoproxyproxy-target-class="true"/> <!--事务注解支持--> <tx:annotation-driventransaction-manager="transactionManager"/> <importresource="applicationContext-jpa.xml"/> </beans>
2、jpa的配置
初始时遇到一个问题:jpaorg.hibernate.LazyInitializationException:couldnotinitializeproxy-noSession
配置如下
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <beanid="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!--指定数据源--> <propertyname="dataSource"ref="dataSource"/> <!--指定Entity实体类包路径--> <propertyname="packagesToScan"> <list> <value>com.xxx.xxx.jpadomain</value> </list> </property> <!--指定JPA属性;如Hibernate中指定是否显示SQL的是否显示、方言等--> <propertyname="jpaVendorAdapter"> <beanclass="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <!--是否生成ddl文件--> <propertyname="generateDdl"value="true"/> <!--是否展示sql--> <propertyname="showSql"value="false"/> <!--必要的数据库库使用的详细信息--> <propertyname="databasePlatform"value="org.hibernate.dialect.MySQLDialect"/> <!--mysql,自行选择--> <propertyname="database"value="MYSQL"/> </bean> </property> <propertyname="jpaProperties"> <props> <propkey="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy </prop> <propkey="hibernate.hbm2ddl.auto">update</prop> <propkey="hibernate.enable_lazy_load_no_trans">true</prop> </props> </property> </bean> <!--SpringDataJpa配置--> <!--配置启用扫描并自动创建代理的功能factory-class="com.monk.base.jpa.PeakJpaRepositoryFactory"自己定义的bean注解方式,可以不写,直接注解所有包下的--> <jpa:repositoriesbase-package="com.xxx.xxx.repository" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/> <!--Jpa事务配置--> <beanid="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager"> <propertyname="entityManagerFactory"ref="entityManagerFactory"/> </bean> <!--开启注解事务--> <tx:annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/> </beans>
以上所述是小编给大家介绍的Springjpa和mybatis整合遇到的问题解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!