MyBatis เป็นโครงการโอเพ่นซอร์สของ Apache ในปี 2010 โครงการนี้ถูกย้ายจาก Apache Software Foundation เป็น Google Code และถูกเปลี่ยนชื่อ MyBatis
1. วิศวกรรมย้อนกลับสร้างข้อมูลพื้นฐาน
<? xml version = "1.0" encoding = "utf-8" 1.0 // en "" http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <seneratoronconfiguration> <context id =" testTables "targetRuntime =" MyBatis3 "> <ความคิดเห็น name = "suppressallComments" value = "true"/> </commentGenerator> <!-ข้อมูลการเชื่อมต่อฐานข้อมูล: คลาสไดรเวอร์ที่อยู่การเชื่อมต่อชื่อผู้ใช้รหัสผ่าน-> <jdbcconnection driverclass = "com.mysql.jdbc.driver" jdbc: mysql: // userId = "root" password = "jalja"> </jdbcconnection> <!-ค่าเริ่มต้นเท็จแยกวิเคราะห์ decimal jdbc และตัวเลขเป็นจำนวนเต็มและเมื่อเป็นจริง, decimal และตัวเลขตัวเลข javaty = javatyperver /> </javatyperesolver> <!-โครงการเป้าหมาย: ตำแหน่งของคลาส PO ที่สร้างขึ้น-> <javamodelgenerator TargetPackage = "com.jalja.springmvc_mybatis.model.pojo" targetProject = "./ src" NAME = "ENABLESUBPACKAGES" value = "FALSE" /> <!-ช่องว่างก่อนและหลังค่าที่ส่งคืนจากฐานข้อมูล-> <property name = "trimStrings" value = "true" /> < /javamodelgenerator> <! targetPackage = "com.jalja.springmvc_mybatis.mapper" targetProject = "./ src"> <!-enablesubpackages: ไม่ว่าจะให้สคีมาเป็นคำต่อท้ายของแพ็คเกจ-> <property name = "enablesubpackages แพ็คเกจ-> <property name = "ENABLESUBPACKAGES" value = "FALSE"/> </SQLMAPGENERATORATOR> <!-TargetPackage: ตำแหน่งของการสร้างอินเตอร์เฟส MAPPER-> <JAVACLIENTGENERATOR TYPE = "XMLMAPPER" TargetPRAGAGE = "com.jalja.springMvC ENABLESUBPACKAGES: ไม่ว่าจะให้สคีมาเป็นคำต่อท้ายของแพ็คเกจ-> <ชื่อคุณสมบัติ = "ENABLESUBPACKAGES" value = "false"/> </javaclientGenerator> <!-ระบุตารางฐานข้อมูล-> <table tablename = "รายการ"> </table> tablename = "ผู้ใช้"> </table> </context> </generatorconfiguration> โมฆะคงที่สาธารณะหลัก (สตริง [] arhs) โยนข้อยกเว้น {รายการ <String> คำเตือน = new ArrayList <String> (); บูลีน overWrite = true; configurationParser (คำเตือน); configuration config = cp.parseconfiguration (configfile); defaultshellcallback backback = ใหม่ defaultShellCallback (overWrite); myBatisGenerator myBatisGenerator = new MyBatisGenerator (config, callback, warnings); 2. SpringMVC และ MyBatis รวมไฟล์การกำหนดค่าต่างๆ
1. โครงสร้างโครงการ
2. รหัสหลักของแต่ละไฟล์
A.web.xml
<? xml version = "1.0" encoding = "utf-8"?> <web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" XSI: schemalocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" เวอร์ชัน = "3.0"> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <context-param> <param-name> contextconfiglocation </param-name> <param-value> classpath: Spring/ApplicationContex <Sistener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener> <context-param> <param-name> log4jconfiglocation </param-name> <param-name> log4jrefreshinterval </param-name> <param-value> 3000 </param-value> </context-param> <Sistener> <Sistener-Lass> org.springframework.web.util.log4jconfiglistener Code-> <Filter> <filter-Name> SpringenCodingFilter </filter-name> <filter-class> org.springframework.web.filter.characterencodingFilter </filter-class> <int-param> </init-param> <init-Param> <param-Name> ForceEncoding </param-name> <param-value> True </param-value> </init-param> </filter> <silter-mapping> คอนโทรลเลอร์-> <servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <int-param> <! name-servlet.xml (springmvc-servlet.xml)-> <param-name> contextconfiglocation </param-name> <param-value> classpath: spring/springmvc.xml </param-value> </init-param> <servlet-name> springmvc </servlet-name> <!-1, *.do: dispatcherservlet parses ทั้งหมดเข้าถึงการเข้าถึงที่ลงท้ายด้วย *.do2 /: Dispatcherservlet Parses คำขอทั้งหมด (รวมถึงทรัพยากรแบบคงที่) การกำหนดค่านี้สามารถใช้ URL3 สไตล์ restful,/*: การกำหนดค่านี้ในที่สุดจะถูกส่งต่อไปยังหน้า JSP-> <url-pattern>*. ทำ </url-pattern> </servlet-mapping> <! <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <init-param> <param-name> contextconfiglocation </param-name> <param-value> classpath: สปริง/แอปพลิเคชัน </servlet> <servlet-mapping> <servlet-name> springmvc_rest </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>-> <session-config>
ข. config/mybatis/applicationcontext-mybatis.xml
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! การกำหนดค่า doctype สาธารณะ "-// mybatis.org//dtd config 3.0 // en" "http://mybatis.org/dtd/mybatis-3-config.dtd พารามิเตอร์การรันสามารถปรับได้เมื่อ mybatis ทำงานเช่น: เปิดใช้งานแคชรอง, เปิดใช้งาน typealiases การโหลดล่าช้า (ประเภทนามแฝง): กำหนดพารามิเตอร์ประเภทพารามิเตอร์ประเภทผลลัพธ์ของสายพันธุ์ใน mapper.xml เมื่อส่งคืนประเภทคุณต้องระบุเส้นทางของประเภทและมันไม่สะดวกในการพัฒนา เราจะระบุนามแฝงสำหรับประเภทเหล่านี้ TypeHandler: ใน MyBatis การแปลงประเภท JDBC และประเภท Java เสร็จสมบูรณ์ผ่าน TypeHandler โปรเซสเซอร์ที่จัดทำโดย MyBatis สามารถตอบสนองความต้องการการพัฒนา ObjectFactory: ปลั๊กอิน: สภาพแวดล้อม (วัตถุประสงค์การรวบรวมสิ่งแวดล้อมวัตถุ): สภาพแวดล้อม (วัตถุสิ่งแวดล้อมย่อยวัตถุ): TransactionManager (การจัดการธุรกรรม): DataSource (แหล่งข้อมูล): Mappers (MAPPER):-> <! name = "lazyloadingenabled" value = "true"/> <!-การโหลดที่ใช้งานไปยังการโหลดแบบพาสซีฟ-> <การตั้งค่าชื่อ = "aggressivelazyloading" value = "false"/> <!-เปิดใช้งานแคชระดับ 2-> <การตั้งค่าชื่อ = "cacheenabled" value = "true"/> type = "com.jalja.mybatis.model.user" alias = "ผู้ใช้"/>-> <!-คำจำกัดความของแบทช์นามแฝง mybatis ชื่อคลาสในแพ็คเกจการสแกนอัตโนมัติคือชื่อคลาส name = "com.jalja.springmvc_mybatis.model.custom"/> <package name = "com.jalja.springmvc_mybatis.model.vo"/> </typealiases> <! Resource = "com/jalja/spring_mybatis/mapper/usermapper.xml"/>-> <!-หลังจากรวมกับฤดูใบไม้ผลิคุณสามารถลบ <package name = "com.jalja.spring_mybatis.mapper"/> </mappers>->
ค. config/spring/applicationcontext-dao.xml
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: บริบท = "http://www.springframework.org/schema/contex xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://ww.springframework http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/cache http://ww.springframework http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"><!-- แนะนำไฟล์การกำหนดค่า JDBC-> <บริบท: สถานที่ตั้งสถานที่ตั้งค่า = "classpath: Jdbc.properties"/> <! NAME = "LOCATIONS"> <SILT> <value> classPath: ทรัพยากร/config/jdbc.properties </value> </list> </property> </epean>-> <bean id = "dataSource" destroy-method = "close"> <property = "driverclassname"> name = "url"> <value> $ {jdbc_url} </value> </คุณสมบัติ> <property name = "username"> <value> $ {jdbc_username} </value> </คุณสมบัติ> <property name = "รหัสผ่าน"> name = "maxactive"> <value> 20 </value> </คุณสมบัติ> <!-เริ่มต้นขนาดการเชื่อมต่อ-> <property name = "iminysize"> <dues> 1 </value> </คุณสมบัติ> <! name = "maxidle"> <dange> 20 </alues> </property> <!-พูลการเชื่อมต่อที่ไม่ได้ใช้งานขั้นต่ำ-> <property name = "minidle"> <dance> 3 </value> </property> <!-การเชื่อมต่อที่ไร้ประโยชน์โดยอัตโนมัติ NAME = "RemoveAbandOnedTimeOut"> <value> 180 </value> </property> <!-คุณสมบัติการเชื่อมต่อ-> <property name = "ConnectionProperties"> <value> clientEncoding = UTF-8 </value> </property> </bean> <! ref = "dataSource"/> <property name = "configlocation" value = "classpath: mybatis/applicationcontext-mybatis.xml"/> </ebean> <!-สแกนเนอร์ Mapper-> <bean> <! value = "com.jalja.springmvc_mybatis.mapper"/> <property name = "SQLSessionFactoryBeanName" value = "SQLSessionFactory"/> </ebean> <! ref = "dataSource"> </property> </ebean> <!-การกำหนดค่าการแจ้งเตือนการทำธุรกรรม-> <tx: คำแนะนำ id = "txadvice" ธุรกรรม-ผู้จัดการ = "transactionManager"> <tx: แอตทริบิวต์> <tx: method = "update*" การเผยแพร่ = การแพร่กระจาย = "จำเป็น"/> <tx: method name = "get*" การแพร่กระจาย = "รองรับ" read-only = "true"/> <tx: method name = "find*" การแพร่กระจาย = "สนับสนุน" read-only = "true"/> <tx: แอตทริบิวต์> </tx: คำแนะนำ> < -> <AOP: config> <aop: ที่ปรึกษาคำแนะนำ-ref = "txadvice" pointcut = "การดำเนินการ (*com.jalja.springmvc_mybatis.service.impl.*(.. ))"/> </aop: config> D.Config/Spring/ApplicationContexte-service.xml
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: บริบท = "http://www.springframework.org/schema/contex xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://ww.springframework http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/schema id = "itemtsService"> </ebean> </epeans>
e, config/spring/springmvc.xml
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: aop = "http://www.springframework xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: บริบท = "http://www.springframework.org/schema/contex xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://ww.springframework http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/cache http://ww.springframework Mapper โปรเซสเซอร์-> <!-Mapper org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping Springmvc3.1-> <! org.springframework.web.servlet.mvc.method.annotation.requestmappinghandlermapping Springmvc3.1 หลังจาก-> <!-อะแดปเตอร์ org.springframework.web.servlet.mvc.annotation.annotationmethodhand org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter หลังจาก SpringMVC3.1-> <!-กำหนดค่า Mapper และ Adapter <Bean/> <ถั่ว/>-> <! วิธีนี้โหลดวิธีการผูกพารามิเตอร์จำนวนมากโดยค่าเริ่มต้นเช่น JSON Conversion Parser-> <MVC: คำอธิบายประกอบที่ขับเคลื่อนด้วย/> <!-กำหนดค่าตัวจัดการ <bean/>-> <!-การกำหนดค่าคำอธิบายประกอบขึ้นอยู่กับวิธีการสร้างการสแกน-> พารามิเตอร์พารามิเตอร์ที่กำหนดเอง-> <MVC: คำอธิบายประกอบการแปลงแบบไดรฟ์-ขับเคลื่อน = "ConversionService"/> <bean id = "ConversionService"> <property name = "Converters"> <list> <!-การแปลงประเภทวันที่-> <bean> </epean> การอัปโหลดไฟล์-> <bean id = "MultipartResolver"> <!-ขนาดไฟล์ 5m-> <property name = "MaxuploadSize" value = "5242880"/> </ebean> <! การกำหนดค่าสปริง MVC Interceptor-> <MVC: Interceptors> <MVC: Interceptor> <MVC: เส้นทางการแมป = "/**"/> <bean/> </mvc: interceptor> </mvc: interceptors> <! value = "org.springframework.web.servlet.view.jstlview"/> <ชื่อคุณสมบัติ = "คำนำหน้า" value = "/web-inf/jsp/"/> <property name = "suffix" value = ". jsp"/> </epean>
f, config/jdbc.properties
jdbc_driverclassname = com.mysql.jdbc.driverjdbc_url = jdbc: mysql: // localhost: 3306/mybatis? useunicode = true & areamentencoding = utf-8jdbc_username = rootunic_pass
G, config/log4j.properties
#ในสภาพแวดล้อมการพัฒนาระดับบันทึกควรตั้งค่าเป็น debug สภาพแวดล้อมการสร้างควรตั้งค่าเป็นข้อมูลหรือ errorlog4j.rootlogger = debug stdoutlog4j.logger.org.apache.ibatis = debuglog4j.appender.stdout = org.apache.log4j.consoleapenderlog4j.appender.stdout.layout = org.apache.log4j.patternlayoutlog4j.appender.lay. %m %n
h, com/jalja/springmvc_mybatis/คอนโทรลเลอร์/itemsController.java
แพ็คเกจ com.jalja.springmvc_mybatis.controller; นำเข้า java.io.file; นำเข้า java.util.list; นำเข้า java.util.uuid; นำเข้า org.springframework.beans.factory.annotation.autowired; org.springframework.ui.model; นำเข้า org.springframework.web.bind.annotation.pathvariable; นำเข้า org.springframework.web.bind.annotation.requestbody; org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.bind.annotation.responsebody; com.jalja.springmvc_mybatis.exception.customexception; นำเข้า com.jalja.springmvc_mybatis.model.custom.itemscustom; นำเข้า com.jalja.springmvc_mybatis.service.itemservice.item SpringMVC ให้ตัวแปลงพารามิเตอร์จำนวนมาก*/@คอนโทรลเลอร์@requestmapping ("/items") // แคบคำขอการแม็พคลาสสาธารณะรายการรายการ controller {@autowired itementservice itementservice; @requestmapping (value = "/findItemSlist") itemsList = itemsService.findItemSlist (null); system.out.println (itemsList); model.addattribute ("itemsList", itemsList); return "itemleist";}@requestmapping (value = "/editiTems" พารามิเตอร์การร้องขอที่มีพารามิเตอร์อย่างเป็นทางการต้องใช้: ระบุว่าค่าของแอตทริบิวต์จะต้องส่งผ่านใน defaultValue: ตั้งค่าเริ่มต้นของสตริงสาธารณะ EditItems (โมเดลโมเดล, @RequestParam (value = "id", จำเป็น = true, defaultValue = "0") รายการจำนวนเต็ม) itemsCustom = itemsService.findItemsById (itemsId); ถ้า (itemsCustom = null) {โยน customexception ใหม่ ("ผลิตภัณฑ์ไม่มีอยู่");} model.adDattribute ("itemsCustom", itemScustom); return "edititems"; ID, itemsCustom itemsCustom, multipartFile itemSpic) พ่นข้อยกเว้น {สตริง uploadFileName = itemspic.getoriginalfilename (); // รับชื่อไฟล์ที่อัปโหลดถ้า (itemspic! = null && uploadfilename! = null && ImageSpath = "E: // Develop // upload // images //"; string newfilename = uuid.randomuuid ()+uploadfilename.substring (uploadfilename.lastindexof ("), uploadfilename.length (); ไฟล์ (imageSpath+newfilename); itemspic.transferto (newfile); // เขียนข้อมูลในหน่วยความจำไปยังดิสก์รายการ custom.setpic (newfilename);} itemsservice.updateitemsbyid (id, itemscustom); returect // การเปลี่ยนเส้นทาง} // การใช้ JSON @ResponseBody: เพื่อคัดค้าน json output @requestbody: เพื่อขอพารามิเตอร์ไปยัง java object @requestmapping (value = "/jsonrequest") สาธารณะ @ResponseBody itemstom jsonRequest (@requestbody รายการ การเขียนโปรแกรม/restfulrequest/{id}: มันหมายถึงการผ่านพารามิเตอร์ที่ตำแหน่งนี้ไปยังชื่อที่ระบุโดย @PathVariable @RequestMapping (value = "/restfulRequest/{id}") สาธารณะ @ResponseBody itemsCustom RESTFULREQUEST (@PathVariable ("ID" itemsCustom = itemsService.findItemsById (id); return itemscustom;}}ด้านบนคือรหัสตัวอย่าง MyBatis ที่รวม SpringMVC แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณต้องการทราบข้อมูลเพิ่มเติมโปรดให้ความสนใจกับเว็บไซต์ Wulin.com!