Spring Boot เป็นสิ่งใหม่สำหรับฉัน ในระหว่างกระบวนการเรียนรู้ของฉันฉันพบว่าสิ่งนี้ยังคงเริ่มต้นได้ง่าย เมื่อไม่มีการกำหนดค่าสปริงบูตจะใช้สปริงข้อมูล JPA โดยค่าเริ่มต้น สิ่งนี้สามารถกล่าวได้ว่าเป็นเครื่องมือที่ง่ายมาก แต่ฉันก็ยังชอบใช้ mybatis ไม่มีเครื่องมือที่ดีที่สุดเพียงอันนี้เหมาะสำหรับฉัน
เมื่อไม่นานมานี้เมื่อพูดถึง MyBatis มีเครื่องมือที่มีประโยชน์มาก - MyBatis -Plus (เว็บไซต์ทางการ) เวอร์ชันที่อัปเดตคือ 2.1.2 และรุ่นนี้ยังใช้ที่นี่ คุณสมบัติที่ฉันชอบคือเครื่องกำเนิดรหัสและตัวสร้างแบบมีเงื่อนไขเพื่อให้สามารถพัฒนาได้ง่ายขึ้น
มีตัวอย่างการบูตฤดูใบไม้ผลิบนเว็บไซต์ทางการของ MyBatisPlus ฉันติดตามมันและโปรแกรมไม่ทำงาน ต่อมาฉันพบว่าฐานข้อมูล H2 ที่ใช้โดยการสาธิตนั้นไม่เหมือนกับ MySQL ดังนั้นหากคุณต้องการรวม MyBatisPlus คุณไม่สามารถดูเว็บไซต์อย่างเป็นทางการและหลีกเลี่ยงการออกนอกเส้นทาง
ต่อไปนี้เป็นกระบวนการรวม
1. ก่อนอื่นคุณต้องได้รับไฟล์ JAR ที่จำเป็นทั้งหมด สิ่งที่คุณต้องการสำหรับ pom.xml มีดังนี้
pom.xml (ไม่สมบูรณ์)
<!-mybatis-plus เริ่มต้น-> <การพึ่งพา> <roupid> com.baomidou </groupid> <ratifactid> mybatisplus-spring-boot-starter </artifactid> <persion> 1.0.4 </เวอร์ชัน> </การพึ่งพา> <Sersion> 2.1.2 </เวอร์ชัน> </การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-starter-jdbc </artifactid> </การพึ่งพา> <! <roupId> com.alibaba </groupid> <ratifactid> druid </artifactid> <cersion> 1.1.3 </เวอร์ชัน> </การพึ่งพาอาศัยกัน> <!-mysql-> <cendency> <roupid> mysql </groupid> <RoupID> org.mybatis.spring.boot </groupId> <ratifactid> mybatis-spring-boot-starter </artifactid> <version> 1.3.1 </version> </derctency>
2. เพิ่มการกำหนดค่าที่เกี่ยวข้องกับ MyBatis เช่นบัญชีรหัสผ่าน ฯลฯ ที่นี่ฉันใช้ Application.yml เพื่อจับคู่
application.yml
เซิร์ฟเวอร์: พอร์ต: 8080#Springspring: Devtools: รีสตาร์ท: เปิดใช้งาน: จริง#นี่คือสำหรับการปรับใช้ที่ร้อนแรงและไม่เกี่ยวข้องกับ mybatis#ฐานข้อมูลกำหนดค่าฐานข้อมูล: Driver-class-name: com.mysql.jdbc.driver ชื่อผู้ใช้ JDBC: MySQL: // MySQLDB: 3306/TDX_SHOP? useUnicode = true & characterencoding = UTF-8 ประเภท: com.alibaba.druid.pool.druiddataSource #her กำลังกำหนดค่าพูลเชื่อมต่อ Druid ข้อมูลต่อไปนี้เป็นข้อมูลการกำหนดค่าทั้งหมดสำหรับตัวกรอง Druid: Stat, Wall, Log4j Maxactive: 20 ค่าเริ่มต้น: 1 Maxwait: 60000 minidle: 1 TimebetweenevictionRunsmillis: 60000 minevictableidletimeLis: 300000 การตรวจสอบ: Select MaxOpenPreparedStatements: 20 การเชื่อมต่อ-properties: druid.stat.merggsql = ture; druid.stat.slowsqlmillis = 5000 #mybatismybatis: mapper-locations: classpath*/mapper /* mapper.xml #put นี่คือ Mapper ภายใต้ทรัพยากรการสแกน #ENTITY แพ็คเกจหลายแพ็คเกจจะถูกคั่นด้วยเครื่องหมายจุลภาคหรือเครื่องหมายอัฒภาค TypeAliasEspackage: com.tdx.account_service.entity #นี่คือตำแหน่งของการกำหนดค่าคลาสเอนทิตี: Map-underscore-to-camel-case: TRUE Cache-enabled: false #loggingLogging: ระดับ: WARN
สิ่งที่กำหนดค่าคล้ายกับสิ่งที่เราใช้ในการกำหนดค่ากับ mybatis แต่ Spring Boot ไม่มีไฟล์การกำหนดค่า XML ให้ความสนใจกับเนื้อหาของตัวอักษรสีแดงโดยทั่วไปไม่มีปัญหา
3. ไฟล์กำหนดค่า MyBatis-Plus ------ MyBatisPlusConfig ก่อนอื่นให้อธิบายเส้นทางไฟล์ในรูปด้านบน ในหมู่พวกเขา MyBatisPlusConfig ถูกวางไว้ในโฟลเดอร์ config ในขณะที่ไฟล์ XML ถูกวางไว้ใน Mapper ภายใต้ resouces
จากนั้นก็มีส่วนเนื้อหา mybatisplusconfig
mybatisproperties.java
แพ็คเกจ com.tdx.account_service.config; นำเข้า com.alibaba.druid.pool.druiddatasource; นำเข้า com.alibaba.druid.support.http.statviewservlet; com.baomidou.mybatisplus.mybatisconfiguration; นำเข้า com.baomidou.mybatisplus.mybatisconfiguration; นำเข้า com.baomidou.mybatisplus.entity.globalconfiguration; com.baomidou.mybatisplus.plugins.paginationInterceptor; นำเข้า com.baomidou.mybatisplus.plugins.performanceInterceptor; นำเข้า com.baomidou.mybatisplus.plugins.parser.isqlparser; com.baomidou.mybatisplus.plugins.parser.isqlparserfilter; นำเข้า com.baomidou.mybatisplus.plugins.parser.Tenant.Tenanthandler com.baomidou.mybatisplus.spring.mybatissqlsessionfactorybean; นำเข้า com.baomidou.mybatisplus.spring.boot.starter.springbootvfs; นำเข้า com.baomidou.mybatisplus.toolkit. net.sf.jsqlparser.expression.expression; นำเข้า net.sf.jsqlparser.expression.longvalue; นำเข้า org.apache.ibatis.mapping.databaseidprovider; นำเข้า org.apache.ibatis.mapping.mappeds org.apache.ibatis.reflection.metaobject; นำเข้า org.mybatis.spring.annotation.mapperscan; นำเข้า org.mybatis.spring.boot.autoconfigure.mybatisproperties; org.springframework.boot.bind.relaxedPropertyResolver; นำเข้า org.springframework.boot.context.properties.enableConfigurationProperties นำเข้า org.springframework.boot.web.servlet.filterterytersteration org.springframework.boot.web.servlet.servletregistrationbean นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; org.springframework.core.io.defaultresourceloader นำเข้า org.springframework.core.io.resourceloader; นำเข้า org.springframework.util.objectutils; นำเข้า org.springframework.util.stringutils; java.sql.sqlexception; นำเข้า java.util.arraylist; นำเข้า java.util.list;/** * รหัสอยู่ห่างจากบั๊กด้วยการปกป้องสัตว์ * ┏┓ * ┃ * ┃ * * ┃อวยพรสัตว์ร้าย * ┃┃┃┃┗┓┗┓┏┛┗┓┏┛┏┛┏┛┃┃┃┃┃┃┃┃ * ┃ * ┃ * ┃ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ * ┗┻┛ * * * @description: การกำหนดค่า mybatisplus * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Private RelaxedPropertyResolver PropertyResolver; @autowired DataSource DataSource; @autowired Private MyBatisProperties คุณสมบัติ; @AutoWired ResourceLoader ResourceLoader = ใหม่ defaultresourceLoader (); @autowired (จำเป็น = false) interceptor ส่วนตัว [] interceptors; @AutoWired (จำเป็น = false) DatabaseIdProvider ส่วนตัว DatabaseIdProvider; /** * @description: ปลั๊กอินประสิทธิภาพการทำงานของ MyBatis-Plus SQL [สามารถปิดในสภาพแวดล้อมการผลิต] * - */ @Bean Public DataSource DruidDataSource () พ่น sqlexception {this.propertyResolver = ใหม่ RelaxedPropertyResolver (สภาพแวดล้อม, "Spring.Datasource."); System.out.println ("================================================================================================================================== - - - DataSource.seturl (PropertyResolver.getProperty ("URL")); DataSource.setPassword (PropertyResolver.getProperty ("รหัสผ่าน")); DataSource.SetMaxWait (Long.ValueOf (PropertyResolver.getProperty ("Max-Wait"))); DataSource.SetMineVictableIdletImeLis (Long.valueof (PropertyResolver.getProperty ("Min-Evictable-Idle-Time-Millis"))); DataSource;} /*** @description: mybatis-plus pagination ปลั๊กอิน* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PaginationInterceptor PaginationInterceptor () {PaginationInterceptor Page = ใหม่ PaginationInterceptor (); MyBatissQlSessionFactoryBean MyBatissQlSessionFactoryBean () {MyBatissQlSessionFactoryBean MyBatisplus = ใหม่ MyBatissQlSessionFactoryBean (); (Stringutils.hastext (this.properties.getConfiglocation ())) {mybatisplus.setconfiglocation (this.resourceloader.getResource (this.properties.getConfiglocation ()); (! ObjectUtils.iseMpty (this.interceptors)) {mybatisplus.setplugins (this.interceptors); Auto-> `0` (" ID ฐานข้อมูลการเพิ่มขึ้นของตัวเอง ") input->` 1` (ID อินพุตผู้ใช้ ") ID_Worker->` 2` ("Global Unique ID") UUID-> `3` (" Global Unique ID ") GlobalConfig.SetIdType (2); MyBatisconfiguration (); (stringutils.haslength (this.properties.getTypealiasEspackage ()))) {myBatisplus.setTypealiasEspackage (this.properties.getTypealiasEspackage ()); } if (stringutils.haslength (this.properties.getTypealiasEspackage ());} ถ้า (stringutils.haslength (this.properties.getTypehandlerspackage ())) (! ObjectUtils.isEmpty (this.properties.resolvemapperlocations ())) {mybatisplus.setmapperlocations (นี่ druidstatviewservle () {//org.springframework.boot.context.embedded.servletRegistrationBean ให้ชั้นเรียนสำหรับการลงทะเบียน // servletregistrationbean.addinitparameter ("อนุญาต", "127.0.0.1"); // การเข้าสู่ระบบของบัญชีเพื่อดูข้อมูล } ** * ลงทะเบียนหนึ่ง: FilterRegistrationBean * * @return */@Bean Public FilterRegistrationBean DruidStatFilter () {FilterRegistrationBean FilterRegistrationBean = FilterStrationBean ใหม่ ไม่จำเป็นต้องถูกเพิกเฉยนี่คือไฟล์การกำหนดค่าที่สมบูรณ์ เป็นสิ่งสำคัญที่จะต้องทราบว่าแพ็คเกจที่แนะนำไม่ถูกต้อง!
4. คุณต้องเปิดใช้งานการสแกน DAO มันง่ายมาก เพิ่ม @mapperscan ("com.tdx.account_service.dao*") ไปยังไฟล์เริ่มต้น ต่อไปนี้เป็นสิ่งที่สมบูรณ์
ณ จุดนี้การกำหนดค่าเสร็จสมบูรณ์และคุณสามารถเรียกใช้โครงการหลังจากเรียกใช้
ฉันคิดว่า mybatis-plus เป็นส่วนที่สนุกที่สุดของตัวสร้างรหัส ต่อไปนี้เป็นกระบวนการของการใช้เครื่องกำเนิดรหัส
เพิ่มบางอย่างลงใน pom.xml
<การพึ่งพา> <roupId> org.apache.velocity </groupId> <ratifactid> Velocity-Engine-Core </artifactid> <version> 2.0 </version> </derness>
1. ไฟล์การกำหนดค่าตัวสร้างรหัส
mybatisplusconfig.java
/*** ลิขสิทธิ์ (c) 2011-2016, Hubin ([email protected]) * <p> * ได้รับอนุญาตภายใต้ใบอนุญาต Apache เวอร์ชัน 2.0 ("ใบอนุญาต"); คุณไม่สามารถใช้ไฟล์นี้ได้ยกเว้นตามใบอนุญาต คุณอาจได้รับสำเนาของ * ใบอนุญาตที่ * <p> * http://www.apache.org/licenses/license-2.0 * <p> * เว้นแต่ว่ากฎหมายที่บังคับใช้หรือตกลงที่จะเป็นลายลักษณ์อักษรซอฟต์แวร์ * แจกจ่ายภายใต้ใบอนุญาตจะถูกแจกจ่ายบน "ตาม" ดูใบอนุญาต * สำหรับภาษาเฉพาะที่ควบคุมการอนุญาตและข้อ จำกัด ภายใต้ * ใบอนุญาต */แพ็คเกจ com.tdx.account_service.generator; นำเข้า java.io.file; นำเข้า java.util.arraylist; นำเข้า java.util.collections; นำเข้า java.util.hashmap; นำเข้า java.util.list; com.baomidou.mybatisplus.generator.autogenerator; นำเข้า com.baomidou.mybatisplus.generator.injectconfig; นำเข้า com.baomidou.mybatisplus.generator.config.datasourceConfig; com.baomidou.mybatisplus.generator.config.fileoutconfig; นำเข้า com.baomidou.mybatisplus.generator.config.globalconfig; นำเข้า com.baomidou.mybatisplus.generator.config.packagaggig com.baomidou.mybatisplus.generator.config.strategyconfig; นำเข้า com.baomidou.mybatisplus.generator.config.templateconfig; นำเข้า com.baomidou.mybatisplus.generator.config.converts.converts.converts com.baomidou.mybatisplus.generator.config.po.tablefill นำเข้า com.baomidou.mybatisplus.generator.config.po.tableinfo; นำเข้า com.baomidou.mybatisplus.generator.config.coles com.baomidou.mybatisplus.generator.config.rules.dbtype นำเข้า com.baomidou.mybatisplus.generator.config.rules.namingstrategy;/** *รหัสอยู่ห่างไกลจากบั๊กที่มีการปกป้องจากสัตว์ *┃ *┃ *┗━┓ *┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃┃ * ┃ * ┃ * ┃ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ * ┗┻┛ * * * @description: mybatisplus code generator * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // พา ธ ไฟล์สตริงคงที่ส่วนตัว uthorName = "liang.guangqing"; // ผู้เขียนตารางสตริงคงที่ส่วนตัว = "SC_USER"; // ชื่อตาราง private Static String คำนำหน้า = "SC_"; // ตารางคำนำหน้าไฟล์ไฟล์คงที่ส่วนตัวไฟล์ = ไฟล์ใหม่ (packagename); Private Static String Path = file.getAbsolutePath (); โมฆะคงที่สาธารณะหลัก (สตริง [] args) {// ปรับแต่งฟิลด์ที่ต้องกรอกรายการ <TableFill> tableFillList = arrayList ใหม่ <> (); tablefilllist.add (Tablefill ใหม่ ("ASDD_SS", FieldFill.INSERT_UPDATE)); // code generator autogenerator mpg = new Autogenerator (). setGlobalConfig (// การกำหนดค่าทั่วโลกใหม่ GlobalConfig () .SetOutputDir (เส้นทาง+"/src/main/java") // output.setFileOverride (true) // XML CACHE CACHE.SETBASERESULTMAP (จริง) // XML RESVOMSMAP .SetBasEcolumnList (จริง) // XML คอลัมน์. SetOpen (เท็จ) // เปิดโฟลเดอร์หลังการสร้าง Setauthor (Authorname) // ปรับแต่งชื่อไฟล์ .SetXMlName ("%smapper") .SetServicEname ("%sservice") .SetServiceImplName ("%sserviceimpl") .setControllerName ("%scontroller") type.setTypeconvert (ใหม่ mysqltypeconvert () {// ปรับแต่งฐานข้อมูลตารางการแปลงประเภทการแปลงประเภท [ไม่บังคับ] @Override สาธารณะ dbColumnType processTypeconvert (String fieldtype) {System.out.println ("ประเภทการแปลง:" // ส่งคืน dbcolumntype.boolean; .SetUrl ("JDBC: MySQL: //127.0.0.1: 3306/tdx_shop? characterencoding = utf8") .setstrategy (// การกำหนดค่านโยบายใหม่ strategyconfig () // Naming.settablePrefix (สตริงใหม่ [] {คำนำหน้า}) // ที่นี่คุณสามารถแก้ไขได้ในคำนำหน้าตารางของคุณการตั้งชื่อ (namingstrategy.underline_to_camel) // กลยุทธ์การสร้างชื่อ Table.setinclude (สตริงใหม่ [] {table}) // ตารางที่ต้องการ String [] {"Test"}) // ยกเว้นตารางที่สร้างขึ้น // custom entity parent class // .setsuperentityclass ("com.baomidou.demo.testentity") // เอนทิตีที่กำหนดเองสนามสาธารณะ //.setsuperentityColumn .SetSuperMapperclass ("com.baomidou.demo.testmapper") // บริการผู้ให้บริการที่กำหนดเองชั้นเรียน // .setsuperserviceClass ("com.baomidou.demo.testservice" // การใช้บริการที่กำหนดเอง คอนโทรลเลอร์คลาส PARTER.SETSUPERCONTROLLERCLASS ("com.tdx."+packagename+". controller.AbstractController") // [เอนทิตี] ไม่ว่าจะเป็นค่าคงที่ของฟิลด์ {this.name = name; return this;} // .setentitybuildermodel (true) // [entity] เป็นรุ่น Lombok (ค่าเริ่มต้นที่ผิดพลาด) <a href = "https://projectlombok.org/" rel = "extremental nofollow" // .SetEntityBoolEanColumnRemoveIsPrefix (จริง) // .SetRestControllerstyle (จริง) // .SetControllerMappinghyphenstyle (จริง)) .SetPackageInfo (// packageConfig () // .SetModulename ("ผู้ใช้"). path.setController ("คอนโทรลเลอร์") // นี่คือชื่อแพ็คเกจคอนโทรลเลอร์, เว็บเริ่มต้น. setEntity ("เอนทิตี") .setMapper ("dao") .setService ("บริการ") .SetServiceimpl ("service.impl") //.setxml("Mapper ")) InjectionConfig () {@Override โมฆะสาธารณะ initmap () {แผนที่ <สตริงวัตถุ> แผนที่ = ใหม่ hashmap <> (); } .SetFileOutConfigList (คอลเลกชัน <FileOutConfig> SingletonList (ใหม่ fileoutConfig ("/templates/mapper.xml.vm") {// ไดเรกทอรีไฟล์เอาท์พุทที่กำหนดเอง "mapper.xml";}}))) .SetTemplate (// ปิดการสร้าง XML เริ่มต้นและปรับการสร้างให้เป็นรูทไดเรกทอรีใหม่ templateconfig (). setxml (null) // ปรับแต่งการกำหนดค่าเทมเพลต/ทรัพยากร/ทรัพยากร ไดเรกทอรี // ดำเนินการเพื่อสร้าง mpg.execute (); // การตั้งค่าการฉีดพิมพ์ที่นี่แสดงให้เห็นถึงวิธีการรับเนื้อหาการฉีดในเทมเพลต [Noble] System.err.println (mpg.getcfg (). getMap (). รับ ("abc")); -
ยังมีการปรับเปลี่ยนจำนวนมากในไฟล์ สิ่งที่สำคัญที่สุดคือข้อมูลการเชื่อมต่อของ MySQL ไม่มีเหตุผลว่าทำไมบัญชีและรหัสผ่านของคุณจึงผิด คุณสามารถเชื่อมต่อกับมัน ประการที่สองตั้งค่าเส้นทางของไฟล์เทมเพลตที่คุณสร้าง เส้นทางที่ฉันสร้างขึ้นที่นี่สามารถเห็นได้ในรูปด้านบน อยู่ภายใต้ com.tdx.account_service โปรดทราบว่าควรวางไฟล์ XML ไว้ใต้ทรัพยากรมิฉะนั้นจะได้รับการยอมรับและกล่าวว่าวิธีนี้ไม่สามารถพบได้
ไฟล์ที่สร้างขึ้นตามเทมเพลตรหัสของเว็บไซต์อย่างเป็นทางการนั้นว่างเปล่า เหตุผลหลักคือ MyBatis-Plus รวมวิธีการสาธารณะและเครื่องมือที่ใช้กันทั่วไปจำนวนมากสามารถอ้างอิงได้ ที่นี่ฉันให้ไฟล์ controller.java.vm ที่ฉันแก้ไขส่วนใหญ่เป็นไปตามนิสัยของฉันเอง
controller.java.vm
แพ็คเกจ $ {package.controller}; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestparam; #if ($ {restcontrollerstyle}) org.springframework.web.bind.annotation.restcontroller; #elseimport org.springframework.steretype.controller;#end#ถ้า ($ {supercontrollerclasspackage}) นำเข้า $ {supercontrollerclasspackage};#endimport org.springframework.beans.factory.annotation.autowired; นำเข้า com.baomidou.mybatisplus.mapper.entitywrapper; นำเข้า com.baomidou.mybatisplus.plugins.page; $ {package.entity} .common.datatablesjson; นำเข้า $ {package.entity} .common.jsonresult; นำเข้า $ {package.entity}. $ {เอนทิตี}; นำเข้า org.slf4j.logger ┃ *┃┗┳┃ *┃ *┃ *┃ *┃ *┃ *┃ *┃ *┃ *┃ *┗┓ *┗┓ *┃┃┃┃┃┃┃┃┃ * ┃ * ┃ * ┃ * ┗┓┓┏━┳┓┏┛ * ┃┫┫┃┫ * ┗┻┛ * * * @description: $ {เอนทิตี} คอนโทรลเลอร์ * - */#ถ้า ($ {RestControllerStyle})@restController#else@controller#end@requestmapping ("#ถ้า ($ {package.modulename})/$ {package.modulename}# end/#ถ้า ($ {ControllerMappingHyphenstyle}) $ {controlerMappinghyphen}#else $ {table.entitypath} #end ")#ถ้า ($ {supercontrollerclass}) สาธารณะ คลาส $ {table.controllerName} ขยาย $ {SuperControllerClass} {#ELESEPBUBLIC คลาส $ {table.ControllerName} {#end ส่วนตัว Logger สุดท้าย Logger = loggerFactory.getLogger ($ {table.ControllerName} .class); @autowired สาธารณะ $ {table.servicename} $ {table.entitypath} บริการ; /*** @description: รับรายการเพจ* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- รับ $ {เอนทิตี} รายการ ($ {entity} param, @requestparam (value = "draw", defaultValue = "0") การวาดจำนวนเต็ม, @RequestParam (value = "length") ความยาวจำนวนเต็ม @requestParam (value = "start") จำนวนเต็ม) ลอง {จำนวนเต็ม pageno = getPageno (เริ่มต้นความยาว); หน้า <$ {เอนทิตี}> หน้า = หน้าใหม่ <$ {เอนทิตี}> (pageno, ความยาว); $ {table.EntityPath} service.selectPage (หน้า, entityWrapper ใหม่ <$ {Entity}> (param)); resjson.setDraw (draw ++); resjson.setRecordStotal (page.getTotal ()); resjson.setRecordsfiltered (page.getTotal ()); resjson.setData (page.getRecords ()); resjson.setsuccess (จริง); } catch (exception e) {resjson.setsuccess (เท็จ); resjson.setError ("ข้อมูลข้อยกเว้น: {"+e.getClass (). getName ()+"}"); logger.info ("ข้อมูลข้อยกเว้น: {}"+e.getMessage ()); } return resjson; } /** * @description: รับ $ {entity} โดย id * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ id) {jsonResult <$ {entity}> resjson = new JsonResult <> (); ลอง {$ {entity} param = $ {table.entitypath} service.selectById (id); resjson.setData (param); resjson.setsuccess (จริง); } catch (exception e) {resjson.setsuccess (เท็จ); resjson.setMessage ("ข้อมูลข้อยกเว้น: {"+e.getClass (). getName ()+"}"); logger.info ("ข้อมูลข้อยกเว้น: {}"+e.getMessage ()); } return resjson; } /** * @description: ลบ $ {entity} โดย id * - JsonResult <> (); ลอง {resjson.setsuccess ($ {table.entitypath} service.deleteById (id)); } catch (exception e) {resjson.setsuccess (เท็จ); resjson.setMessage ("ข้อมูลข้อยกเว้น: {"+e.getClass (). getName ()+"}"); logger.info ("ข้อมูลข้อยกเว้น: {}"+e.getMessage ()); } return resjson; } /** * @description: อัปเดต $ {Entity} โดย ID * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ $ {วันที่} */ @requestmapping (value = "/ update $ {entity} byid", method = requestMethod.post) วัตถุสาธารณะอัปเดต $ {เอนทิตี} byid ($ {entity} param) {jsonResult <$ {entity}> resjson = new ลอง {resjson.setsuccess ($ {table.entitypath} service.updateById (param)); } catch (exception e) {resjson.setsuccess (เท็จ); resjson.setMessage ("ข้อมูลข้อยกเว้น: {"+e.getClass (). getName ()+"}"); logger.info ("ข้อมูลข้อยกเว้น: {}"+e.getMessage ()); } return resjson; } /** * @description: เพิ่ม $ {entity} * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ jsonResult <$ {entity}> resjson = new JsonResult <> (); ลอง {resjson.setsuccess ($ {table.entitypath} service.insert (param)); } catch (exception e) {resjson.setsuccess (เท็จ); resjson.setMessage ("ข้อมูลข้อยกเว้น: {"+e.getClass (). getName ()+"}"); logger.info ("ข้อมูลข้อยกเว้น: {}"+e.getMessage ()); } return resjson; -นอกเหนือจากไฟล์นี้ฉันติดตามเว็บไซต์อย่างเป็นทางการของเทมเพลตรหัสอื่น ๆ ที่นี่ฉันอ้างถึง 3 ไฟล์ภายนอกรวมถึง AbstractController.java (คลาสฐานคอนโทรลเลอร์), dataTablesjson.java และ jsonresult.java จากนั้นไฟล์เหล่านี้อ้างถึงไฟล์อื่น ๆ ฉันสับสน ทำไมมีไฟล์มากมาย? ดูเหมือนว่าถ้าพวกเขาทั้งหมดถูกโพสต์มันจะใช้พื้นที่มากขึ้น ดังนั้นฉันเลือกที่จะเชื่อในรหัสคลาวด์ หากคุณต้องการโปรดดาวน์โหลดรหัสคลาวด์ตามไฟล์ไม่กี่ไฟล์บนรหัสคลาวด์ ควรสังเกตว่าเทมเพลตของฉันถูกแทนที่โดยตรงด้วยไดเรกทอรีเทมเพลตใน mybatis-plus.jar
ในที่สุดก็เป็นกระบวนการทดสอบ
ตัวอย่างโค้ดต่อไปนี้จะถูกวางไว้ในไฟล์คอนโทรลเลอร์จากนั้นโปรแกรมจะเริ่มขึ้นซึ่งสอดคล้องกับพอร์ตและวิธีการขอ สำหรับการทดสอบคุณต้องใช้งานตามคลาสเอนทิตีของคุณเองดังนั้นจึงเป็นการอ้างอิงเท่านั้น
/** * หน้า pagination */@getMapping ("/test") หน้าสาธารณะ <ผู้ใช้> ทดสอบ () {return userservice.selectPage (หน้าใหม่ <user> (0, 12)); }/*** การทดสอบบางส่วน*/@getMapping ("/test1") หน้าสาธารณะ <user> test1 () {ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); System.err.println ("ลบทั้งหมด:" + user.delete (null)); //user.setid(2017091801L); user.setAccout ("ทดสอบ"+num ++); user.setType ("ทดสอบ"); user.setCreateTime (วันที่ใหม่ ()); user.setphone ("13111110000"); user.setPassword ("123456"); user.setNickName ("guangqing"+2*num ++); user.Insert (); System.err.println ("Query แทรกผล:" + user.selectbyid (). toString ()); //user.setNickName("MyBatis-plus-ar "); System.err.println ("อัปเดต:" + user.updateById ()); return user.selectPage (หน้าใหม่ <user> (0, 12), null); }/** * เพิ่มลบแก้ไขและตรวจสอบ crud */@getMapping ("/test2") ผู้ใช้สาธารณะ test2 () {ผู้ใช้ผู้ใช้ = ผู้ใช้ใหม่ (); user.setId (123456L); user.setAccout ("ทดสอบ"); user.setType ("ทดสอบ"); user.setCreateTime (วันที่ใหม่ ()); user.setphone ("13111110000"); user.setPassword ("123456"); user.setNickName ("guangqing"); System.err.println ("ลบข้อมูล:" + Userservice.deleteById (1L)); System.err.println ("แทรกข้อมูล:" + userservice.insert (ผู้ใช้)); ผู้ใช้ผู้ใช้ 2 = ผู้ใช้ใหม่ (); user.setId (223456L); user.setAccout ("test2"); user.setType ("ทดสอบ"); user.setCreateTime (วันที่ใหม่ ()); user.setphone ("13111110000"); user.setPassword ("123456"); user.setNickName ("guangqing"); ผลลัพธ์บูลีน = userservice.insert (ผู้ใช้); // id writeback อัตโนมัติ id long id = user.getId (); System.err.println ("แทรกชิ้นส่วนของข้อมูล:" + ผลลัพธ์ + ", แทรกข้อมูล:" + user.toString ()); System.err.println ("Query:" + Userservice.SelectById (ID) .toString ()); Page <user> userlistpage = UserserVice.SelectPage (หน้าใหม่ <user> (1, 5), EntityWrapper ใหม่ <> (ผู้ใช้ใหม่ ())); System.err.println ("total =" + userlistpage.getTotal () + ", ขนาดรายการปัจจุบัน =" + userlistpage.getRecords (). size ()); return userservice.selectbyid (1L); } @getMapping ("testSelect") วัตถุสาธารณะ testSelect () {จำนวนเต็มเริ่มต้น = 0; ความยาวจำนวนเต็ม = 10; param ผู้ใช้ = ผู้ใช้ใหม่ (); //param.setNickName("guangqing2 "); จำนวนเต็ม pageno = getPageno (เริ่มต้นความยาว); หน้า <ผู้ใช้> หน้า = หน้าใหม่ <user> (pageno, ความยาว); EntityWrapper<User> ew = new EntityWrapper<User>(); ew.setEntity(param); ew.where("password={0}","123456") .like("nickname","guangqing") .ge("create_time","2017-09-21 15:50:00"); userService.selectPage(page, ew); DatatablesJSON<User> resJson= new DatatablesJSON<>(); //resJson.setDraw(draw++); resJson.setRecordsTotal(page.getTotal()); resJson.setRecordsFiltered(page.getTotal()); resJson.setData(page.getRecords()); return resJson; } @GetMapping("/selectsql") public Object getUserBySql() { JSONObject result = new JSONObject(); result.put("records", userService.selectListBySQL()); ผลการกลับมา; } /** * 7. Pagination size Number of displays on one page Current page number* Method 1: http://localhost:8080/user/page?size=1¤t=1<br> * Method 2: http://localhost:8080/user/pagehelper?size=1¤t=1<br> * / // Parameter mode pagination @GetMapping("/page") public Object page(Page page) { return userService.selectPage(page); } // ThreadLocal Pattern pagination @GetMapping("/pagehelper") public Object pagehelper(Page page) { PageHelper.setPagination(page); page.setRecords(userService.selectList(null)); page.setTotal(PageHelper.freeTotal());//Get the total number and free resources. PageHelper.getTotal() return page; } /** * Test things* http://localhost:8080/user/test_transactional<br> * Access the following and no data is found to indicate that things are reliable! - <br> * http://localhost:8080/user/test<br> * <br> * Start Application Add @EnableTransactionManagement The annotation seems to be enabled without default<br> * Methods that require things plus @Transactional is a must! - */ @Transactional @GetMapping("/test_transactional") public void testTransactional() { //userService.insert(new User(1000L, "Test Things", 16, 3)); System.out.println("Exception is thrown manually here and data is automatically rolled back"); throw new RuntimeException(); -这么多的测试,我觉得最有趣的是条件构造器,在官网上有更齐全的,而我这里是按照我自己的需求写的。
最后谢谢大家的观看,写博客经验不足,写得不好请见谅,如果能给你带来帮助请点个赞,若遇到不明白的,或者我有写错的地方请提出,谢谢!也希望大家多多支持武林网。