ฉันสรุปวิธีการสามวิธีในบทความก่อนหน้า แต่มีสองวิธีที่คำอธิบายประกอบ SQL วิธีนี้ค่อนข้างสับสนดังนั้นทุกคนไม่ได้ใช้มันมากนัก มาสรุปวิธีการที่ใช้กันทั่วไปสองวิธีกันเถอะ:
1. คลาสการใช้งานพร็อกซีแบบไดนามิกโดยไม่ต้องเขียน DAO
วิธีนี้ค่อนข้างง่าย คุณไม่จำเป็นต้องใช้เลเยอร์ DAO คุณเพียงแค่ต้องกำหนดอินเทอร์เฟซ นี่เป็นเพียงการบันทึกไฟล์การกำหนดค่าดังนั้นโปรแกรมจึงง่ายมาก:
1. แผนภาพโครงสร้างโดยรวม:
2. ไฟล์การกำหนดค่าสามไฟล์และไฟล์การแมปหนึ่งไฟล์
(1) การกำหนดค่าเข้าร่วมโปรแกรมและการกำหนดค่าคอนโทรลเลอร์ front-end web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3 <play-name> เว็บไซต์ 1 </display-name> <!-ตั้งค่าการฟังและรวบรวมข้อมูลการกำหนดค่าของ ApplicationContext โดยอัตโนมัติเมื่อเว็บคอนเทนเนอร์เริ่มต้น-> <Listener> <Sistener-Lass> org.springFramework.web.context.context.contextlistener <param-name> contextconfiglocation </param-name> <param-value> classpath: config/springmvc-servlet.xml, classpath: confic/applicationcontext.xml </param-value> </context-param> <! <Tilter-class> org.springframework.web.filter.characterencodingFilter </filter-class> <init-Param> <param-Name> การเข้ารหัส </param-name> <param-value> UTF-8 </param-value> <param-value> True </param-value> </itrin-Param> </filter> <filter-mapping> <scod-name> encodingFilter </filter-name> <url-pattern>*. do </url-pattern> </filter-mapping> <! <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value> classpath: config/springmvc-servlet.xml </param-value> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name> springmvc </servlet-name> <!-การสกัดกั้นคำขอ-> <url-pattern>* <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file>
(2) เลเยอร์ควบคุมการสแกนการฉีดอัตโนมัติและดูการกำหนดค่าตัวแยกวิเคราะห์ SpringMvc-servlet.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: บริบท = "http://www.springframework.org/schema/contex xmlns: util = "http://www.springframework.org/schema/util" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: cache = "http://www.springframework.org/schema/cache" xsi: schemalocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://ww.springframework http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://ww.springframework.org/schema/schema http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://ww.springframework http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schemabc/schema http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cring-cring-chema <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย /> <!-<บริบท: Annotation-config />-> <!-บริบท: Component-Scan มีฟังก์ชั่นของคำอธิบายประกอบ-config-> <!-เลเยอร์ควบคุมการสแกน-> <บริบท: Component-Scan base-package = "com.website.Controller" id = "viewResolver"> <property name = "คำนำหน้า" value = "/web-inf/view/"> </property> <property name = "suffix" value = ". jsp"> </property> </ebeans>
(3) แหล่งข้อมูลการสแกนและการฉีดอัตโนมัติบริการสปริงจัดการ MyBatissQlSessionFactory อินเตอร์เฟส DAO LAYER PROXY แบบไดนามิกและการกำหนดค่าการทำธุรกรรม ApplicationContext.xml
จะมีไฟล์การกำหนดค่าหลายไฟล์ที่นี่
1) แหล่งข้อมูลเดียวพร็อกซีแบบไดนามิกไม่ได้ตั้งค่าของคุณสมบัติสองคุณสมบัติของ SQLSessionFactoryBeanName หรือ SQLSessionTemplateBeanName เมื่อใช้อินเตอร์เฟสเลเยอร์ DAO
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: บริบท = "http://www.springframework.org/schema/contex http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! id = "dataSource"> <property name = "driverclassName"> <dange> $ {jdbc.DriverClassName} </alue> </คุณสมบัติ> <property name = "url"> <value> $ {jdbc.url} </value> NAME = "รหัสผ่าน"> <ค่า> $ {JDBC.Password} </alues> </property> </ebean> <!-เปิดการกำหนดค่าคำอธิบายประกอบคือ AutoWried-> <!-<บริบท: Annotation-config/>-> <! base-package = "com.website.service" /> <!-เมื่อใช้ mybatis สปริงใช้ sqlsessionfactorybean เพื่อจัดการ mybatis 'sqlsessionfactory-> <bean id = "SQLSessionFactory"> <property name = "DataSource" Ref = "DataSource" /> <!-เส้นทางไฟล์แผนที่คลาสเอนทิตีมีเพียงหนึ่งในนั้นเท่านั้นที่เขียนถึงจุดสิ้นสุดและสามารถแทนที่ได้หลายครั้งด้วย mybatis/*. xml-> <ชื่อคุณสมบัติ = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/> </epeals bean = "sqlsessionfactory"/> </constructor-Arg> </ebean>-> <!-การใช้งานพร็อกซีแบบไดนามิกไม่จำเป็นต้องเขียน dao-> <bean id = "MapperscannerConfigurer"> <!-ที่นี่ value = "com.website.dao" /> <!-หากมีแหล่งข้อมูลเพียงแหล่งเดียวคุณไม่สามารถระบุได้ แต่หากมีแหล่งข้อมูลหลายแหล่งคุณต้องระบุ-> <!-<property name = "SQLSessionFactoryBeanName" ที่มีชื่อจริง name = "SQLSessionTemplateBeanName" value = "SQLSession" />-> </ebean> <!-ผู้จัดการธุรกรรม-> <bean id = "TransactionManager"> <property name = "DataSource" REF = "DataSource" </epeans>2) กำหนดค่าค่าคุณสมบัติของ SQLSessionFactoryBeanName สำหรับแหล่งข้อมูลเดียว
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: บริบท = "http://www.springframework.org/schema/contex http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! id = "dataSource"> <property name = "driverclassName"> <dange> $ {jdbc.DriverClassName} </alue> </คุณสมบัติ> <property name = "url"> <value> $ {jdbc.url} </value> NAME = "รหัสผ่าน"> <ค่า> $ {JDBC.Password} </alues> </property> </ebean> <!-เปิดการกำหนดค่าคำอธิบายประกอบคือ AutoWried-> <!-<บริบท: Annotation-config/>-> <! base-package = "com.website.service" /> <!-เมื่อใช้ mybatis สปริงใช้ sqlsessionfactorybean เพื่อจัดการ mybatis 'sqlsessionfactory-> <bean id = "SQLSessionFactory"> <property name = "DataSource" Ref = "DataSource" /> <!-เส้นทางไฟล์แผนที่คลาสเอนทิตีมีเพียงหนึ่งในนั้นเท่านั้นที่เขียนถึงจุดสิ้นสุดและสามารถแทนที่ได้หลายครั้งด้วย mybatis/*. xml-> <ชื่อคุณสมบัติ = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/> </epeals bean = "sqlsessionfactory"/> </constructor-Arg> </ebean>-> <!-การใช้งานพร็อกซีแบบไดนามิกไม่จำเป็นต้องเขียน dao-> <bean id = "MapperscannerConfigurer"> <!-ที่นี่ value = "com.website.dao" /> <!-หากมีแหล่งข้อมูลเพียงแหล่งเดียวคุณไม่สามารถระบุได้ แต่หากมีแหล่งข้อมูลหลายแหล่งคุณต้องระบุ-> <ชื่อคุณสมบัติ = "SQLSessionFactoryBeanName" ค่าที่สูงกว่า name = "SQLSessionTemplateBeanName" value = "SQLSession" />-> </ebean> <!-ผู้จัดการธุรกรรม-> <bean id = "TransactionManager"> <property name = "DataSource" REF = "DataSource" </epeans>3) กำหนดค่าค่าคุณสมบัติ SQLSessionTemplateBeanName สำหรับแหล่งข้อมูลเดียว
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: บริบท = "http://www.springframework.org/schema/contex http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! id = "dataSource"> <property name = "driverclassName"> <dange> $ {jdbc.DriverClassName} </alue> </คุณสมบัติ> <property name = "url"> <value> $ {jdbc.url} </value> NAME = "รหัสผ่าน"> <ค่า> $ {JDBC.Password} </alues> </property> </ebean> <!-เปิดการกำหนดค่าคำอธิบายประกอบคือ AutoWried-> <!-<บริบท: Annotation-config/>-> <! base-package = "com.website.service" /> <!-เมื่อใช้ mybatis สปริงใช้ sqlsessionfactorybean เพื่อจัดการ mybatis 'sqlsessionfactory-> <bean id = "SQLSessionFactory"> <property name = "DataSource" Ref = "DataSource" /> <!-เส้นทางไฟล์แผนที่คลาสเอนทิตีมีเพียงหนึ่งในนั้นเท่านั้นที่เขียนถึงจุดสิ้นสุดและหลาย ๆ อันสามารถแทนที่ได้ด้วย mybatis/*. xml-> <property name = "mapperlocations" value = "classpath: mybatis/usermapper.xml"/> </ebean> /> < /constructor-Arg> </ebean> <!-การใช้งานพร็อกซีแบบไดนามิกไม่จำเป็นต้องเขียน dao-> <bean id = "MapperscannerConfigurer"> <!-basepackage ที่นี่ หากมีแหล่งข้อมูลเพียงแหล่งเดียวคุณไม่สามารถระบุได้ แต่หากมีแหล่งข้อมูลหลายแหล่งคุณต้องระบุ-> <!-<ชื่อคุณสมบัติ = "SQLSessionFactoryBeanName" value = "SQLSessionFactory" />-> <! value = "sqlsession" /> </ebean> <!-ตัวจัดการธุรกรรม-> <bean id = "transactionManager"> <property name = "dataSource" ref = "DataSource" /> </ebean> <!4) แหล่งข้อมูลหลายแหล่ง
โปรดทราบว่าหากเป็นแหล่งข้อมูลหลายข้อมูลคุณต้องใช้ SQLSessionFactoryBeanName หรือ SQLSessionTemplateBeanName เพื่อระบุแหล่งข้อมูลเฉพาะ ฉันไม่รู้ว่าคุณสังเกตเห็นในการกำหนดค่าข้างต้นหรือไม่ หากคุณใช้ SQLSessionTemplateBeanName คุณต้อง
<bean id = "sqlsession"> <constructor-arg index = "0"> <ref bean = "sqlsessionfactory"/> </constructor-Arg> </ebean>
ในการสร้างอินสแตนซ์เฉพาะและกำหนดให้กับคุณสมบัติ SQLSessionTemplateBeanName
(4) ไฟล์การแมป mybatis SQL usermapper.xml:
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" สำหรับการสาธิตนี้เนมสเปซเป็นเส้นทางที่สมบูรณ์ของ userdao.java-> <mapper namespace = "com.website.dao.userdao"> <!-ID นี่คือชื่อของวิธีการในอินเทอร์เฟซ-> ค่า (#{id},#{name}) </insert> </mapper>ตกลงไปที่ไฟล์กำหนดค่าและทำเสร็จ มาดูเลเยอร์ควบคุมเลเยอร์ตรรกะทางธุรกิจและรหัสเลเยอร์ DAO
3. เลเยอร์คอนโทรลเลอร์
แพ็คเกจ com.website.controller; นำเข้า java.util.hashmap; นำเข้า java.util.map; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า com.website.service.userservice; @Controller @RequestMapping (value = "/user") คลาสสาธารณะ userController {// inject Userservice Object @AutoWired Userservice Userservice; @RequestMapping (value = "/save.do", method = requestMethod.get) สตริงสาธารณะ saveUser (คำขอ httpservletRequest, การตอบกลับ httpservletResponse) {string id = request.getParameter ("ID"); ชื่อสตริง = request.getParameter ("ชื่อ"); แผนที่ <สตริงสตริง> แผนที่ = ใหม่ hashmap <สตริงสตริง> (); map.put ("id", id); map.put ("ชื่อ", ชื่อ); userservice.saveuser (แผนที่); กลับ "ดัชนี"; -4. ชั้นบริการ
แพ็คเกจ com.website.service; นำเข้า java.util.map; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.service; นำเข้า org.springframework.transaction.annotation.transactional; นำเข้า com.website.dao.userdao; @Service ("Userservice") @Transactional คลาสสาธารณะผู้ใช้บริการ {// การฉีดอินเทอร์เฟซอินเตอร์เฟสอินสแตนซ์อินสแตนซ์ // วิธีการฉีดทั้งสองวิธีสามารถ @AutoWired UserDao UserDao; โมฆะสาธารณะ saveUser (แผนที่ <สตริงสตริง> แผนที่) {int end = userDao.saveUser (แผนที่); System.out.println ("สิ้นสุด:" + สิ้นสุด); -5. อินเทอร์เฟซเลเยอร์ Dao
แพ็คเกจ com.website.dao; นำเข้า java.util.map; //com.website.dao.userdao อินเตอร์เฟสสาธารณะ userdao {int saveuser (แผนที่ <สตริง, สตริง> แผนที่); - เส้นทางที่สมบูรณ์ของอินเทอร์เฟซ DAO คือเนมสเปซของไฟล์การแมปที่สอดคล้องกับอินเทอร์เฟซ DAO นี้และชื่อวิธีคือค่าของ ID
ตกลงวิธีการกำหนดค่านี้จบลงแล้วและยังมีการสาธิตขนาดเล็กที่สมบูรณ์ สรุปสั้น ๆ ด้านล่าง:
เมื่อเทียบกับวิธีการกำหนดค่าก่อนหน้า (ซึ่งจะเขียนไว้ด้านล่าง) คือใช้วิธีพร็อกซีแบบไดนามิกของอินเทอร์เฟซเลเยอร์ DAO ก่อนหน้านี้เราจะใช้เลเยอร์ DAO ด้วยตนเองในเลเยอร์ DAO จากนั้นฉีดอินสแตนซ์ SQLSessionTemplate โดยอัตโนมัติเพื่อเรียกใช้วิธีการเฉพาะเช่นแทรก ("", "") SelectOne ("", "") และวิธีอื่น ๆ พารามิเตอร์แรกคือที่อยู่ของไฟล์การแมป: เนมสเปซ+ID และพารามิเตอร์ที่สองคือเงื่อนไขที่ผ่าน ด้วยวิธีนี้ MyBatis จะพบไฟล์การแมปเฉพาะตามพารามิเตอร์สองพารามิเตอร์ที่เราผ่านการแยกวิเคราะห์และสอบถาม การใช้พร็อกซีแบบไดนามิกที่นี่จะช่วยลดขั้นตอนของการใช้อินเทอร์เฟซ DAO แต่เรานำไปใช้ในฤดูใบไม้ผลิ มีปัญหา เราผ่านพารามิเตอร์เงื่อนไขการสืบค้น แต่เส้นทางเฉพาะของไฟล์การแมปคือ: Namespce+ID ฉันควรทำอย่างไรถ้าไม่ผ่าน? นั่นคือเนมสเปซของไฟล์การแมปของคุณจะต้องเป็นชื่อเต็มของคลาสอินเตอร์เฟสและ ID จะต้องเป็นชื่อวิธีในอินเทอร์เฟซเพื่อให้พร็อกซีแบบไดนามิกสามารถค้นหาเส้นทางและมีพารามิเตอร์ คุณคิดว่ามันเหมือนกันหรือไม่? ฮ่าฮ่าฮ่า
2. ใช้อินเทอร์เฟซเลเยอร์ DAO ด้วยตนเองด้วยตนเอง
ก่อนอื่นมาดูการกำหนดค่าและรหัสสำหรับการใช้เลเยอร์ DAO ด้วยตนเอง:
1. โครงสร้างแผนภาพของหัวข้อ
2. ไฟล์การกำหนดค่าสามไฟล์และไฟล์การแมป
(1) รายการโปรแกรมการกำหนดค่าคอนโทรลเลอร์ front-end web.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml XSI: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3 <pramplay-name> เว็บไซต์ 2 </display-name> <!-โหลดการกำหนดค่าคอนเทนเนอร์สปริง-> <listener> <ผู้ฟัง> org.springframework.web.context.context.contextloaderListener </listener-class> </listener> <! classpath: config/springmvc-servlet.xml, classpath: config/applicationcontext.xml </param-value> </context-param> <!-ตัวกรองการเข้ารหัสอักขระ-> <filter> <init-Param> <param-Name> การเข้ารหัส </param-name> <param-value> UTF-8 </param-value> </init-param> <init-Param> <init-Param> <sider-name> encodingFilter </filter-name> <url-pattern>*. Do </url-pattern> </filter-mapping> <!-คอนโทรลเลอร์ front-end-> <servlet> <param-name> contextconfiglocation </param-name> <param-value> classpath: config/springmvc-servlet.xml </param-value> </init-param> <!-ไฟล์การกำหนดค่านี้ถูกโหลดเมื่อคอนเทนเนอร์เริ่มต้น-> <!-คำขอสกัดกั้น-> <url-pattern>*. ทำ </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index.html </felcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default.jsp </welcome-file> </welcome-file-list> </eb-app>
(2) เลเยอร์ควบคุมการสแกนการฉีดอัตโนมัติและดูการกำหนดค่าตัวแยกวิเคราะห์ SpringMvc-servlet.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: บริบท = "http://www.springframework.org/schema/contex xmlns: util = "http://www.springframework.org/schema/util" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: jdbc = "http://www.springframework.org/schema/jdbc" xmlns: cache = "http://www.springframework.org/schema/cache" xsi: schemalocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://ww.springframework http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://ww.springframework.org/schema/schema http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://ww.springframework http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schemabc/schema http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cring-cring-chema <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย/> <!-<บริบท: คำอธิบายประกอบ-config/>-> <!-สแกน-> <บริบท: คอมโพเนนต์ -scan base-base-package = "com.website.controller"> </บริบท: Component-scan> <! </perty> <property name = "suffix" value = ". jsp"> </property> </ebean> </ebeans>
(3) แหล่งข้อมูลการสแกนและการฉีดอัตโนมัติบริการสปริงจัดการ MyBatissQlSessionFactory และการกำหนดค่าการทำธุรกรรม ApplicationContext.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: บริบท = "http://www.springframework.org/schema/contex http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <! id = "dataSource"> <property name = "driverclassName"> <dange> $ {jdbc.DriverClassName} </alue> </คุณสมบัติ> <property name = "url"> <value> $ {jdbc.url} </value> NAME = "รหัสผ่าน"> <ค่า> $ {JDBC.Password} </alues> </property> </ebean> <!-เปิดการกำหนดค่าคำอธิบายประกอบคือ AutoWried-> <! ในการสแกนถั่วก่อนที่จะสามารถใช้-> <บริบท: ส่วนประกอบ-สแกนฐานแพคเกจ = "com.website.service, com.website.dao" /> <!-เมื่อใช้ mybatis สปริงใช้ sqlsessionfactorybean เพื่อจัดการ sqlsessionfactory ของ mybatis มันมีวิธีการบางอย่าง-> <bean id = "sqlsessionfactory"> <property name = "dataSource" ref = "dataSource" /> <!-เส้นทางไฟล์การกำหนดค่า mybatis-> <property name = "configlocation" value = "" /> <! value = "classpath: mybatis/usermapping.xml"/> </ebean> <!-ในความเป็นจริงตัวอย่างของคลาสนี้คือ sqlsession ใน mybatis-> <bean id = "sqlsession"> <contructor-Arg index = "0"> < <property name = "dataSource" ref = "dataSource" /> </ebean> <!-ใช้ธุรกรรมคำอธิบายประกอบแบบเต็ม-> <tx: การทำธุรกรรมที่ขับเคลื่อนด้วยคำอธิบายประกอบ = "TransactionManager"(4) ไฟล์การแมป mybatis
<? xml เวอร์ชัน = "1.0" การเข้ารหัส = "utf-8"?> <! doctype mapper สาธารณะ "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd หลังจากที่เราเขียนเส้นทางที่สมบูรณ์ใน Dao Layer MyBatis คือการแมปของคำสั่ง SQL ที่เกี่ยวข้องในไฟล์นี้-> <mapper namespace = "com.website.usermapper"> <!-พารามิเตอร์เป็นประเภทของพารามิเตอร์ที่คุณยอมรับ-> <! ผู้ใช้ (ID, ชื่อ, รหัสผ่าน) ค่า (#{id},#{ชื่อ},#{รหัสผ่าน}) </insert> </mapper>คุณอาจเห็นว่า Namespace +ID ของไฟล์การแมปที่นี่ได้รับการปรับแต่งแทนชื่อคลาสเต็ม +ID ของอินเทอร์เฟซเลเยอร์ DAO
3. คอนโทรลเลอร์
แพ็คเกจ com.website.controller; นำเข้า java.util.hashmap; นำเข้า java.util.map; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.controller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า com.website.service.userservice; /*** @author whd data 5 มิถุนายน 2016*/@controller @requestmapping (value = "/user") คลาสสาธารณะ userController {@autoWired Userservice ผู้ใช้บริการ; @RequestMapping (value = "/save.do") สตริงสาธารณะ SaveUser (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) {string id = request.getParameter ("id"); ชื่อสตริง = request.getParameter ("ชื่อ"); สตริงรหัสผ่าน = request.getParameter ("รหัสผ่าน"); แผนที่ <สตริงสตริง> แผนที่ = ใหม่ hashmap <สตริงสตริง> (); map.put ("id", id); map.put ("ชื่อ", ชื่อ); map.put ("รหัสผ่าน", รหัสผ่าน); userservice.saveuser (แผนที่); กลับ "ดัชนี"; -4. บริการเลเยอร์ตรรกะธุรกิจ
แพ็คเกจ com.website.service; นำเข้า java.util.map; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.service; นำเข้า org.springframework.transaction.annotation.transactional; นำเข้า com.website.dao.userdao; / *** @author whd data 5 มิถุนายน 2016*/ @service ("userservice") @Transactional คลาสสาธารณะผู้ใช้บริการ {@AutoWired UserDao UserDao; โมฆะสาธารณะ saveUser (แผนที่ <สตริงสตริง> แผนที่) {userDao.saveUser (แผนที่); -5. Dao Layer
แพ็คเกจ com.website.dao; นำเข้า java.util.map; นำเข้า org.mybatis.spring.sqlsessionTemplate; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.repository; / *** @author whd data 5 มิถุนายน 2016*/ @repository ("userdao") คลาสสาธารณะ userdao {@autowired ส่วนตัว sqlsessiontemplate sqlsession; โมฆะสาธารณะ saveUser (แผนที่ <สตริง, สตริง> แผนที่) {int end = sqlsession.insert ("com.website.usermapper.insertuser", แผนที่); System.out.println ("end" + end); -เราดูที่ SQLSessionTemplate ของชั้น DAO คว่ำ นี่คือวัตถุ SQLSession ใน mybatis เราเห็นว่ามีการกำหนดค่าใน ApplicationContext.xml ดังนั้นเมื่อเราใช้มันสปริงจะฉีดโดยอัตโนมัติ เราสามารถใช้มันโดยตรงโดยไม่ต้องสร้างมันเอง นี่คือการผกผันควบคุมที่เรียกว่า ตกลงวิธีการกำหนดค่าของไฟล์ทั้งสองนี้จบลงแล้ว
สรุป
ข้างต้นเป็นคำอธิบายโดยละเอียดเกี่ยวกับวิธีการกำหนดค่าของฤดูใบไม้ผลิและ mybatis แนะนำให้คุณ (สองวิธีที่ใช้กันทั่วไป) ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!