เมื่อไม่นานมานี้ฉันเข้าร่วมโครงการที่พัฒนาขึ้นโดยใช้ Springboot และ Spring-Data-JPA ต่อมามีเพื่อนใหม่เพิ่มขึ้นโดยบอกว่า JPA นั้นใช้งานยากเกินกว่า mybatis และมันก็ยากที่จะเขียนแบบสอบถามที่มีหลายตารางดังนั้นฉันจึงเพิ่มการสนับสนุน mybatis
ในตอนแรก
@configuration@enablejparepositories ("com.xxx.xxx.repository") คลาส jpaconfigเมื่อใช้วิธีนี้ในการกำหนดค่า JPA ปัญหาหนึ่งที่พบคือมันสามารถเลือกได้ แต่ไม่สามารถบันทึกได้ดังนั้นจึงได้รับการแก้ไขในไฟล์การกำหนดค่า:
ต่อไปนี้แนะนำไฟล์การกำหนดค่าโดยตรง:
1. การกำหนดค่าสปริง
<? xml version = "1.0" encoding = "utf-8"?> <ถั่ว 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"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.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 http://www.springframework.org/schema/task/spring-task.xsdญ id = "SpringContexTholder" lazy-init = "false"> </ebean> <bean id = "configproperties" class = "org.springframework.beans.factory.config.propertiesfactorybean" NAME = "LOCATIONS"> <SILT> <value> classPath*: jdbc.properties </value> <value> classpath*: app.properties </value> </list> </property> </ebean> name = "SystemProperTiesModeName" value = "system_properties_mode_override"/> <property name = "fileencoding" value = "UTF-8"/> <property name = "คุณสมบัติ" ref = "configProperties"/> </ebean> <! init-method = "init" destroy-method = "close"> <!-URL คุณสมบัติพื้นฐานผู้ใช้รหัสผ่าน-> <!-<name property = "driverclassName" value = "$ {ds.driverClassName}"/>-> <property = "url" value = "$ {ds.url} name = "password" value = "$ {ds.password}"/> <!-กำหนดค่าขนาดการเริ่มต้นขั้นต่ำและสูงสุด-> <ชื่อคุณสมบัติ = "ค่าเริ่มต้น" value = "$ {ds.initialsize}"/> <property name = "minidle" value = "$ {ds.minidle}" กำหนดค่าเวลาที่จะรอให้การเชื่อมต่อหมดเวลา-> <property name = "maxWait" value = "$ {ds.maxwait}"/> <!-กำหนดค่าใช้เวลานานแค่ไหนในการตรวจสอบหนึ่งครั้งตรวจจับการเชื่อมต่อที่ไม่ได้ใช้งาน value = "$ {ds.timebetweenevictionrunsmillis}"/> <!-กำหนดเวลาขั้นต่ำสำหรับการเชื่อมต่อเพื่อความอยู่รอดในสระว่ายน้ำในมิลลิวินาที-> <ชื่อคุณสมบัติ = "minevictableidletimeMillis" value = "$ {ds.minevictableidletimeLis}" value = "$ {ds.validationQuery}"/> <property name = "testharyIdle" value = "$ {ds.testharyidle}"/> <property name = "testonborrow" value = "$ {ds.testonborrow}"/> PSCACHE ในการเชื่อมต่อแต่ละครั้ง-> <property name = "PoolPreparedStatements" value = "$ {ds.poolpreparedStatements}"/> <ชื่อคุณสมบัติ = "MaxPoolPreparedStatementPerConnesctionsize" ค่า FILTERSTERSTITERS value = "$ {ds.filters}"/> <!-บันทึกข้อผิดพลาดเอาท์พุทเมื่อปิดการเชื่อมต่อที่ถูกทอดทิ้ง-> <property name = "logabandoned" value = "$ {ds.logabandoned}"/> </bean> <! name = "dataSource" ref = "dataSource"/> <property name = "การกำหนดค่า" value = "classpath: mybatis-config.xml"/> <property name = "typealiasespackage" value = "com.xxx.culture.domain"/> <! value = "classpath: mapper/**/*. xml"/> </ebean> <!-ชื่อแพ็คเกจที่อินเทอร์เฟซ DAO ตั้งอยู่สปริงจะค้นหาคลาสภายใต้มันโดยอัตโนมัติ-> <bean> <ชื่อคุณสมบัติ = "basePackage" value = "com.xxx.xx.dao"/> -> <bean id = "transactionManager" class = "org.springframework.jdbc.datasource.datasourceTransactionManager"> <ชื่อทรัพย์สิน = "DataSource" ref = "DataSource"/> </bean> <! Transaction-Manager = "TransactionManager"> <tx: คุณลักษณะ> <!-วิธีการจัดการธุรกรรมเริ่มต้นด้วยการแทรกอัปเดตลบกลับย้อนกลับตราบใดที่มีข้อยกเว้น-> <tx: method name = "แทรก*" การเผยแพร่ = "update =" Rollback-for = "java.lang.-hrowable"/> <tx: method name = "delete*" การแพร่กระจาย = "ต้องการ" rollback-for = "java.lang.-hrowable"/> <tx: method name = "ลบ*" การแพร่กระจาย = "จำเป็นต้องใช้" ROLLBACK-FOR = "Java.lang.-hrowable"/> <tx: method name = "เพิ่ม*" การแพร่กระจาย = "ต้องการ"/> <tx: method name = "flush*" การแพร่กระจาย = "ต้องการ"/> <! name = "count*" read-only = "true"/> <tx: method name = "get*" read-only = "true"/> <tx: method name = "get*" read-only = "true"/> <tx: method = "find*" read-only = "true"/> -> <tx: method name = "*"/> </tx: แอตทริบิวต์> </tx: คำแนะนำ> <!-ธุรกรรม AOP การกำหนดค่า com.xxx.smp.service ..*impl.*(.. ))-> <aop: config> <aop: pointcut id = "serviceMethods" com.xxx.xxx.service ..*(.. )) "/> <aop: คำแนะนำที่ปรึกษา ref =" txadvice "pointcut-ref =" servicemethods "/> </aop: config> <! การสนับสนุน-> <tx: transaction-driven transaction-manager = "TransactionManager"/> <import Resource = "ApplicationContext-jpa.xml"/> </ebeans>2. การกำหนดค่า JPA
ฉันพบปัญหาในตอนแรก: JPA org.hibernate.lazyinitializationException: ไม่สามารถเริ่มต้นพร็อกซี - ไม่มีเซสชัน
การกำหนดค่ามีดังนี้
<? xml version = "1.0" encoding = "utf-8"?> <ถั่ว xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" rk.org/schema/tx"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemalocation="http://ww.springframework 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.localcontainerentity ref = "dataSource"/> <!-ระบุพา ธ แพ็คเกจคลาสเอนทิตีเอนทิตี-> <ชื่อคุณสมบัติ = "packageScan"> <list> <dale> com.xxx.xxx.jpadomain </value> </list> </คุณสมบัติ> <!-ระบุแอตทริบิวต์ JPA; เช่นว่าจะแสดง SQL, ภาษาถิ่นและอื่น ๆ ใน Hibernate-> <property name = "JPavendoradapter"> <bean> <!-ไม่ว่าจะสร้างไฟล์ DDL-> <property name = "generatedDl" value = "true"/> <! name = "databaseplatform" value = "org.hibernate.dialect.mysqldialect"/> <!-mysql เลือกด้วยตัวเอง-> <property name = "database" value = "mysql"/> </epean> key = "hibernate.ejb.naming_strategy"> org.hibernate.cfg.improvednamingstrategy </prop> <prop key = "hibernate.hbm2ddl.auto"> อัปเดต </prop> key = "hibernate.enable_lazy_load_no_trans"> true </prop> </props> </property> </ebean> <!-การกำหนดค่าข้อมูล jpa ของสปริง-> <!-กำหนดค่าฟังก์ชั่นของการสแกนและการสร้างพร็อกซี คุณสามารถใส่หมายเหตุประกอบแพ็คเกจทั้งหมดได้โดยตรงโดยไม่ต้องเขียน-> <jpa: repositories base-package = "com.xxx.xxx.repository" ธุรกรรม-manager-ref = "transactionManager" เอนทิตี-manager-factory-ref = "entityManagerFactory"/> <! name = "EntityManagerFactory" ref = "EntityManagerFactory"/> </ebean> <!-เปิดการทำธุรกรรมคำอธิบายประกอบ-> <tx: การทำธุรกรรมที่ขับเคลื่อนด้วยคำอธิบายประกอบ-ไดรฟ์ไดรฟ์
ข้างต้นคือการวิเคราะห์ปัญหาที่พบในการบูรณาการฤดูใบไม้ผลิ JPA และ MyBatis แนะนำให้คุณรู้จักโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!