คำนำ
ฉันมักจะใช้ SpringMVC+MyBatis เพื่อพัฒนาอินเทอร์เฟซแบ็คเอนด์ ฉันไม่ได้ใช้งานเมื่อเร็ว ๆ นี้ ตามข้อกำหนดการทำงานที่มีอยู่ฉันใช้ Springboot+MyBatis เพื่อปรับใช้โครงการเว็บอย่างง่าย
เครื่องมือที่ใช้
แก้ไขปัญหา
1. วิธีสร้างโครงการ Springboot ด้วยความคิด
2. วิธีกำหนดค่าเซิร์ฟเวอร์ฐานข้อมูล mybatis ดู parser
3. วิธีใช้เครื่องกำเนิด mybatis เพื่อสร้างรหัสโดยอัตโนมัติ
4. วิธีใช้ Multipart เพื่ออัปโหลดไฟล์
5. วิธีใช้ธุรกรรม Springboot
6. วิธีการจัดเก็บการปรับใช้ Tomcat
ใช้แนวคิดในการสร้างโครงการ Springboot
1. แนวคิดเปิด, ไฟล์ -> ใหม่ -> โครงการ, เลือก Spring Initializr แล้วถัดไป
2. แก้ไข Ariifact ชื่อและแพ็คเกจด้านล่างจะได้รับการแก้ไขโดยอัตโนมัติ บรรจุภัณฑ์มีสองโหมดหนึ่งคือโถและอีกโถสประเทศคือสงคราม เนื่องจาก Springboot มาพร้อมกับ Tomcat โครงการสามารถเป็นขวดและเรียกใช้โดยตรง ในขณะที่โครงการที่มีอยู่ของฉันถูกนำไปใช้กับ Tomcat ฉันต้องเป็นแพ็คเกจสงคราม แล้วถัดไป
3. ตั้งค่าการพึ่งพาโครงการจากนั้นไปที่หน้าถัดไปตั้งชื่อโครงการและคลิกเสร็จสิ้นเพื่อให้เสร็จสิ้น
4. ป้อนโครงการ
pom.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> SpringBootDemo </artifactId> <sersion> 0.0.1-Snapshot </version> <packaging> สงคราม </packaging> <name> SpringbootDemo </name> <scription> โครงการสาธิตสำหรับการบูตฤดูใบไม้ผลิ </คำอธิบาย> <carent> <ArtIfactId> Spring-Boot-Starter-Parent </artifactid> <sersion> 2.0.2.release </เวอร์ชัน> <inelypath/> <!-ผู้ปกครองค้นหาจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> <Project.Reporting.UutputEncoding> UTF-8 </Project.Reporting.UutputPutenCoding> <Java.Version> 1.8 </Java.Version> </premerties> </perctency> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-starter-web </artifactid> </dependency> <Sersion> 1.3.2 </Serve> </derctency> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Devtools </artifactid> <ArtIfactId> MSSQL-JDBC </artifactId> <pope> รันไทม์ </cope> </การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <RoupID> org.springframework.boot </groupId> <ratifactid> ฤดูใบไม้ผลิ-boot-starter-test </artifactid> <scope> ทดสอบ </scope> </derpendency> </erpendencies> <uffactid> </plugin> </plugins> </uffer> </ched>
SpringMVC โดยไม่มีไฟล์กำหนดค่า
สองตัวอย่าง: 1. การเข้าถึงคำขอ http และการแสดงผลหน้า 2. คำขอ http ส่งคืนสตริง JSON
-กำหนดค่าแหล่งข้อมูลและดูการแสดงผล
-ดูการแสดงผลการพึ่งพา POM
-Create WelcomeController, welcome.jsp
โครงสร้างโครงการใหม่
Application.yml กำหนดค่าแหล่งข้อมูลและดูการแสดงผล
# แหล่งข้อมูลและมุมมองการกำหนดค่าสปริง: DataSource: URL: JDBC: SQLServer: // xx: 1433; databaseName = XX ชื่อผู้ใช้: XX รหัสผ่าน: XX ไดรเวอร์--class-name: com.microsoft.sqlserver.jdbc.sqlserverdriver MVC: ดู
pom.xml เพิ่มการพึ่งพาการแสดงผลมุมมอง
<!-ใช้ JSP ต้องการการพึ่งพา-> <การพึ่งพา> <roupId> org.apache.tomcat.embed </groupid> <ratifactid> tomcat- embed-jasper </artifactid> <pope> ให้ </pope>
สร้าง welcomecontroller
แพ็คเกจ com.example.springbootdemo.web; นำเข้า com.example.springbootdemo.entity.welcome; นำเข้า com.example.springbootdemo.response.response; นำเข้า com.example.springbootdemo.response.responsecode; org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.servlet.modelandview; welcomeController { / *** เยี่ยมชมหน้า welcome.jsp* @return* / @requestmapping ("WelcomeIndex") Public ModelandView WelcomeIndex () {ModelAndView MV = ใหม่ ModelAndView (); mv.setViewName ("ยินดีต้อนรับ"); mv.addobject ("ชื่อ", "xx"); กลับ MV; } / *** ส่งคืนสตริง JSON* @return* / @requestmapping ("getWelcomeinfo") @ResponseBody การตอบสนองสาธารณะ getWelcomeInfo () { / *** ข้อมูลทดสอบ* / รายการ <ยินดีต้อนรับ> ยินดีต้อนรับ = new ArrayList <> (); ยินดีต้อนรับ w1 = ใหม่ยินดีต้อนรับ (); W1.SetId ("1"); w1.setName ("xx1"); W1.Setage (11); W1.SetGender ("หญิง"); ยินดีต้อนรับ w2 = ใหม่ยินดีต้อนรับ (); W2.SetId ("2"); w2.setName ("xx2"); W2.Setage (22); W2.SetGender ("ชาย"); ยินดีต้อนรับ (W1); ยินดีต้อนรับ (W2); การตอบสนองการตอบสนอง = การตอบสนองใหม่ (); Response.setData (ยินดี); Response.SetRetCode (Responsecode.Success); Response.SetRetDesc ("ความสำเร็จ"); การตอบกลับกลับ; -สร้าง welcome.jsp
< %@ page contentType = "text/html; charset = utf-8" language = "java" %> <html> <head> <title> ดูการเรนเดอร์ </title> </head> <body> สวัสดี $ {ชื่อ} </body> </html>เริ่มโครงการและการเข้าถึง
http: // localhost: 8080/welcome/getwelcomeinfo
http: // localhost: 8080/welcome/welcomeindex
สร้างรหัสโดยอัตโนมัติโดยใช้เครื่องกำเนิด mybatis
ใช้เพื่อสร้าง *mapper.xml, รุ่น, ไฟล์ DAO สำหรับตาราง
เพิ่มเครื่องกำเนิด mybatis เพื่อสร้างปลั๊กอินรหัสโดยอัตโนมัติใน pom.xml
<uild> <plugins> <plugin> <sloupid> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> <! <ArtIfactId> MyBatis-Generator-Maven-Plugin </artifactId> <cersion> 1.3.2 </เวอร์ชัน> <การกำหนดค่า> <การกำหนดค่า file> $ {basedir} /src/main/resources/generator/generatorconfig.xml </การกำหนดค่า </plugin> </plugins> </ufferive>เพิ่ม GeneratorConfig.xml ในโฟลเดอร์ทรัพยากร/เครื่องกำเนิดไฟฟ้าที่กำหนดค่าไว้ใน pom.xml ด้านบน
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! DOCTYPE GENERATORATONCONTURAUNT POVER "-// MyBatis.org//dtd MyBatis Generation 1.0 // en" "http://mybatis.org/dtd/MyBatis <!-ไดรเวอร์ฐานข้อมูล: เลือกแพ็คเกจไดรเวอร์ฐานข้อมูลบนฮาร์ดดิสก์ในพื้นที่ของคุณ-> <classpathentry location = "c: /users/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/6.2.2.jre8/mssql-jdbc-6.6.jr8 targetRuntime = "myBatis3"> <ความคิดเห็นเจนเนอเรเตอร์> <ชื่อคุณสมบัติ = "suppressDate" value = "true"/> <!-ไม่ว่าจะลบความคิดเห็นที่สร้างขึ้นโดยอัตโนมัติ: ใช่: เท็จ: ไม่-> <property name = "suppressallComments" value = "true"/> </commentGenerator driverclass = "com.microsoft.sqlserver.jdbc.sqlserverDriver" การเชื่อมต่อ = "jdbc: sqlserver: // xx: 1433; databasename = xx" userid = "xx" value = "false"/> </javatyperesolver> <!-สร้างชื่อแพ็คเกจและตำแหน่งของโมเดล-> <JavamodelGenerator TargetPackage = "com.example.SpringBootDemo.entity" targetProject = "SRC/Main/Java"> </javamodelGenerator> <!-สร้างชื่อแพ็คเกจและตำแหน่งของไฟล์การแมป-> <sqlmapgenerator targetPackage = "mybatis" targetProject = "src/main/resources"> <property name = "enablesubpackages" value = "true"/> </sqlmapgenerator> <! type = "xmlmapper" targetPackage = "com.example.springbootdemo.mapper" เป้าหมาย project = "src/main/java"> <property name = "enablebpackages" value = "true"/> </javaclientGenerator> <! <table tablename = "xx" domainObjectName = "studentbinding" enableCountByExample = "false" enableUpDateByExample = "false" enableteByExample = "false" eNablesElectByExample = "false" selectByexampleQueryId = "false"
ใช้ MyBatis-Generator: สร้างใน Maven เพื่อผลิตคลาสที่เกี่ยวข้องตามตารางในฐานข้อมูล
แก้ไขการกำหนดค่า -> เพิ่ม -> maven
กำหนดค่า mybatis
เพิ่มการกำหนดค่า mybatis ใน application.yml
# mybatis การกำหนดค่า mybatis: mapper-locations: classpath*: mybatis/*mapper.xml type-aliases-package: com.example.springbootdemo.entity
เพิ่ม @Repository ("StudentBindingMapper") คำอธิบายประกอบใน StudentBindingMapper.java เพื่อสแกน
@Repository ("StudentBindingMapper") ส่วนต่อประสานสาธารณะ StudentBindingMapper {}เพิ่ม @mapperscan ใน SpringbootDemoapplication.java
แพ็คเกจ com.example.springbootdemo; นำเข้า org.mybatis.spring.annotation.mapperscan; นำเข้า org.springframework.boot.springapplication; org.springframework.boot.autoconfigure.springbootapplication;@springbootapplication@mapperscan ("com.example.springbootdemo.mapper") สปริงสปริงสปริง -เพิ่มบริการและเลเยอร์คอนโทรลเลอร์
ระดับโครงการ
เพิ่ม StudentBindingService
แพ็คเกจ com.example.springbootdemo.service; นำเข้า com.example.springbootdemo.entity.studentbinding; นำเข้า java.util.list; อินเทอร์เฟซสาธารณะ int insert (บันทึกการเรียนการสอน); int insertselective (บันทึกการเรียนการสอน); studentbinding selectbyprimarykey (Long ID); intupterbyPrimaryKeySelective (บันทึกการเรียนการสอน); IntupterbyPrimaryKey (บันทึกการเรียนการสอน); Void ValidTransaction (Long ID); รายการ <studentbinding> GetStudentBindByQuery (บันทึกการเรียนการสอน);}
เพิ่ม studentbindingserviceimpl
แพ็คเกจ com.example.springbootdemo.service.impl; นำเข้า com.example.springbootdemo.entity.studentbinding; นำเข้า com.example.springbootdemo.mapper.studentbindingmapper; org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.service; นำเข้า org.springframework.transaction.notation.transactional; นำเข้า Java.util.list; {@AutoWired Private StudentBindingMapper StudentBindingMapper; @Override public int deleteByPrimaryKey (Long ID) {return studentBindingMapper.deleteByPrimaryKey (ID); } @Override public int แทรก (บันทึกการศึกษา studentbinding) {return studentbindingmapper.insert (บันทึก); } @Override IntsertSelective (บันทึกการศึกษาของนักเรียน) {return studentbindingmapper.insertSelective (บันทึก); } @Override Public Studentbinding SelectByPrimaryKey (Long ID) {ส่งคืน StudentBindingMapper.SelectByPrimaryKey (ID); } @Override Public IntupterbyPrimaryKeySelective (บันทึกการเรียนการสอน) {ส่งคืน StudentBindingMapper.updateByPrimaryKeySelective (บันทึก); } @Override Public IntupterbyPrimaryKey (บันทึกการเรียนการสอน) {ส่งคืน StudentBindingMapper.updateByPrimaryKey (บันทึก); } @Override @Transactional โมฆะสาธารณะ ValidTransaction (Long ID) {// หลังจากการลบให้แทรกข้อมูลของ ID studentBindingMapper.deleteByPrimaryKey (ID); บันทึกการเรียนการสอน = นักศึกษาใหม่ (); record.setId (id); StudentBindingMapper.InsertSelective (บันทึก); } @Override รายชื่อสาธารณะ <SudentBinding> getStudentBindByQuery (บันทึกการเรียนการสอน) {ส่งคืน StudentBindingMapper.getStudentBindByQuery (บันทึก); -เพิ่ม StudentBindingController
แพ็คเกจ com.example.springbootdemo.web; นำเข้า com.example.springbootdemo.entity.studentbinding; นำเข้า com.example.springbootdemo.response.response; นำเข้า com.example.springbootdemo.response.responsecode; com.example.springbootdemo.service.studentbindingservice; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.nanntroller; org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.multipart.multipartfile; java.io.ioException; นำเข้า java.util.list;@controller@requestmapping (value = "/studentbind") นักศึกษาชั้นเรียนสาธารณะ BindingController {@autowired นักศึกษาเอกชน / ** * ลบข้อมูลนักเรียนที่ถูกผูกไว้ตามพารามิเตอร์คำขอ * @param id * @return */ @requestmapping ("DELETEBYPRIMARYKEY") @ResponseBody การตอบสนองสาธารณะ DELETEBYBRIMARYKEY (LONG ID) {ตอบกลับ = การตอบกลับใหม่ (); if (id == null) {response.setRetCode (responsecode.paramarter_error); Response.SetRetDesc ("ข้อผิดพลาดพารามิเตอร์"); การตอบกลับกลับ; } ลอง {StudentBindingService.deleteByPrimaryKey (ID); Response.SetRetCode (Responsecode.Success); Response.SetRetDesc ("deletesuccess"); } catch (exception e) {e.printstacktrace (); Response.SetRetCode (Responsecode.failed); Response.SetRetDesc ("Deleteeexception"); } ตอบกลับการตอบกลับ; } / ** * เพิ่มข้อมูลนักเรียนที่ถูกผูกไว้ตามพารามิเตอร์คำขอ * @param Record * @return * / @requestmapping ("InsertSelective") @ResponseBody การตอบสนองสาธารณะการตอบสนองสาธารณะ if (record == null) {response.setRetCode (responsecode.paramarter_error); Response.SetRetDesc ("ข้อผิดพลาดพารามิเตอร์"); การตอบกลับกลับ; } ลอง {StudentBindingService.InsertSelective (บันทึก); Response.SetRetCode (Responsecode.Success); Response.SetRetDesc ("เพิ่มความสำเร็จ"); } catch (exception e) {e.printstacktrace (); Response.SetRetCode (Responsecode.failed); Response.SetRetDesc ("เพิ่มข้อยกเว้น"); } ตอบกลับการตอบกลับ; } / ** * ตามพารามิเตอร์คำขอตรวจสอบข้อมูลนักเรียนที่ถูกผูกไว้ * @param id * @return * / @requestmapping ("SelectByPrimaryKey") @ResponseBody Response SelectByPrimaryKey (Long ID) {Response Response = new Response (); if (id == null) {response.setRetCode (responsecode.paramarter_error); Response.SetRetDesc ("ข้อผิดพลาดพารามิเตอร์"); การตอบกลับกลับ; } ลอง {studentbinding studentbinding = studentbindingservice.selectbyprimarykey (id); Response.setData (นักศึกษา binding); Response.SetRetCode (Responsecode.Success); Response.SetRetDesc ("ความสำเร็จในการสืบค้น"); } catch (exception e) {e.printstacktrace (); Response.SetRetCode (Responsecode.failed); Response.SetRetDesc ("ข้อยกเว้นแบบสอบถาม"); } ตอบกลับการตอบกลับ; } / ** * ตรวจสอบว่า @TransAction คำอธิบายประกอบนั้นใช้งานง่าย * @param id * @return * / @requestmapping ("ValidTransaction") @ResponseBody การตอบสนองสาธารณะ ValidTransAction (Long ID) {ตอบกลับ = new Response (); if (id == null) {response.setRetCode (responsecode.paramarter_error); Response.SetRetDesc ("ข้อผิดพลาดพารามิเตอร์"); การตอบกลับกลับ; } ลอง {StudentBindingService.ValidTransaction (ID); Response.SetRetCode (Responsecode.Success); } catch (exception e) {e.printstacktrace (); Response.SetRetCode (Responsecode.failed); } ตอบกลับการตอบกลับ; }/ *** แสดงหน้า JSP* @return*/ @requestmapping ("welidindex") โมเดลสาธารณะและวิววิว WelcomeIndex () {list <studentbinding> studentbindings = studentbindingservice.getStudentBindByQuery (นักศึกษาใหม่); ModelAndView MV = new ModelAndView (); mv.setViewName ("ยินดีต้อนรับ"); mv.addobject ("studentbindings", studentbindings); กลับ MV; } / *** ข้ามเพื่ออัปโหลดหน้าไฟล์* @return* / @requestmapping ("MultipartIndex") สตริงสาธารณะ MultipartIndex () {return "Multipart-index"; }/** * อัปโหลดไฟล์ไปยังไดเรกทอรีที่ระบุ * ไฟล์ @param * @return */@requestmapping ("/อัปโหลด") @ResponseBody การตอบกลับการตอบสนองสาธารณะ (@RequestParam ("ไฟล์") ไฟล์ MultipArtFile) {Response Response = new Response (); if (file.isempty ()) {response.setRetCode (responsecode.paramarter_error); Response.SetRetDesc ("ข้อผิดพลาดพารามิเตอร์"); การตอบกลับกลับ; } ลอง {string filePath = "d: // ceshi // upload //"; ไฟล์ dir = ไฟล์ใหม่ (filePath); if (! dir.isdirectory ()) {dir.mkdir (); } string fileOriginalName = file.getOriginalFileName (); File WriteFile = ไฟล์ใหม่ (FilePath + FileRiginalName); // เขียนไฟล์ไปยังไฟล์ดิสก์ transferto (writefile); Response.SetRetCode (Responsecode.Success); Response.SetRetDesc ("อัปโหลดสำเร็จ"); } catch (ioexception e) {e.printstacktrace (); Response.SetRetCode (Responsecode.failed); Response.SetRetDesc ("อัปโหลดล้มเหลว"); } ตอบกลับการตอบกลับ; -หลังจากรีสตาร์ทโครงการคุณสามารถเข้าถึงอินเทอร์เฟซต่าง ๆ
ธุรกรรมการกำหนดค่า Springboot
มีสองวิธีในการกำหนดค่าการทำธุรกรรมใน Springboot
1. ในทางเข้าโครงการ SpringbootDemoApplication.java เพิ่มคำอธิบายประกอบ @EnableTransactionManagement เพื่อเริ่มการทำธุรกรรม
2. เพิ่ม @Transactional คำอธิบายประกอบในคลาสการใช้งานบริการจากนั้นวิธีการทั้งหมดของคลาสจะได้รับการจัดการผ่านการทำธุรกรรม นอกจากนี้คุณยังสามารถเพิ่มคำอธิบายประกอบ @Transactional ลงในวิธีการใช้บริการระดับบริการได้โดยตรงจากนั้นการจัดการธุรกรรมจะดำเนินการเฉพาะในวิธีนี้ มีตัวอย่างของการเพิ่มธุรกรรมไปยังวิธีการในรหัสข้างต้น
แพ็คเกจ Springboot สำหรับการปรับใช้ Tomcat
แก้ไขการกำหนดค่า -> maven -> เพิ่ม -> เริ่ม -> คัดลอกแพ็คเกจสงคราม -> tomcat webapp -> แก้ไขชื่อของแพ็คเกจสงคราม -> tomcat bin -> startup.bat
หลังจากเริ่ม Tomcat ไปที่ http: // localhost: 8080/springbootdemo/welcome/welcomeindex สำหรับการตรวจสอบ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น