منذ فترة ، استولت على مشروع تم تطويره باستخدام Springboot و Spring-Data-JPA. في وقت لاحق ، تمت إضافة صديق جديد ، قائلاً إن JPA يصعب استخدامه من MyBatis ، ومن الصعب أيضًا كتابة استعلام مع جداول متعددة ، لذلك أضفت دعم MyBatis.
في البداية
@configuration@enablejParePositories ("com.xxx.xxx.repository") class jpaconfigعند استخدام هذه الطريقة لتكوين JPA ، تتمثل إحدى المشكلات في أنه يمكن تحديدها ولكن لا يمكن حفظها ، لذلك يتم تعديلها إلى ملف التكوين:
ما يلي يقدم مباشرة ملف التكوين:
1. تكوين الربيع
<؟ 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.xsdhtp: //www.springframework.or g/schema/context/spring-context.xsdhtp: //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"> </bean> <bean id = "configproperties" class = "org.springframework.beans.factory.config.propertiesFactoryBean" <property name = "solceations"> <list> <value> classpath*: jdbc.properties </value> <value> classpath*: app.properties </value> </list> </property> </bean> <bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.preferCoRENCELSTINGERCOMERCOMERCONFURENCOMER name = "systempropertiesModename" value = "system_properties_mode_override"/> <property name = "fileEncoding" value = "utf-8"/> <property name = "properties" ref = "inter-meth"/> </bean> <! خصائص عنوان URL ، المستخدم ، كلمة المرور-> <!-<property name = "driverClassName" value = "$ {ds.driverClassName}"/>-> <property name = "url" value = "$ {ds.url}"/> <property name = "username" value = "$ {ds.username} value = "$ {ds.password}"/> <!-تكوين حجم التهيئة ، الحد الأدنى والحد الأقصى-> <property name = "initialsize" value = "$ {ds.InitialSize}"/> <property name = "$ {ds.maxure} لكي يتم توقيت الاتصال-> <property name = "maxwait" value = "$ {ds.MaxWait}"/> <!-تكوين المدة التي يستغرقها التحقق مرة واحدة ، اكتشف اتصال الخمول الذي يجب إغلاقه ، بالملليسيكوند-> <property name = "timetweetweenevictionrunsmillis" value = {ds.ttimeTene الحد الأدنى للوقت للاتصال بالبقاء على قيد الحياة في المجمع بالمللي ثانية-> <property name = "minevictableDletimemillis" value = "$ {ds.MineVictableDletimEmillis}"/> <property name = "ValidationQuery" value = "$ {ds.validationQuery}"/> value = "$ {ds.testWhileIdle}"/> <property name = "testOnBorrow" value = "$ {ds.testonBorrow}"/> <property name = "testOnreturn" value = "$ {ds.TestOnreturn}"/> <! value = "$ {ds.PoolPrepedStatements}"/> <property name = "maxPoolPrepedStateMentPerConnectionSize" value = "$ {ds.MaxPoolPropedStatePerConnections}"/> <! إغلاق الاتصال المهجور-> <property name = "logabandoned" value = "$ {ds.logabandoned}"/> </bean> <!-Spring و MyBatis متكامل تمامًا ، وملف MyBatis Configuration لا يتطلب "dataSource <sursource" name = "configlocation" value = "classpath: mybatis-config.xml"/> <property name = "typealiasespackage" value = "com.xxx.culture.domain"/> <!-تلقائيًا مسح ملف التعيين. توجد واجهة DAO ، ستجد Spring تلقائيًا الفئة الموجودة أسفلها-> <bean> <property name = "basePackage" value = "com.xxx.xxx.dao"/> </bean> <!-(إدارة المعاملات) ، مدير المعاملات ، استخدم jTatransActionManager للعالم tx-> id = "TransactionManager" class = "org.springframework.jdbc.datasource.datasourCetransActionManager"> <property name = "datasource" ref = "datasource"/> </bean> <! المعاملات-manager = "TransactionManager"> <tx: entributes> <!-طرق إدارة المعاملات التي تبدأ بالإدراج ، التحديث ، حذف ، تراجع طالما كان هناك استثناء-> <tx: method name = "insert*" spectation = "required" randback-for = "java.lang.Throw. flack-for = "java.lang.throwable"/> <tx: method name = "delete*" spection = "required" Rollback-for = "java.lang.Throwable"/> <tx: method name = "remove*" seversation = "required" rabback-for = "java.lang.throwable"/> flackback-for = "java.lang.throwable"/> <tx: method name = "add*" spection = "required"/> <tx: method name = "flush*" spection = "required"/> <! 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 = "*"/> </tx: entributes> </sx: explive> <!-تكوين المعاملة com.xxx.smp.service ..*Impl.*(..) com.xxx.xxx.service ..*(..)) "/> <aop: Advisor excloy-ref =" txAdvice "pointcut-ref =" servicemethods "/> </aop: config> <!-configure spring for spring for cglib proxy-> <aop: side-aoproxy proxy- الدعم-> <TX: معاملة المعاملة التي تعتمد على التعليقات التوضيحية = "TransactionManager"/> <import Resource = "ApplicationContext-jpa.xml"/> </bans>2. تكوين JPA
واجهت مشكلة في البداية: JPA org.hibernate.lazyinitializationException: لا يمكن تهيئة الوكيل - لا جلسة
التكوين كما يلي
<؟ xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3 rk.org/schema/tx"xmlns: jpa="http://www.springframework.org/schema/data/jpa"xsi:schemalocation="http://www.springfframework.org/schemains http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhtp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhtp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> <bean id =" entityManagerFactory "class =" org.springframework.mpa.localcontainerentityManagean " name = "dataSource" ref = "datasource"/> <!-حدد مسار حزمة فئة الكيان الكيان-> <property name = "packagestoscan"> <list> <value> com.xxx.xxx.jpadomain </value> </list> </sprement> <!-تحديد سمة JPA ؛ مثل ما إذا كان سيتم عرض SQL ، واللهجة ، وما إلى ذلك في hibernate-> <property name = "jpavendoradapter"> <!-ما إذا كان لإنشاء ملف ddl-> <property name = "enderddl" value = "true"/> <!-ما إذا كان يجب عرض sql-> <propert name = "databaseplatform" value = "org.hibernate.dialect.mysqldialect"/> <!-mysql ، حدد بنفسك-> <property name = "database" value = "mysql"/> </bean> </propert key = "hibernate.ejb.naming_strategy"> org.hibernate.cfg.ImproveDnamingStrategy </prop> <prop key = "hibernate.hbm2ddl.auto"> تحديث </prop> <prespring = "hibernate. تكوين البيانات JPA-> <!-تكوين وظيفة تمكين المسح وإنشاء مصانع الوكيل تلقائيًا = "com.monk.base.jpa.peakjparepositoryfactory" طريقة تعليق الفول المحددة في حد ذاتها. يمكنك توضيح جميع الحزم مباشرة دون الكتابة-> <jpa: repositories base-package = "com.xxx.xxx.repository" Transaction-Manager-REF = "TransactionManager" Entity-Manager-Factory-Ref = "entityManagerFactory"/> <! id = "TransactionManager"> <property name = "entityManagerFactory" ref = "entityManagerFactory"/> </bean> <!-قم بتشغيل معاملة التعليقات التوضيحية-> <tx: معاملة معاملة تعليمية = "True-true".
ما سبق هو تحليل للمشاكل التي واجهتها في دمج الربيع JPA و MyBatis الذي قدمه لك المحرر. آمل أن يكون ذلك مفيدًا لك. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة لي وسوف يرد المحرر إليك في الوقت المناسب. شكرا جزيلا لدعمكم لموقع wulin.com!