Springboot เป็นผลิตภัณฑ์ที่เกิดมาเพื่อลดความซับซ้อนของการสร้างการดำเนินการการดีบักและการปรับใช้แอปพลิเคชันฤดูใบไม้ผลิ คุณสมบัติการประกอบอัตโนมัติช่วยให้เราสามารถมุ่งเน้นไปที่ธุรกิจของตัวเองได้ดีกว่าการกำหนดค่า XML ภายนอก เราจำเป็นต้องทำตามข้อกำหนดและแนะนำการพึ่งพาที่เกี่ยวข้องเพื่อสร้างโครงการเว็บได้อย่างง่ายดาย
การดำเนินการฐานข้อมูลของ Spring Framework นั้นถูกห่อหุ้มอย่างลึกซึ้งใน JDBC ผ่านฟังก์ชั่นการฉีดพึ่งพา DataSource สามารถลงทะเบียนใน JDBCTEMPLATE ช่วยให้เราสามารถทำแผนที่ความสัมพันธ์กับวัตถุได้อย่างง่ายดายและช่วยหลีกเลี่ยงข้อผิดพลาดทั่วไป ใน Springboot เราสามารถใช้งานได้อย่างง่ายดาย
คุณสมบัติ
นำเข้าการพึ่งพา
เพิ่มการพึ่งพา jdbctemplate ใน pom.xml
<!-แพ็คเกจการพึ่งพา Spring JDBC การใช้ Spring-Boot-Starter-JDBC หรือ Spring-Boot-Starter-Data-JPA จะได้รับการพึ่งพา Hikaricp โดยอัตโนมัติ-> <การพึ่งพา> <roupId> org.springframework.boot </groupid> แพ็คเกจ-> <predency> <roupId> mysql </groupId> <ratifactid> mysql-connector-java </artifactid> </percterency> <!-Tomcat ถูกฝังอยู่ในคอนเทนเนอร์เริ่มต้นหากคุณต้องการเปลี่ยนคอนเทนเนอร์ <ArtIfactId> Spring-Boot-Starter-Web </artifactid> </dermentrency>
เชื่อมต่อกับฐานข้อมูล
เพิ่มการกำหนดค่าต่อไปนี้ใน application.properties เป็นที่น่าสังเกตว่า Springboot จะกำหนดค่าแหล่งข้อมูลโดยอัตโนมัติโดยค่าเริ่มต้นซึ่งจะจัดลำดับความสำคัญของการใช้พูลการเชื่อมต่อ Hikaricp หากไม่มีการพึ่งพาให้เลือก Tomcat-jdbc หากไม่มีสองตัวแรกให้เลือกคอมมอนส์ DBCP2 คุณสมบัติ Spring.datasource.type สามารถระบุกลุ่มการเชื่อมต่อประเภทอื่นได้
spring.datasource.url = jdbc: mysql: // localhost: 3306/บทที่ 4? useunicode = true & characterencoding = utf-8 & zerodatetimebehavior = convertto null & allowmultiqueries = true & usessl = falsespring.datasource.password = rootspring.datasource.username = root#spring.datasource.type#เพิ่มเติม การกำหนดค่าที่ละเอียดอ่อนสามารถปรับได้โดยคำนำหน้าต่อไปนี้#spring.datasource.hikari#spring.datasource.tomcat#spring.datasource.dbcp2
เริ่มโครงการผ่านบันทึกคุณจะเห็นว่าโดยค่าเริ่มต้น Hikaridatasource จะถูกฉีด
2018-05-07 10: 33: 54.021 ข้อมูล 9640 --- [Main] OsjeaAnnotationMbeanExporter: ถั่วที่มีชื่อ 'DataSource' ได้รับการตรวจสอบโดยอัตโนมัติสำหรับ JMX Exposure2018-05-07 10: 33: 54.026 ข้อมูล 9640- การลงทะเบียนกับ JMX Server เป็น mBean [com.zaxxer.hikari: name = dataSource, type = hikaridatasource] 2018-05-07 10: 33: 54.071 ข้อมูล 9640 --- [Main] Osbwembedded.tomcat.tomcatwebserver '' 2018-05-07 10: 33: 54.075 ข้อมูล 9640 --- [หลัก] com.battcn.Chapter4Application: เริ่มบทที่ 4Application ใน 3.402 วินาที (JVM Running) สำหรับ 3.93)
รหัสเฉพาะ
หลังจากเสร็จสิ้นการกำหนดค่าพื้นฐานให้ดำเนินการเข้ารหัสเฉพาะ เพื่อลดจำนวนรหัสฉันจะไม่เขียนอินเทอร์เฟซเช่น UserDao และ Userservice ฉันจะใช้ JDBCTEMPLATE โดยตรงในคอนโทรลเลอร์เพื่อเข้าถึงฐานข้อมูล นี่ไม่ได้มาตรฐาน อย่าเรียนรู้จากฉัน ...
โครงสร้างตาราง
สร้างตารางด้วย t_user
สร้างตาราง `t_user` (` id` int (8) ไม่ใช่ null auto_increment ความคิดเห็น 'คีย์การเพิ่มขึ้นหลัก', 'ชื่อผู้ใช้' Varchar (50) ไม่ใช่ความคิดเห็นที่เป็นโมฆะ
ชั้นเรียน
แพ็คเกจ com.battcn.entity;/** * @author levin * @since 2018/5/7 0007 */ผู้ใช้คลาสสาธารณะ {Private Long ID; ชื่อผู้ใช้สตริงส่วนตัว; รหัสผ่านสตริงส่วนตัว // todo ละเว้นการตั้งค่า}อินเทอร์เฟซสไตล์พักผ่อน
แพ็คเกจ com.battcn.controller; นำเข้า com.battcn.entity.user; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.jdbc.core.beanpropertyrowmapper; org.springframework.web.bind.annotation. *; นำเข้า java.util.list;/** * @author levin * @since 2018/4/23 0023 */ @restcontroller @requestmapping ("/ผู้ใช้" @AutoWired Public SpringJDBCCONTROLLER (JDBCTEMPLATE JDBCTEMPLATE) {this.jdBctemplate = JDBCTEMPLATE; } @getMapping รายการสาธารณะ <user> queryusers () {// query ผู้ใช้ทั้งหมดสตริง sql = "เลือก * จาก t_user"; ส่งคืน jdbctemplate.Query (SQL, วัตถุใหม่ [] {}, ใหม่ BeanPropertyRowMapper <> (user.class)); } @getMapping ("/{id}") ผู้ใช้สาธารณะ GetUser (@PathVariable Long ID) {// string string sql = "เลือก * จาก t_user โดยที่ id =?"; return jdbctemplate.QueryForObject (SQL, วัตถุใหม่ [] {id}, ใหม่ beanPropertyRowMapper <> (user.class)); } @DeleTeMapping ("/{id}") INT deluser สาธารณะ (@PathVariable Long ID) {// ลบข้อมูลผู้ใช้ตามสตริงรหัสหลัก SQL = "ลบจาก t_user โดยที่ id =?"; ส่งคืน jdbctemplate.update (SQL, ID); } @PostMapping public int adduser (@requestbody ผู้ใช้ผู้ใช้) {// เพิ่มสตริงผู้ใช้ sql = "แทรกลงใน t_user (ชื่อผู้ใช้, รหัสผ่าน) ค่า (?,?)"; ส่งคืน jdbctemplate.update (SQL, user.getUserName (), user.getPassword ()); } @putMapping ("/{id}") public int edituser (@PathVariable Long ID ผู้ใช้ @RequestBody ผู้ใช้) {// แก้ไขข้อมูลผู้ใช้ตามคีย์หลักรหัสสตริง SQL = "อัปเดต T_USER SET ชื่อผู้ใช้ =?, รหัสผ่าน =? ส่งคืน jdbctemplate.update (SQL, user.getUserName (), user.getPassword (), id); -ทดสอบ
เนื่องจากอินเทอร์เฟซข้างต้นเป็นอินเทอร์เฟซแบบพักผ่อนการเพิ่มและการแก้ไขจึงไม่สามารถทำได้ผ่านเบราว์เซอร์เราจึงต้องเขียน Junit ตัวเองหรือใช้เครื่องมือเช่นบุรุษไปรษณีย์
สร้างบททดสอบบทที่บทที่ 4ApplicationTests และจำลองการดำเนินการตามคำขอเช่น Get, Post, Put, DELETE ฯลฯ ผ่าน TestRestTemplate
แพ็คเกจ com.battcn; นำเข้า com.battcn.entity.user; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.slf4j.logger นำเข้า org.slf4j.loggerfactory; org.springframework.boot.test.context.springboottest นำเข้า org.springframework.boot.test.web.client.testresttemplate; นำเข้า org.springframework.boot.web.server.localserverport org.springframework.http.httpmethod; นำเข้า org.springframework.http.responseentity; นำเข้า org.springframework.test.context.junit4.springrunner; */@runwith (springrunner.class) @springboottest (คลาส = บทที่ 4application.class, webenvironment = springboottest.webenvironment.random_port) คลาสสาธารณะบทที่ 4applicationtests {ส่วนตัว @autowired Private TestRestTemplate แม่แบบ; @localserverport พอร์ต INT ส่วนตัว; @Test โมฆะสาธารณะ test1 () พ่นข้อยกเว้น {template.postforentity ("http: // localhost:" + พอร์ต + "/ผู้ใช้", ผู้ใช้ใหม่ ("user1", "pass1"), integer.class); log.info ("[เพิ่มความสำเร็จของผู้ใช้]/n"); // สิ่งที่ต้องทำหากเป็นคอลเลกชันที่ส่งคืนให้ใช้ Exchange แทน GetForentity ซึ่งจำเป็นต้องบังคับประเภท ResponseEntity <รายการ <ผู้ใช้ >> response2 = template.exchange ("http: // localhost:" + พอร์ต + "/ผู้ใช้", httpmethod.get, null รายการสุดท้าย <user> body = response2.getbody (); log.info ("[คำถามทั้งหมด] - [{}]/n", ร่างกาย); userId long = body.get (0) .getId (); ResponseEntity <user> response3 = template.getForentity ("http: // localhost:" + พอร์ต + "/ผู้ใช้/{id}", user.class, userid); log.info ("[คีย์คีย์หลัก] - [{}]/n", response3.getbody ()); template.put ("http: // localhost:" + พอร์ต + "/ผู้ใช้/{id}", ผู้ใช้ใหม่ ("user11", "pass11"), userid); log.info ("[แก้ไขผู้ใช้สำเร็จ]/n"); template.delete ("http: // localhost:" + พอร์ต + "/ผู้ใช้/{id}", userId); log.info ("[ลบผู้ใช้สำเร็จ]"); -สรุป
บทนี้แนะนำการดำเนินการที่ใช้กันทั่วไปหลายประการของ JDBCTEMPLATE สำหรับรายละเอียดโปรดดูเอกสาร JDBCTEMPLATE API
Bigwigs หลายคนเขียนบทเรียนเกี่ยวกับ Springboot หากมีความคล้ายคลึงกันโปรดยกโทษให้ฉัน บทช่วยสอนนี้ขึ้นอยู่กับผู้ปกครองสปริง-สตาร์สตาร์สเตอร์ล่าสุด: 2.0.1.release และคุณสมบัติของเวอร์ชันใหม่จะถูกนำมารวมกัน ...