อ้างถึงตัวอย่างอย่างเป็นทางการ: http://spring.io/guides/gs/relational-data-access/
1. การเตรียมโครงการ
สร้างตารางใหม่ "t_order" หลังจากสร้างฐานข้อมูล MySQL
ตั้งค่า _KEY_CHECKS = 0;----------------------------------- โครงสร้างตารางสำหรับ `t_order`-- ---------------------------- ตารางการวางถ้ามีอยู่` t_order`; สร้างตาราง `t_order` (ord ord_id` varchar (36) ไม่ใช่ null` order ' null, คีย์หลัก (`order_id`)) เครื่องยนต์ = myisam เริ่มต้น charset = utf8;------------------------------- บันทึกของ t_order-----------------------------------------------------------------------------------------------------------------
แก้ไข pom.xml
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/MANCMMMMMMMMMMMMMMMMMMMMMMMMMMBM http://maven.apache.org/xsd/maven-4.0.0.xsd "> <moderVersion> 4.0.0 </modelversion> <moderversion> <moderversion> <name> spring04 </name> <url> http://maven.apache.org </url> <carent> <roupId> org.springframework.boot </groupId> <ArtIfactId> สปริง-สตาร์สตาร์เทอร์ </artifactid> <Project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </premerties> </predency> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Devtools </artifactid> <plorital> True </onftiveal> </การพึ่งพา> <ArtIfactId> Spring-Boot-Starter-Thymeleaf </artifactid> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> org.springframework.boot </groupId> <ArtIfactId> mysql-connector-java </artifactid> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> Commons-DBCP </groupId> <ratifactId> Commons-DBCP </artifactid> </การพึ่งพา> <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
2. เขียนไฟล์คลาส:
แก้ไข app.java
แพ็คเกจ com.github.carter659.spring04; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; */@SpringBootapplicationPublic คลาสแอพ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (app.class, args); -สร้างไฟล์ Data Carrier ใหม่ "order.java"
แพ็คเกจ com.github.carter659.spring04; นำเข้า java.util.date;/** * บล็อกที่มา: http://www.cnblogs.com/goodhelper/ * @author Liu Dong * */คำสั่งระดับสาธารณะ สตริงสาธารณะหมายเลข; วันที่สาธารณะ ปริมาณ int สาธารณะ; /** * ละเว้นการตั้งค่า */}
สร้างคลาส Layer Layer การคงอยู่ใหม่ "orderdao.java"
แพ็คเกจ com.github.carter659.spring04; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า java.util.uuid; นำเข้า org.springframework.beans.factory.annotation.autowired; org.springframework.jdbc.support.rowset.sqlrowset; นำเข้า org.springframework.stereotype.repository;/** * บล็อกที่มา: http://www.cnblogs.com/goodhelper/ JDBCTEMPLATE JDBCTEMPLATE; รายการสาธารณะ <order> findall () {รายการ <order> list = new ArrayList <> (); String sql = "เลือก * จาก t_order"; sqlrowset rows = jdbctemplate.QueryForRowset (SQL, วัตถุใหม่ [] {}); ในขณะที่ (rows.next ()) {order order = new order (); list.add (คำสั่งซื้อ); order.id = rows.getString ("order_id"); order.no = rows.getString ("order_no"); order.date = rows.getDate ("order_date"); order.quantity = rows.getInt ("ปริมาณ"); } return list; } คำสั่งซื้อสาธารณะรับ (string id) {order order = null; String sql = "เลือก * จาก t_order โดยที่ order_id =?"; แถว SQLROWSET = JDBCTEMPLATE.QUERYFORROWSET (SQL, วัตถุใหม่ [] {ID}); ในขณะที่ (rows.next ()) {order = new order (); order.id = rows.getString ("order_id"); order.no = rows.getString ("order_no"); order.date = rows.getDate ("order_date"); order.quantity = rows.getInt ("ปริมาณ"); } return order; } การแทรกสตริงสาธารณะ (คำสั่งซื้อ) {string id = uuid.randomuuid (). toString (); String sql = "แทรกลงใน t_order (order_id, order_no, order_date, ปริมาณ) ค่า (?,?,?,?)"; jdbctemplate.update (sql, วัตถุใหม่ [] {id, order.no, java.sql.date ใหม่ (order.date.getTime ()), order.quantity}); return id; } การอัปเดตโมฆะสาธารณะ (คำสั่งซื้อ) {string sql = "อัปเดต t_order set order_no =?, order_date =?, ปริมาณ =? order_id =?"; jdbctemplate.update (SQL, วัตถุใหม่ [] {order.no, java.sql.date ใหม่ (order.date.getTime ()), order.quantity, order.id}); } โมฆะสาธารณะลบ (รหัสสตริง) {สตริง sql = "ลบจาก t_order โดยที่ order_id =?"; jdbctemplate.update (SQL, วัตถุใหม่ [] {id}); -การดำเนินการในฐานข้อมูลตามชื่อแนะนำ:
findall-> สอบถามข้อมูลทั้งหมด
รับ-> รับข้อมูลผ่าน ID
แทรก-> แทรกข้อมูล
อัปเดต-> แก้ไขข้อมูล
ลบ-> ลบข้อมูล
สร้างคอนโทรลเลอร์ใหม่ "MainController.java"
แพ็คเกจ com.github.carter659.spring04; นำเข้า java.util.hashmap; นำเข้า java.util.map; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.controller; org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.postmapping; นำเข้า org.springframework.web.bind.annotation.Requestbody; com.mysql.jdbc.stringutils; @controllerpublic maincontroller {@autowired private orderdao orderdao; @getMapping ("/") ดัชนีสตริงสาธารณะ () {return "ดัชนี"; } @PostMapping ("/save") public @ResponseBody Map <String, Object> บันทึก (@requestbody order order สั่ง) {แผนที่ <string, object> result = new hashmap <> (); if (stringutils.isnullorempty (order.id)) order.id = orderdao.insert (คำสั่งซื้อ); อื่น {orderdao.update (คำสั่งซื้อ); } result.put ("id", order.id); ผลการกลับมา; } @PostMapping ("/get") สาธารณะ @ResponseBody Object Get (String ID) {return orderdao.get (id); } @PostMapping ("/findall") สาธารณะ @ResponseBody Object findall () {return orderdao.findall (); } @PostMapping ("/ลบ") แผนที่สาธารณะ @ResponseBody แผนที่ <สตริงวัตถุ> ลบ (รหัสสตริง) {แผนที่ <สตริงวัตถุ> ผลลัพธ์ = ใหม่ hashmap <> (); orderdao.delete (id); result.put ("id", id); ผลการกลับมา; -3. สร้างเทมเพลต thymeleaf ใหม่
สร้างไฟล์ใหม่ "src/main/resources/templates/index.html"
<! doctype html> <html xmlns: th = "http://www.thymeleaf.org"> <head> <meta http-equiv = "content-type" content = "ข้อความ/html; charset = utf-8"/> src = "// cdn.bootcss.com/angular.js/1.5.6/angular.min.js"></script> type =" ข้อความ/javascript ">/*<! [cdata [*/var app = angular.module ('แอป' app.controller ('MainController', ฟังก์ชั่น ($ rootscope, $ scope, $ http) {$ scope.data = {}; $ scope.rows = []; // เพิ่ม $ scope.add = function () {$ scope.data = {no: };} // แก้ไข $ scope.edit = function (id) {สำหรับ (var i ใน $ scope.rows) {var row = $ scope.rows [i]; $ scope.rows [i]; ถ้า id == row.id) {$ scope.rows.splice (i, 1); $ scope.get (r.id); $ http ({url: '/get? id =' + id, วิธี: 'post',}). ความสำเร็จ (ฟังก์ชั่น (ข้อมูล) {สำหรับ (var i ใน $ scope.rows) {var row = $ spope.rows [i]; if (data.id == row.id) {row.no = data.no; $ scope.rows.push (ข้อมูล);}); /*]]>*/</script> </head> <body ng-app = "แอพ" ng-controller = "MainController"> <h1> เล่นสปริงบูต-รวมกับ jdbc </h1> <h4> <a href = "http://ww.cnblog.Com type = "button" value = "เพิ่ม" ng-click = "เพิ่ม ()" /> <อินพุต type = "ปุ่ม" value = "บันทึก" ng-click = "บันทึก ()" /> <br /> <br /> <h3> รายการข้อมูล: < /h3> <อินพุต id = "id" ประเภท <tr> <td> ไม่: </td> <td> <อินพุต id = "ไม่" ng-model = "data.no"/> </td> <td> วันที่: </td> <td> <อินพุต id = "วันที่" ng-model = "data.date"/> </td> ng-model = "data.quantity"/> </td> </tr> </table> <br/> <h3> รายการรายการ: </h3> <table cellpacing = "1" style = "พื้นหลังสี: #a0c6e5"> <tr style = "text-align <td> การดำเนินการ </td> <td> number </td> <td> วันที่ </td> <td> ปริมาณ </td> </tr> <ttr ng-repeat = "แถวในแถว" bgcolor = '#f4faff'> <td> ng-click = "del (row.id)" value = "ลบ" type = "ปุ่ม"/> </td> <td> {{row.no}} </td> <td> {{row.date}} </td> <td> {{row.quantity href = "http://www.cnblogs.com/goodhelper/"> คลิกเพื่อเข้าถึงบล็อกดั้งเดิม </a> </body> </html>ใช้ angularjs 'Ajax เพื่อเรียกวิธีพื้นหลังของ Spring Boot MV
4. การเชื่อมต่อฐานข้อมูล
สร้างไฟล์ "src/main/resources/application.properties" ใหม่
Spring.datasource.initialize = Falsespring.datasource.url = jdbc: mysql: // localhost: 3306/demospring.datasource.username = rootspring.datasource.password
โครงสร้างที่สมบูรณ์คือ:
5. ผลการดำเนินงาน
ป้อน "http: // localhost: 8080/" ในเบราว์เซอร์ของคุณ
เพิ่มข้อมูล:
บันทึกข้อมูลใหม่:
แก้ไขข้อมูล:
ลบข้อมูล:
ลบเอฟเฟกต์ของความสำเร็จ:
รหัส: https://github.com/carter659/spring-boot-04.git
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น