เนื่องจากมีวิธีการรวมหลายวิธีในกระบวนการของสปริงที่รวม MyBatis โดยเฉพาะแหล่งข้อมูล ฉันมักจะเห็นวิธีการกำหนดค่าที่แตกต่างกันและรู้สึกยุ่งอยู่เสมอดังนั้นฉันจึงมีเวลาสรุปวันนี้
1. ใช้ org.mybatis.spring.mapper.mapperscannerconfigurer
อันที่จริงนี่เป็นวิธีเดียวกับวิศวกรรมย้อนกลับ
1. ไฟล์การกำหนดค่าแหล่งข้อมูล
2. ไฟล์ dao
แพ็คเกจ com.jdd.mapper; นำเข้า com.jdd.pojo.employee; นำเข้า java.util.list; Interface Public EmployeEmapper {พนักงานสาธารณะ getemployeebyId (ID int); รายการสาธารณะ <Spop อยู่> findalleMployees (); -3. ไฟล์ mapper.xml
<? xml version = "." การเข้ารหัส = "utf-"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper .//en" "http://mybatis.org/dtd/mybatis--mapper.dtd"> parameterType = "int" resultType = "com.jdd.pojo.employee"> <! [cdata [เลือก * จากพนักงานที่ id = #{id}; ]]> </select> <select id = "findalleMployees" resultType = "com.jdd.pojo.employee"> <! [cdata [เลือก * จากพนักงานที่สถานะ = ''; ]]> </select> </mapper>ด้วยวิธีนี้คุณสามารถฉีดอินเทอร์เฟซ DAO ลงในคลาสบริการโดยตรง
แพ็คเกจ com.jdd.service.impl; นำเข้า com.jdd.mapper.employemapper; นำเข้า com.jdd.pojo.employee; นำเข้า com.jdd.service.employeeservice; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.service; นำเข้า java.util.list; @Service ("พนักงานบริการ") พนักงานชั้นเรียนสาธารณะบริการ uspliplements พนักงานบริการ {@AutoWired ExperApper ExperApper; @Override พนักงานสาธารณะ getEmployeEBYID (ID int) {return EmployEmapper.getEmployeebyId (ID); } @Override List Public <SPOTHERE> FindAleLemployees () {return EmpereEmapper.FindAlleMployees (); -2. ใช้คลาสนามธรรม org.mybatis.spring.support.sqlsessiondaosupport เพื่อฉีดเข้าไปใน SQLSessionFactory
1. ไฟล์การกำหนดค่าแหล่งข้อมูล
<? xml version = "." การเข้ารหัส = "utf-"?> <ถั่ว xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/Contexte" xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/tx" xmlns: xsi = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd http://www.springframework.org. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-..xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-..xsd http://www.springframework.org/schema/util http://ww.springframework.org/schema -> <บริบท: สถานที่ที่วางตำแหน่งตำแหน่ง = "classpath: ทรัพยากร /*. คุณสมบัติ" /> <!-พูลการเชื่อมต่อฐานข้อมูล-> <bean id = "dataSource" destroy-method = "close"> <property name = "driverclassname" value = "$ {jdbc.driver}" /> < value = "$ {jdbc.username}" /> <property name = "รหัสผ่าน" value = "$ {jdbc.password}" /> <property name = "maxactive" value = " /> <property name =" minidle "value =" " /> value = "classpath: mybatis/sqlmapconfig.xml"> </คุณสมบัติ> <property name = "dataSource" ref = "dataSource"> </property> <property name = "mapperlocations" value = "compath: com/jdd/mapper/*. xml"2. คลาสที่ใช้
แพ็คเกจ com.jdd.dao; นำเข้า org.apache.ibatis.session.sqlsessionfactory; นำเข้า org.mybatis.spring.support.sqlsessiondaosupport; นำเข้า Javax.annotation.Resource; Public Abstract Class Agexao ขยาย SQLSessionDaosupport {@Resource โมฆะสาธารณะ setSqlSessionFactory (SQLSessionFactory SQLSessionFactory) {super.SetSqlSessionFactory (SQLSessionFactory); -3. Interface Employeedao.java คลาส
แพ็คเกจ com.jdd.dao; นำเข้า com.jdd.pojo.employee; นำเข้า java.util.list; อินเทอร์เฟซสาธารณะ Emapereedao {พนักงาน getemployeebyidid (ID int); รายการ <Spop อยู่> FindAleLemployees (); -4. Dao ใช้คลาส Employeedaoimpl
แพ็คเกจ com.jdd.dao.impl; นำเข้า com.jdd.dao.basedao; นำเข้า com.jdd.dao.employeedao; นำเข้า com.jdd.pojo.employee; นำเข้า org.springframework.stereotype.repository; นำเข้า java.util.list; @Repository ("Employeedao") Public Class Employeedaoimpl ขยายการใช้งาน Employeedao {@Override พนักงานสาธารณะ getemployeebyId (int id) {return this.getsqlsession () Selectone ("com.jdd.dao.employeedao.getemployee } @Override List Public <SPOTHERE> findAleLemployees () {return this.getSqlSession () SelectList ("com.jdd.dao.employeedao.findalleMployees"); -5. ด้วยวิธีนี้คุณสามารถฉีด Employeedao เข้าสู่ชั้นบริการ
3. ใช้ org.mybatis.spring.sqlsessionTemplate คลาสเทมเพลต
1. ไฟล์แหล่งข้อมูล
<? xml version = "." การเข้ารหัส = "utf-"?> <ถั่ว xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/Contexte" xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/tx" xmlns: xsi = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-..xsd http://www.springframework.org. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-..xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-..xsd http://www.springframework.org/schema/util http://ww.springframework.org/schema -> <บริบท: สถานที่ที่วางตำแหน่งตำแหน่ง = "classpath: ทรัพยากร /*. คุณสมบัติ" /> <!-พูลการเชื่อมต่อฐานข้อมูล-> <bean id = "dataSource" destroy-method = "close"> <property name = "driverclassname" value = "$ {jdbc.driver}" /> < value = "$ {jdbc.username}" /> <property name = "รหัสผ่าน" value = "$ {jdbc.password}" /> <property name = "maxactive" value = " /> <property name =" minidle "value =" " /> value = "classpath: mybatis/sqlmapconfig.xml"> </คุณสมบัติ> <property name = "dataSource" ref = "dataSource"> </property> <property name = "mapperlocations" value = "classpath: com/jdd/mapper/*. xml" index = "" ref = "sqlsessionfactory"/> </ebean> </ebeans>2. คลาสที่ใช้
แพ็คเกจ com.jdd.dao; นำเข้า org.mybatis.spring.sqlsessionTemplate; นำเข้า Javax.annotation.Resource; Public Abstract Class Basedao {Public SQLSessionTemplate SQLSessionTemplate; @Resource โมฆะสาธารณะ setSqlSessionTemplate (SQLSessionTemplate SQLSessionTemplate) {this.SQLSessionTemplate = SQLSessionTemplate; - 3. Interface Employeedao.java คลาส
แพ็คเกจ com.jdd.dao; นำเข้า com.jdd.pojo.employee; นำเข้า java.util.list; อินเทอร์เฟซสาธารณะ Emapereedao {พนักงาน getemployeebyidid (ID int); รายการ <Spop อยู่> FindAleLemployees (); -4. Dao ใช้คลาส Employeedaoimpl
แพ็คเกจ com.jdd.dao.impl; นำเข้า com.jdd.dao.basedao; นำเข้า com.jdd.dao.employeedao; นำเข้า com.jdd.pojo.employee; นำเข้า org.springframework.stereotype.repository; นำเข้า java.util.list; @Repository ("Employeedao") Public Class Emploeneedaoimpl ขยายการใช้งาน Employeedao {@Override พนักงานสาธารณะ getemployeebyId (int id) {return sqlsessiontemplate.selectone ("com.jdd.dao.employeedao.getemployeeid } @Override List Public <SPOTHERE> FindAleLemployees () {ส่งคืน SQLSessionTemplate.SelectList ("com.jdd.dao.employeedao.findallemployeees"); -5. ตอนนี้คุณสามารถฉีด Employeedao ลงในคลาสบริการโดยตรงเพื่อใช้งานได้
หมายเหตุ: การฉีดของที่นี่มีความยืดหยุ่นมากขึ้น นอกจากนี้คุณยังสามารถฉีด SQLSessionFactory จากนั้นสร้าง SQLSessionTemplate ในวิธีการตั้งค่าดังต่อไปนี้:
แพ็คเกจ com.jdd.dao; นำเข้า org.apache.ibatis.session.sqlsessionfactory; นำเข้า org.mybatis.spring.sqlsessionTemplate; นำเข้า Javax.annotation.Resource; Public Abstract Class Basedao {Public SQLSessionTemplate SQLSessionTemplate; @Resource โมฆะสาธารณะ setSqlSessionFactory (SQLSessionFactory SQLSessionFactory) {SQLSessionTemplate = SQLSessionTemplate ใหม่ (SQLSessionFactory); }}ในความเป็นจริงไม่ว่าจะเป็นวิธีการสืบทอดคลาส SQLSessionDaosupport, ฉีด SQLSessionFactory หรือฉีด SQLSessionTemplate โดยตรงมันก็เหมือนกัน
หากคุณฉีด SQLSessionFactory มันจะสร้าง SQLSessionTemplate ผ่าน SQLSessionFactory ที่ด้านล่างแล้วทำงานผ่าน API
หากคุณไม่เชื่อให้ฉันดูซอร์สโค้ดของ SQLSessionDaosupport:
// // ซอร์สโค้ดที่สร้างขึ้นใหม่จากไฟล์. class โดย Intellij Idea // (ขับเคลื่อนโดย fernflower decompiler) // package org.mybatis.spring.support; นำเข้า org.apache.ibatis.session.sqlsession; นำเข้า org.apache.ibatis.session.sqlsessionfactory; นำเข้า org.mybatis.spring.sqlsessionTemplate; นำเข้า org.springframework.dao.support.daosupport; นำเข้า org.springframework.util.assert; บทคัดย่อระดับสาธารณะระดับ SQLSessionDaosupport ขยาย daosupport {ส่วนตัว sqlsession sqlsession; บูลีนส่วนตัว externalsqlsession; สาธารณะ SQLSessionDaosupport () {} โมฆะสาธารณะ setSqlSessionFactory (SQLSessionFactory SQLSessionFactory) {ถ้า (! this.externalsqlsession) {this.sqlsession = new SQLSessionTemplate (SQLSessionFactory); }} โมฆะสาธารณะ setSqlSessionTemplate (SQLSessionTemplate SQLSessionTemplate) {this.SQLSession = SQLSessionTemplate; this.externalsqlsession = true; } สาธารณะ sqlsession getSqlsession () {return this.sqlsession; } การป้องกันโมฆะ checkdaoconfig () {assert.notnull (this.sqlsession, "คุณสมบัติ 'sqlsessionfactory' หรือ 'SQLSessionTemplate' เป็นสิ่งจำเป็น"); -ในทำนองเดียวกัน SQLSessionTemplate สืบทอดอินเตอร์เฟส SQLSession ดังนั้นเอฟเฟกต์จะเหมือนกันโดยไม่คำนึงถึงการทำงาน
// // ซอร์สโค้ดที่สร้างขึ้นใหม่จากไฟล์. class โดย Intellij Idea // (ขับเคลื่อนโดย fernflower decompiler) // package org.mybatis.spring; นำเข้า java.lang.reflect.invocationhandler; นำเข้า java.lang.reflect.method; นำเข้า java.lang.reflect.proxy; นำเข้า java.sql.connection; นำเข้า java.util.list; นำเข้า java.util.map; นำเข้า org.apache.ibatis.exceptions.persistenceexception; นำเข้า org.apache.ibatis.executor.batchresult; นำเข้า org.apache.ibatis.reflection.exceptionUtil; นำเข้า org.apache.ibatis.session.configuration; นำเข้า org.apache.ibatis.session.executortype; นำเข้า org.apache.ibatis.session.resulthandler; นำเข้า org.apache.ibatis.session.rowbounds; นำเข้า org.apache.ibatis.session.sqlsession; นำเข้า org.apache.ibatis.session.sqlsessionfactory; นำเข้า org.springframework.dao.support.persistenceExceptionTranslator; นำเข้า org.springframework.util.assert; ระดับสาธารณะ SQLSessionTemplate ใช้ SQLSESSION {ส่วนตัว SQLSessionFactory SQLSessionFactory Executortype Executortype ส่วนตัว SQLSESSION SQLSESSIONPROXY ส่วนตัวครั้งสุดท้าย; Private PersistenceExceptionTranslator ExceptionTranslator; SQLSessionTemplate สาธารณะ (SQLSessionFactory SQLSessionFactory) {สิ่งนี้ (SQLSessionFactory, SQLSessionFactory.getConfiguration (). getDefaultExecutortype ()); } สาธารณะ SQLSessionTemplate (SQLSessionFactory SQLSessionFactory, Executortype Executortype) {นี่ (SQLSessionFactory, Executortype, MyBatisexceptionTranslator (SQLSessionFactory.getConfiguration () } สาธารณะ SQLSessionTemplate (SQLSessionFactory SQLSessionFactory, Executortype Executortype, PersistenceExceptionTranslator ExceptionTranslator) {assert.notNull (SQLSessionFactory, "SQLSessionFactory ' assert.notnull (executortype, "คุณสมบัติ 'executortype' เป็นสิ่งจำเป็น"); this.sqlSessionFactory = SQLSessionFactory; this.executortype = executortype; this.exceptionTranslator = ExceptionTranslator; this.sqlSessionProxy = (SQLSession) Proxy.newProxyInstance (SQLSessionFactory.class.getClassLoader (), คลาสใหม่ [] {sqlsession.class}, SQLSessionTemplate.SqlSessionInterceptor ()); } สาธารณะ SQLSessionFactory GetSqlSessionFactory () {return this.SQLSessionFactory; } public executortype getExecutortype () {return this.executortype; } Public PersistenceExceptionTranslator getPersistenceExceptionTranslator () {return this.exceptionTranslator; } สาธารณะ <t> t selectone (คำสั่งสตริง) {return this.sqlsessionproxy.selectone (คำสั่ง); } สาธารณะ <t> t selectone (คำสั่งสตริง, พารามิเตอร์วัตถุ) {return this.sqlsessionproxy.selectone (คำสั่ง, พารามิเตอร์); } สาธารณะ <k, v> map <k, v> selectmap (คำสั่งสตริง, สตริง mapkey) {return this.sqlsessionproxy.selectmap (คำสั่ง, mapkey); } สาธารณะ <k, v> map <k, v> selectmap (คำสั่งสตริง, พารามิเตอร์วัตถุ, สตริง mapkey) {return this.sqlsessionproxy.selectmap (คำสั่ง, พารามิเตอร์, mapkey); } สาธารณะ <k, v> map <k, v> selectmap (คำสั่งสตริง, พารามิเตอร์วัตถุ, สตริง mapkey, Rowbounds Rowbounds) {ส่งคืน this.sqlSessionProxy.selectMap (คำสั่ง, พารามิเตอร์, mapkey, rowbounds); } สาธารณะ <E> รายการ <E> selectList (คำสั่งสตริง) {return this.sqlSessionProxy.SelectList (คำสั่ง); } สาธารณะ <E> รายการ <E> selectList (คำสั่งสตริง, พารามิเตอร์วัตถุ) {ส่งคืน this.sqlSessionProxy.SelectList (คำสั่ง, พารามิเตอร์); } สาธารณะ <E> รายการ <E> SELECTLIST (คำสั่งสตริง, พารามิเตอร์วัตถุ, ROWBOUNDS ROWBOUNDS) {ส่งคืน this.SQLSessionProxy.SELECTLIST (คำสั่ง, พารามิเตอร์, ROWBOUNDS); } โมฆะสาธารณะเลือก (คำสั่งสตริง, resulthandler handler) {this.sqlsessionproxy.select (คำสั่ง, handler); } โมฆะสาธารณะเลือก (คำสั่งสตริง, พารามิเตอร์วัตถุ, resulthandler handler) {this.sqlsessionProxy.select (คำสั่ง, พารามิเตอร์, ตัวจัดการ); } โมฆะสาธารณะ SELECT (คำสั่งสตริง, พารามิเตอร์วัตถุ, Rowbounds Rowbounds, ResulThandler Handler) {this.sqlSessionProxy.Select (คำสั่ง, พารามิเตอร์, Rowbounds, Handler); } การแทรก int สาธารณะ (คำสั่งสตริง) {return this.sqlSessionProxy.insert (คำสั่ง); } int public int แทรก (คำสั่งสตริง, พารามิเตอร์วัตถุ) {return this.sqlSessionProxy.insert (คำสั่ง, พารามิเตอร์); } การอัปเดต int สาธารณะ (คำสั่งสตริง) {return this.sqlSessionProxy.update (คำสั่ง); } การอัปเดต int สาธารณะ (คำสั่งสตริง, พารามิเตอร์วัตถุ) {ส่งคืน this.sqlSessionProxy.Update (คำสั่ง, พารามิเตอร์); } public int delete (คำสั่งสตริง) {return this.sqlSessionProxy.delete (คำสั่ง); } public int delete (คำสั่งสตริง, พารามิเตอร์วัตถุ) {return this.sqlSessionProxy.delete (คำสั่ง, พารามิเตอร์); } สาธารณะ <t> t getMapper (คลาส <t> ประเภท) {return this.getConfiguration (). getMapper (พิมพ์, นี่); } โมฆะสาธารณะ commit () {โยน unsupportedoperationException ใหม่ ("การกระทำแบบแมนนวลไม่ได้รับอนุญาตในการจัดการ sqlsession ในฤดูใบไม้ผลิ"); } โมฆะสาธารณะ commit (กองกำลังบูลีน) {โยน unsupportedoperationException ใหม่ ("การกระทำแบบแมนนวลไม่ได้รับอนุญาตในการจัดการ sqlsession ในฤดูใบไม้ผลิ"); } โมฆะสาธารณะย้อนกลับ () {โยน unsupportedOperationException ใหม่ ("ไม่อนุญาตให้ย้อนกลับด้วยตนเองผ่าน SQLSession ที่มีการจัดการในฤดูใบไม้ผลิ"); } โมฆะสาธารณะย้อนกลับ (กองกำลังบูลีน) {โยน unsupportedoperationException ใหม่ ("ไม่อนุญาตให้ย้อนกลับแบบแมนนวลผ่าน SQLSession ที่มีการจัดการในฤดูใบไม้ผลิ"); } public void close () {โยน unsupportedoperationException ใหม่ ("ปิดด้วยตนเองไม่ได้รับอนุญาตในการจัดการ sqlsession สปริง"); } โมฆะสาธารณะ ClearCache () {this.sqlSessionProxy.ClearCache (); } การกำหนดค่าสาธารณะ getConfiguration () {return this.sqlSessionFactory.getConfiguration (); } การเชื่อมต่อสาธารณะ getConnection () {return this.sqlSessionProxy.getConnection (); } รายการสาธารณะ <batchResult> flushstatements () {return this.sqlsessionProxy.flushstatements (); } คลาสส่วนตัว SQLSessionInterceptor ใช้ InvocationHandler {Private SQLSessionInterceptor () {} วัตถุสาธารณะเรียกใช้ (พร็อกซีวัตถุวิธีการวิธีการวัตถุ [] args) โยน {sqlsession sqlsession = sqlsessionuts.getsqlsession SQLSessionTemplate นี้. executortype, SQLSessionTemplate.His.ExceptionTranslator); วัตถุที่ยังไม่ได้ห่อ; ลอง {object result = method.invoke (sqlsession, args); if (! sqlsessionutils.issqlsessiontransactional (sqlsession, sqlsessiontemplate.his.sqlsessionfactory)) {sqlsession.commit (จริง); } Unwrapped = ผลลัพธ์; } catch (throwable var) {unwrapped = exctionUtil.unWrapthrowable (var); if (sqlsessionTemplate.his.exceptionTranslator! = null && unprapped อินสแตนซ์ของ PersistenceException) {SQLSessionUts.ClosesQlSession (SQLSession, SQLSessionTeMplate.SQLSessionFactory); sqlsession = null; translated throwable = sqlsessiontemplate this.exceptionTranslator.translateExceptionificible ((PersistenceException) UNWRAPPED); if (แปล! = null) {Unwrapped = แปล; }} โยน (โยนได้) Unwapped; } ในที่สุด {if (sqlsession! = null) {sqlsessionutils.closesqlsession (sqlsession, sqlsessiontemplate.this.sqlsessionfactory); }} return Unwapped; -สรุป
ข้างต้นเป็นวิธีการกำหนดค่าหลายวิธีสำหรับแหล่งข้อมูลการรวมสปริงใน MyBatis แนะนำให้คุณรู้จัก ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉัน บรรณาธิการจะตอบกลับคุณทันเวลา!