얼마 전에 SpringBoot 및 Spring-Data-JPA를 사용하여 개발 된 프로젝트를 인수했습니다. 나중에 JPA가 Mybatis보다 사용하기가 너무 어렵다고 말하면서 새로운 친구가 추가되었으며 여러 테이블이있는 쿼리를 작성하기가 어렵 기 때문에 MyBatis 지원을 추가했습니다.
처음에
@configuration@enablejparepositories ( "com.xxx.xxx.repository") 클래스 jpaconfig
이 메소드를 사용하여 JPA를 구성 할 때 한 가지 문제가 발생할 수 있지만 저장할 수는 없으므로 구성 파일로 수정됩니다.
다음은 구성 파일을 직접 소개합니다.
1. 스프링 구성
<? xml 버전 = "1.0"encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://www.w3.org/2001/xmlschem a-instance "xmlns : context ="http://www.springframework.org/schema/context "xmlns : tx ="http : // www. springframework.org/schema/tx"x"x"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:task="http://www.spramframework.org/schema/task"x"xsi:schemalocation="http://sww.oror g/schema/beanshttp : //www.springframework.org/schema/beans/spring-beans.xsdhttp : //www.springframework.or g/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.xsdhtt p : //www.springframework.org/schema/aophttp : //www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task/task http://www.springframework.org/schema/task/spring-task.xsd"><--- 주석 파일 스캔-> <context : context : context : component-scan base-package = "com.xxx"/> <aud : annotation-driven/<ban id = "SpringContexTholder"lazy-init = "false"> </bean> <bean id = "configproperties"class = "org.springframework.beans.config.config.propertiesfactorybean"> <속성 이름 = "locations"> <slest> <value> classpath*: jdbc.properties </value> <talue> classpath*: app.properties </value> </list> </bean> <bean id = "PropertyConfigurer"class = "org.spramework.beans.beans.config.creferencesplaceConfigerer"> 이름 = "SystemProperTiesModeName"value = "System_Properties_Mode_Override"/> <속성 이름 = "FileEncoding"value = "utf-8"/> <속성 이름 = "properties"ref = "configProperties"/</bean> <!-dataSource configuration-> bean id = "datasource" init-method = "init"destrove-method = "close"> <!-기본 속성 URL, 사용자, 암호-> <!-<속성 이름 = "driver className"value = "$ {ds.driverClassName}"/>-<속성 이름 = "url"value = "$ {ds.url}"/>> <value = "username" "username" "username". 이름 = "password"value = "$ {ds.Password}"/> <!-초기화 크기, 최소 및 최대 및 최대-> <속성 이름 = "이니셜 크기"값 = "$ {ds.initialSize}"/> <속성 이름 = "minidle"value = "$ {ds.minidle}"/> "property name ="maxactive " value = "$ {ds.MaxActive}"/> <!-연결 시간이 초과 될 때까지 기다릴 시간을 구성합니다-> <속성 이름 = "maxWait"value = "$ {ds.maxwait}"/> <!-밀리 초에서 한 번 확인하는 데 필요한 유휴 연결을 감지하는 시간을 확인합니다. value = "$ {ds.timebetweenevictionRunsmillis}"/> <!-밀리 초에서 수영장에서 수영장에서 생존하기위한 최소 시간을 구성-> <속성 이름 = "minevictableDletimemillis"value = "$ {ds.minevictableDletimemillis}"/> <Property Name = "PADITIONQUERY" value = "$ {ds.ValidationQuery}"/> <속성 이름 = "testwhileIdle"value = "$ {ds.testStIrIdle}"/> <property name = "testOnborring"value = "$ {ds.TestOnborring}"/> <property name = "testonreturn" "value ="$ {ds.testonbress} "! 각 연결에 대한 pscache-> <속성 이름 = "poolpreparedStatements"value = "$ {ds.poolpreparedStatements}"/> <속성 이름 = "maxpoolpreparedStatementperConnectionsize"value = "$ {ds.maxpoolpreparedStateTerConnectionsize}"/> <! <! 이름 = "필터"value = "$ {ds.filters}"/> <!-포기 된 연결을 닫을 때 출력 오류 로그-> <property name = "logabandoned"value = "$ {ds.logabandoned}"/> </bean> <! id = "sqlsessionFactory"> <속성 이름 = "dataSource"ref = "dataSource"/> <property name = "configlocation"value = "classpath : mybatis-config.xml"/> <속성 이름 = "intealiasespackage"value = "com.xxx.culture.domain"/> <! 이름 = "mapperlocations"value = "classpath : mapper/**/*. xml"/> </bean> <!-DAO 인터페이스가 위치한 패키지 이름, Spring은 자동으로 클래스를 자동으로 찾을 것입니다-> <eApackage "value ="com.xxx.xxx.dao "/> <!-트랜잭션 관리자) tx-> <bean id = "transactionManager"class = "org.springframework.jdbc.datasource.datasourcetransactionManager"> <property name = "dataSource"ref = "dataSource"/> </bean> <! </bean>-트랜잭션 관리 알림 --> id = "txadvice" Transaction-Manager = "TransactionManager"> <TX : attributes> <!-예외가있는 한 삽입, 업데이트, 삭제, 롤백으로 시작하는 트랜잭션 관리 방법-> <tx : "삽입*"propagation = "필수"Rollback-for = "java.lang.throwable"/> <tx = "proplagation =" "" "" "" "" "" "롤백-롤백이 필요합니다. rollback-for = "java.lang.throwable"/> <tx : method name = "delete*"propagation = "필수"rollback-for = "java.lang.throwable"/> <tx : "method name ="remove*"propagation ="필수 "rollback-for ="java.lang.throwable "/> <tx : method = save*"proforation ""profagation " rollback-for = "java.lang.throwable"/> <tx : method name = "add*"propagation = "필수"/> <tx : 메소드 이름 = "flush*"propagation = "필수"/> <!-데이터베이스 액세스 성능을 향상시키기 위해 선택, 카운트, get, 찾기, 선택*""///> 이름 = "count*"read-dony = "true"/> <tx : 메소드 이름 = "get*"read-rony = "true"/> <tx : 메소드 이름 = "get*"read-road = "true"/> <tx : method name = "find*"read-rony = "true"/> <tx : method name = "true ="true = ""tranly management를 사용하십시오 ": method management를 사용합니다. 이름 = "*"/> </tx : attributes> </tx : 조언> <!-트랜잭션 AOP 구성 com.xxx.smp.service ..*empr.*(..))-> <aop : config> <aop : pointcut id = "servicemethods"expression = "execution (*com.xxx.xx.service 조언 -ref = "txadvice"pointcut-ref = "servicemethods"/> </aop : config> <!-cglib 프록시를 사용하도록 spring을 구성-> <aop : sagle-autoproxy proxy-target class = "true"/> <!-트랜잭션 님이 tx :> transaction-manager = "transaction-manager" Resource = "ApplicationContext-jpa.xml"/> </beans>2. JPA 구성
처음에는 문제가 발생했습니다.
구성은 다음과 같습니다
<? xml 버전 = "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.spramewo rk.org/schema/tx"Xmlns:JPA="http://www.springframework.org/schema/data/jpa"xsi:schemalocation="http://www.spramework.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/jpa/spron-jpa-.data/data/jpa id = "entityManagerFactory"class = "org.springframework.orm.jpa.localContainerentityManagerFactoryBean"> <!-데이터 소스를 지정-> <속성 이름 = "dataSource"ref = "datasource"/<!-속성을 지정하십시오. 이름 = "packagestoscan"> <list> <value> com.xxx.xxx.jpadomain </value> </list> </property> <!- JPA 속성을 지정합니다. 최대 절전 모드에 SQL, 방언 등을 표시할지 여부-> <property name = "jpavendoradapter"> <ean> <!-ddl 파일 생성 여부-> <property name = "generateddl"value = "true"/> <! 이름 = "databasePlatform"value = "org.hibernate.dialect.mysqldialect"/> <!-mysql, 혼자서 선택-> <property name = "database"value = "mysql"/> </bean> <property name = "jpaproperties"> <props key = "hibernate.ejb.naming_strategy"> org.hibernate.cfg.improvednamingstrategy </prop> <prop key = "hibernate.hbm2ddl.auto"> update </prop> <prop key = "hibernate.enable_llazy_no_trans"> true </props> </props> </props> </props> 데이터 JPA 구성-> <!-스캔을 활성화하고 자동으로 프록시 공장 클래스 클래스 = "com.monk.base.jpa.peakjparepository factory"정의 된 Bean 주석 메소드 자체를 구성하는 기능을 구성합니다. 쓰기없이 모든 패키지를 직접 주석을 달 수 있습니다-> <jpa : repositories base-package = "com.xxx.xxx.repository"transaction-manager-ref = "transaction-manager"entity-manager-factory-ref = "entityManagerActory"/> <! jpa transaction-configuration-configuration-> <bean chactingManager "> 이름 = "EntityManagerFactory"ref = "EntityManagerFactory"/> </bean> <!-주석 트랜잭션을 켜십시오-> <TX : 주석 중심 트랜잭션-매너 = "TransactionManager"proxy-target class = "true"/</beans>
위의 것은 편집자가 소개 한 Spring JPA 및 Mybatis의 통합에서 발생하는 문제에 대한 분석입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!