1. Windows 7 Enterprise Edition
2. ความคิด 14
3. JDK 1.8
4. Maven 3.5.2
5. Mariadb
6. Sqlyog
เพิ่ม settings.xml ในไดเรกทอรี COF ในไดเรกทอรี maven ไปยังเนื้อหาต่อไปนี้:
1. การใช้พื้นที่เก็บข้อมูลของ Alibaba Cloud นั้นเร็วกว่าเว็บไซต์ทางการมาก
<มิเรอร์> <id> nexus-aliyun </id> <irlorof> กลาง </irmorf> <name> nexus Aliyun </name> <url> http://maven.aliyun.com/nexus/content/groups/public </url>
2. การกำหนดค่า JDK ทั่วโลก
<!-การกำหนดค่า JDK ทั่วโลก, settings.xml-> <profile> <id> jdk18 </id> <การเปิดใช้งาน> <activeByDefault> True </ActiveByDefault> <jdk> 1.8 </jdk> </activation> <properties> <Maven.Compiler.Target> 1.8 </maven.compiler.target> <maven.compiler.compilerversion> 1.8 </maven.compiler.compilerversion> </properties> </premerties> </properties>
1. การตั้งค่า Maven: เลือกไดเรกทอรี Maven และกำหนดค่าไฟล์และที่เก็บในท้องถิ่นในเวลาเดียวกัน
2. การตั้งค่าการเข้ารหัสอักขระ
เลือกเปิดใช้งาน Auto-Import และสร้างไดเรกทอรีโครงการดังที่แสดงด้านล่าง:
1. 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" <ratifactid> studyspringboot </artifactid> <persion> 1.0-snapshot </version> <!-การตั้งค่าเริ่มต้นสำหรับการใช้การบูตฤดูใบไม้ผลิ-> <carent> <menter> <!-เว็บ-> <การพึ่งพา> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Starter-Web </artifactid> </การพึ่งพาอาศัย> <!-thymeleaf-> <predency> <ArtIfactId> Spring-Boot-Starter-Thymeleaf </artifactid> </การพึ่งพาอาศัย> <!-mysql ---> <การพึ่งพาอาศัย> <GroupId> mysql </groupid> <ratifactid> mysql-connector-java </artifactid> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Data-JPA </artifactid>
2. สร้างแอปพลิเคชันใหม่ properties ในไดเรกทอรีทรัพยากร (แน่นอนถ้าคุณต้องการใช้ Yaml คุณสามารถใช้ Yaml)
# การเชื่อมต่อฐานข้อมูล spring.datasource.url = jdbc: mysql: //127.0.0.1: 3306/testspring.datasource.username = rootspring.datasource.password = saspring.datasource.driver-class-name = com.mysql. Spring.jpa.properties.hibernate.hbm2ddl.auto = อัปเดต
3. สร้างคลาส Springboot Startup Class Springbootapplication.java
แพ็คเกจ cn.temptation; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; @springbootaplicationplass springbotaplication args); -
4. สร้างคลาส Entity Class.java
แพ็คเกจ cn.temptation.model; นำเข้า javax.persistence.*; // ห้องสมุดการสร้างตาราง // drop table หมวดหมู่; //// สร้างตารางตาราง // (// categoryid int auto_increment คีย์หลัก // categoryname varchar (10) null //); //// 'เสื้อผ้า'), (null, 'รองเท้า'); //////เลือก * จากหมวดหมู่; @entity @table (name = "category") หมวดหมู่คลาสสาธารณะ {@id @id @GeneratedValue (กลยุทธ์ = generationType.identity) @Column (ชื่อ = "categoryId") หมวดหมู่จำนวนเต็มส่วนตัว; @column (name = "categoryName") สตริงส่วนตัวหมวดหมู่ชื่อ; Public Integer GetCategoryId () {return categoryId; } โมฆะสาธารณะ setCategoryId (Integer categoryId) {this.categoryId = categoryId; } สตริงสาธารณะ getCategoryName () {return categoryName; } โมฆะสาธารณะ setCategoryName (String categoryName) {this.categoryName = categoryName; -5. สร้าง Dao Interface categorydao.java
แพ็คเกจ cn.temptation.dao; นำเข้า cn.temptation.model.category; นำเข้า org.springframework.data.jpa.repository.jparepository; Interface Public CategoryDao ขยาย JParePository <หมวดหมู่
6. สร้างคลาสคอนโทรลเลอร์หมวดหมู่ Controller.java
แพ็คเกจ cn.temptation.web; นำเข้า cn.temptation.dao.categorydao; นำเข้า cn.temptation.model.category; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.data.domain.pageRequest; นำเข้า org.springframework.data.domain.pageable; นำเข้า org.springframework.data.domain.sort; นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestparam; นำเข้า org.springframework.web.servlet.modelandview; Categorydao Categorydao; /** * unpaging query * * @return * /// @requestmapping ("/categorylist") // public modelandview categorylist () {// list <category> list = categorydao.findall (); //////////////////////////oldView mav; //}/** * การสืบค้น pagination * * @return */@requestmapping ("/categorylist") Public ModelandView CategoryList (@requestparam (value = "start", defaultValue = "0" 0: เริ่ม; sort sort = new sort (sort.default_direction, "categoryId"); PAGABLE PAGABLE = NEW PAGEREQUEST (เริ่ม, จำกัด , เรียงลำดับ); หน้า <หมวดหมู่> หน้า = categorydao.findall (pageable); // system.out.println (page.getNumber ()); // system.out.println (page.getNumberOfElements ()); // system.out.println System.out.println (page.getTotalPages ()); // system.out.println (page.isfirst ()); // system.out.println (page.islast ()); ModelAndView mav = new ModelandView ("categoryList"); mav.addobject ("หน้า" หน้า); กลับมา mav; }/*** มุมมองใหม่ในหมวดหมู่* @return*/@requestmapping ("/categoryInit") สตริงสาธารณะหมวดหมู่ Int () {return "categoryInit"; }/** * การดำเนินการใหม่ในหมวดหมู่ * @param โมเดล * @return */@requestmapping ("/categoryInsert") สตริงสาธารณะหมวดหมู่ (แบบจำลองหมวดหมู่) {categorydao.save (รุ่น); return "Redirect: CategoryList"; }/** * การลบหมวดหมู่การดำเนินการ * @param categoryId * @return */@requestmapping ("/categorydelete") สตริงสาธารณะหมวดหมู่ delete (จำนวนเต็มหมวดหมู่) {categorydao.deleteById (หมวดหมู่); return "Redirect: CategoryList"; }/** * มุมมองการแก้ไขหมวดหมู่ * @param categoryId * @return */@requestmapping ("/categoryEdit") Public ModelandView CategoryEdit (Integer CategoryId) {category model = categorydao.getone (หมวดหมู่); ModelAndView mav = new ModelandView ("categoryEdit"); mav.addobject ("หมวดหมู่", โมเดล); กลับมา mav; }/** * การดำเนินการแก้ไขหมวดหมู่ * @param model * @return */@requestmapping ("/categoryUpdate") สตริงสาธารณะหมวดหมู่ updoReUpdate (ประเภทหมวดหมู่) {categorydao.save (รุ่น); return "Redirect: CategoryList"; -7. สร้างไดเรกทอรีเทมเพลตใหม่ภายใต้ไดเรกทอรีทรัพยากรและสร้างเลเยอร์การนำเสนอ: หน้ารายการหมวดหมู่ (categorylist.html), หมวดหมู่ใหม่หน้า (categoryinit.html), หน้าแก้ไขหมวดหมู่ (categoryedit.html)
หน้าหมวดหมู่ (categorylist.html)
<! doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> รายการหมวดหมู่ </title> <style> ตาราง, th, td {border: 1px Solid Green; การล่มสลายของชายแดน: การล่มสลาย; } </style> </head> <body> <a th: href = "@{/categoryInit}"> เพิ่ม </a> <table> <tr> <th> หมายเลขหมวดหมู่ </th> <th> ชื่อหมวดหมู่ </th> <th> </th> </tr> <! Traversal-> <tr th: แต่ละรายการ = "รายการ: $ {page.content}"> <td th: text = "$ {item.categoryid}"> หมายเลขหมวดหมู่ </td> <td th: text = "$ {item.categoryname}"> ชื่อหมวดหมู่ </td> <td> Th: href = "@{/categoryEdit (categoryId = $ {item.categoryid})}"> แก้ไข </a> <a th: href = "@{/categoryDelete (categoryId = $ {item.categoryid}) th: href = "@{/categoryList (start = 0)}"> [home] </a> <a th: if = "$ {ไม่ใช่ page.isfirst ()}" th: href = "@{/categorylist (start = $ {page.totalpage-1}" th: href = "@{/categoryList (start = $ {page.number+1})}"> [หน้าถัดไป] </a> <a th: href = "@{/categoryList (start = $ {page.totalpages-1})}"> [หน้าสุดท้าย]หน้าใหม่สำหรับหมวดหมู่ (categoryinit.html)
<! doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> หมวดหมู่ใหม่ </title> </head> <body> <form action = "categoryInsert" method = "post"> <label for "txtCategoryName" name = "categoryName"/> <br/> <input type = "submit" value = "ส่ง"> </form> </body> </html>
หมวดหมู่แก้ไขหน้า (categoryEdit.html)
<! doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> หมวดหมู่แก้ไข </title> </head> <body> <form action = "categoryUpdate" method = "post"> <อินพุต type = th: field = "$ {category.categoryid}"/> <br/> <label for = "txtCategoryName"> ชื่อหมวดหมู่: </label> <อินพุต type = "text" id = "txtCategoryName" ชื่อ = "categoryName" th field = "$ {category.category value = "ส่ง"> </form> </body> </html>สรุป
ข้างต้นคือ Idea+Maven+Springboot+JPA+การใช้งาน thymeleaf ของ crud และ pagination แนะนำให้คุณโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทุกคนในเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!