MyBatis هو مشروع مفتوح المصدر من Apache. في عام 2010 ، تم نقل هذا المشروع من مؤسسة Apache Software Foundation إلى Google Code وتم إعادة تسمية MyBatis.
1. الهندسة العكسية تولد المعلومات الأساسية
<؟ 1.0 // en "" http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> <enseratorConfiguration> <context id =" testtables "targetRuntime =" mybatis3 "> <someGenerator> <! value = "true"/> </commentGenerator> <!-معلومات اتصال قاعدة البيانات: فئة السائق ، عنوان الاتصال ، اسم المستخدم ، كلمة المرور-> <jdbcconnection driverClass = "com.mysql.jdbc.driver userId = "root" password = "jalja"> </jdbcconnection> <!-خطأ افتراضي ، قم بتحليل أنواع JDBC العشرية والرقمية إلى عدد صحيح ، وعندما يحل jdbc decimal و numeric إلى java.math.bigdecimal-> <javatyperes> /> </javatyperesolver> <!-TargetProject: موقع فئة PO التي تم إنشاؤها-> <javamodelgenerator targetpackage = "com.jalja.springmvc_mybatis.model.pojo" targetproject = "./ src name = "enblesUBPackages" value = "false" /> <!-يتم تنظيف المساحات قبل وبعد القيمة التي يتم إرجاعها من قاعدة البيانات-> <property name = "trimstrings" value = "true" /> </javamodelgenerator> <!-TargetProject: حيث يتم إنشاء ملف خريطة الخريطة-> <sqlmapgenerator TargetPackage = "com.jalja.springmvc_mybatis.mapper" TargetProject = "./ src"> <!-EnlobesUBPackages: ما إذا كان للسماح لـ Schema أن يكون لاحقة الحزمة-> <property name = "enblesubpackages" value = "false"/> </sqlmaperator. -> <اسم property = "enblesubpackages" value = "false"/> </sqlmapgenerator> <!-TargetPackage: موقع توليد واجهة mapper-> <javaclientgenerator type = " EnlabesUbPackages: سواء للسماح للمخطط يكون لاحقة الحزمة-> <property name = "enlobesUBPackages" value = "false"/> </javaclientgenerator> <!-حدد جداول قاعدة البيانات-> <جدول TablEname = "العناصر"> </table> tablename = "user"> </table> </sultext> </generatorConfiguration> public static void main (string [] arhs) يلقي الاستثناء {list <string> تحذيرات = ArrayList new string> () ؛ boolean overtrite = true ؛ file configfile = new file ("src.main.resources configurationParser (تحذيرات) ؛ التكوين التكوين = cp.parseconfiguration (configfile) ؛ defaultShellCallback Callback = New DefaultShellCallback (الكتابة فوق) ؛ MyBatisGenerator MyBatisGenerator = new MyBatisGenerator (Config ، Callback ، Warnings) ؛ 2. SpringMVC و MyBatis يدمجون ملفات التكوين المختلفة
1. هيكل المشروع
2. الكود الأساسي لكل ملف
A.Web.xml
<؟ xmlns = "http://java.sun.com/xml/ns/javaee" schemalocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" الإصدار = "3.0"> <-live-file-lister> <lisp-file> index.jsp </ishified-file> </iship-file-lister> <context-param> <Param-name> contextConfigLocation </param-name> <Param-value> classpath: spring/applicationContext-*. <StaNerer-Class> org.springframework.web.context.contextLoaderListener </stanterer-class> </leader> <context-param> <param-name> log4jconfiglocation </param-name> <Param-value> classpath: log4j.properties </param-value </ <Param-name> log4jrefreshinterval </param-name> <Param-value> 3000 </param-value> </suctext-param> <stanker> <cleder-class> org.springframework.web.util.log4jconfiGlistener </stanker-class> </stanker> <! رمز-> <iliter> <filter-name> springencodingfilter </filter-name> <ilter-class> org.springframework.web.filter.characterencodingfilter </ilipter-class> <Ing-param> <Param-name> الترميز </param-name> </ith-param> <INIT-PARAM> <PARAM-NAME> FORCENCODING </param-name> <param-value> true </parm-value> </IPT-PARAM> </filter> <filter-mapping> <filter-name> springencodingfilter </filter-name> وحدة تحكم-> <Servlet> <Srevlet-Name> springmvc </servlet-name> <Servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> name-servlet.xml (springmvc-servlet.xml)-> <Param-name> contextConfigLocation </param-name> <param-value> classpath: spring/springmvc.xml </parm-value> </ith-param> <servlet-name> springmvc </servlet-name> <!-1 ، *.do: dispatcherservlet يتوصل جميع الوصول إلى *.do2. /: Dispatcherservlet يموضع جميع الطلبات (بما في ذلك الموارد الثابتة) يمكن لهذا التكوين تطبيق url3 على غرار الراحة ،/*: سيتم إعادة توجيه هذا التكوين في النهاية إلى صفحة JSP-> <url-pattern>*. <Srevlet-Class> org.springframework.web.servlet.dispatcherservlet </isherlet-class> <Ing-param> <Parm-Name> contextConfigLocation </param-name> <baram-value> classpath: spring/applicationcontext-springmvc.xml </param-value> <load-on-startup> 1 </ load-on-startup> </stervlet> <Servlet-mapping> <Servlet-Name> springmvc_rest </servlet-name> <Url-pattern>/</url-pattern> </terdled>-
ب. config/mybatis/applicationContext-mybatis.xml
<؟ يمكن تعديل المعلمات عند تشغيل myBatis ، مثل: تمكين ذاكرة التخزين المؤقت الثانوية ، وتمكين تأخر التحميل المكاعن (نوع الاسم المستعار): تحديد نوع المعلمة نوع المعلمة resulttype في mapper.xml عند إرجاع النوع ، تحتاج إلى تحديد مسار النوع ولا يكون ذلك مناسبًا للتطوير. سنحدد الاسم المستعار لهذه الأنواع. TypeHandler: في MyBatis ، يتم تحويل تحويل نوع JDBC ونوع Java من خلال TypeHandler. يمكن للمعالج الذي توفره MyBatis تلبية متطلبات التطوير ObjectFactory: المكونات الإضافية: البيئات (كائن سمة جمع البيئات): البيئة (كائن البيئة الفرعية): المعاملات (إدارة المعاملات): بيانات البيانات (مصدر البيانات): MAPPER (MAPPER):-> <!-إدارة المعاملات وتكوين تجمعات الاتصال->! name = "lazyloadingEnabled" value = "true"/> <!-التحميل النشط للتحميل السلبي-> <الإعداد name = "aggressivelazyloading" value = "false"/> <!-تمكين المستوى 2 ذاكرة التخزين المؤقت-> <setting name = "cacheenable" type = "com.jalja.mybatis.model.user" alias = "user"/>-> <!-تعريف الاسم المستعار للدفعة myBatis اسم الفئة في حزمة المسح التلقائي هو اسم الفصل (يمكن استخدام كلا الأحرف الأولى والثانية)-> <package name = name = "com.jalja.springmvc_mybatis.model.custom"/> <package name = "com.jalja.springmvc_mybatis.model.vo"/> </typealiases> <!-تحميل ملف التعريف-> <!-<mappers> Resource = "com/jalja/spring_mybatis/mapper/usermapper.xml"/>-> <!-بعد الاندماج مع الربيع ، يمكنك إزالة <package name = "com.jalja.spring_mybatis.mapper"/> </mappers>-> </concepturation>
ج. config/spring/applicationContext-dao.xml
<؟ Xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: context = "http: //www.sp ringframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc " xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3 http://www.springframework.org/schema/aop/spring-aop-3 http://www.springframework.org/schema/context 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.xsdhtp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">< !- تقديم ملف تكوين JDBC-> <contex name = "sortations"> <list> <value> classpath: الموارد/config/jdbc.properties </value> </list> </sprement> </boen>-> <bean id = "dataSource name = "url"> <value> $ {jdbc_url} </value> </sopperation> <property name = "username"> <value> $ {jdbc_username} </value> </propert name = "MaxActive"> <value> 20 </value> </property> <!-تهيئة حجم الاتصال-> <property name = "initialsize"> <value> 1 </value> </sopperation> <!-احصل على الحد الأقصى لوقت الانتظار للاتصال-> <property name = "maxwait"> name = "maxidle"> <value> 20 </value> </property> <!-الحد الأدنى لمجموعة اتصال الخمول-> <property name = "minidle"> <value> 3 </value> </sopperation> <!-توصيل الاتصال غير المجدي تلقائيًا-> <property name = "redovabinoned"> </value> </value> </propert name = "removaBandOnedTimeout"> <value> 180 </value> </sopperation> <!-خصائص الاتصال-> <property name = "connectionProperties"> <value> clientencoding = utf-8 </value> </swalperation> </bean> <! ref = "datasource"/> <property name = "configlocation" value = "classpath: mybatis/applicationContext-mybatis.xml"/> </bean> <!-mapper scanner-> <bean> <! value = "com.jalja.springmvc_mybatis.mapper"/> <property name = "sqlsessionfactorybeanname" value = "sqlsessionfactory"/> </bean> <! ref = "datasource"> </property> </bean> <!-تكوين إشعار المعاملة-> <tx: excloy id = "txAdvice" txadcate-manager = "transactionManager"> <tx: attibutes> <tx: method name = "update*" "sparceling =" tx: الانتشار = "مطلوب"/> <tx: method name = "get*" spection = "support" read-only = "true"/> <tx: method name = "find*" spection = "support" read-only = "true" -> <aop: config> <aop: advisor excloy-res = "txadvice" pointcut = "execution (*com.jalja.springmvc_mybatis.service.impl.*.*. D.Config/Spring/ApplicationContext-service.xml
<؟ Xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: context = "http: //www.sp ringframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc " xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3 http://www.springframework.org/schema/aop/spring-aop-3 http://www.springframework.org/schema/context 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 id = "headssservice"> </ban> </bans>
E ، التكوين/الربيع/springmvc.xml
<؟ Xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: context = "http: //www.sp ringframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc " xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: xsi = "http://www.w3 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context 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.xsdhtp://www.springframework.org/schema/cache http://www.springframework.org/schema/cachemaf> المعالج mapper-> <!-mapper org.springframework.web.servlet.mvc.annotation.defaultannotationHandlermapping springmvc3.1-> <!-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.annotationmethodhandlerderdapter قبل springmvc3.1-> <! org.springframework.web.servlet.mvc.method.annotation.requestmappingHandlerAdapter بعد springmvc3.1-> <!-تكوين Mapper ومحول <bean/>-> <!-قم بتشغيل خريطة التعليقات والمحول. تقوم هذه الطريقة بتحميل العديد من طرق ربط المعلمة بشكل افتراضي ، مثل محلل تحويل JSON-> <MVC: تعتمد التعليقات التوضيحية/> <!-تكوين معالج <bean/>-> <!-يعتمد تكوين التعليقات التوضيحية على طريقة بناء المسح-> <context: component-scan package = "com.jalja Parmeter Parser-> <mvc: خدمة التحويل التي تعتمد على التعليقات التوضيحية = "conversionservice"/> <bean id = "conversionservice"> <property name = "converters"> <list> <! id = "multipartresolver"> <!-حجم الملف 5M-> <property name = "maxuploadsize" value = "5242880"/> </bean> <!-مشكلة الوصول إلى الموارد الثابتة التي تسببها البرمجة باستخدام نمط Restful-> <! -> <mvc: interceptors> <mvc: interceptor> <mvc: mapping path = "/**"/> <bean/> </mvc: interceptor> </mvc: interceptors> <!-عرض الخريطة jsp استخدام jstl-> <! value = "org.springframework.web.servlet.view.jstlview"/> <property name = "prefix" value = "/web-inf/jsp/"/> <propert
f ، config/jdbc.properties
jdbc_driverclassname = com.mysql.jdbc.driverjdbc_url = jdbc: mysql: // localhost: 3306/mybatis؟ useUnicode = true & paractionoding = utf-8jdbc_username = rootjdbc_password = 111111
G ، config/log4j.properties
#في بيئة التطوير ، يجب تعيين مستوى السجل على تصحيح الأخطاء ، وينبغي تعيين بيئة التوليد على المعلومات أو errorlog4j.rootlogger = debug ، stdoutlog4j.logger.org.apache.ibatis = debuglog4j.appender.stdout = org.apache.log4j.consoleAppenderLog4j.appender.stdout.layout = org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.convers
H ، com/jalja/springmvc_mybatis/controller/headscontroller.java
package com.jalja.springmvc_mybatis.controller ؛ استيراد java.io.file ؛ استيراد java.util.list ؛ استيراد java.util.uuid ؛ import org.springframework.beans.factory.annotation.autowired ؛ importpringframework.sterepypeoTypeOty.Controller ؛ org.springframework.ui.model ؛ استيراد org.springframework.web.bind.annotation.pathvariable ؛ استيراد org.springframework.web.bind.annotation.requestbody ؛ استيراد org.springframework.web.bind.annotation.requestmapping ؛ 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 العديد من محولات المعلمة*/@controller@requestmapping ("/items") // طلب طلب request تعيين عناصر الفئة العامة controller {autowired itemsservice severvice ؛@requestmapping (value = "/findItemSlist") public findItemSlist (نموذج الطراز) استثناء {list <estemcustom> itemslist = headsservice.finditemslist (null) ؛ system.out.println (headlistlist) ؛ model.addattribute ("headlistlist" ، heatlist) ؛ return "heatlist" ؛}@requestmapp المعلمات الرسمية المطلوبة: حدد أن قيمة السمة يجب أن يتم تمريرها في DefaultValue: قم بتعيين القيمة الافتراضية للسلسلة العامة EditItems (نموذج النموذج ، requestparam (value = "id" ، مطلوب = true ، defaultValue = "0") INTEGER under) استثناء {heatscustom heatscustom = headssservice.findItemSbyId (عناصر) ؛ if (headscustom = null) {رمي customexception جديد ("المنتج غير موجود") ؛} model.addattribute ("heatscustom" ، itemscustom) heatscustom ، multipartfile itempic) يلقي الاستثناء {string eploadFiLename = headpic.getoriginalfilename () ؛ // الحصول على اسم الملف المحمّل if (عناصر! = null && eploadfilename! = null &&! uploadfilename.equals ("")) newFilename = uuid.randomuuid ()+uploadFilename.subString (UploadFilename.lastIndexof (".") ، UploadFilename.length ()) heatscustom.setpic (newFilename) ؛} sexsservice.updateItemSbyid (id ، heatscustom) ؛ إرجاع "إعادة التوجيه: findItemSlist.do" ؛ . البرمجة/RESTULERQUEST/{id}: يعني تمرير المعلمة في هذا الموضع إلى الاسم المحدد بواسطة pathvariable @requestmapping (value = "/restfulrequest/{id}") public responsebody undercustom restfulrequest ( @pathvariable ("id" id) inte id) headscustom = headsservice.finditemsbyid (id) ؛ return headscustom ؛}}ما سبق هو رمز مثال MyBatis المدمج springMVC الذي قدمه لك المحرر. آمل أن يكون ذلك مفيدًا لك. إذا كنت تريد معرفة المزيد من المعلومات ، فيرجى الانتباه إلى موقع Wulin.com!