เมื่อสร้างโครงการใหม่คุณต้องเพิ่มการพึ่งพา JPA และ H2
การพึ่งพาไฟล์ POM มีดังนี้:
<? 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"> <soderversion> 4.0.0 <ArtIfactId> SpringBoot-H2 </artifactid> <sersion> 0.0.1-Snapshot </Sersion> <packaging> jar </packaging> <name> Springboot-H2 </name> <scription> โครงการสาธิตสำหรับ Spring Boot H2 </คำอธิบาย> <carent> <ArtIfactId> Spring-Boot-Starter-Parent </artifactid> <sersion> 1.4.3.release </เวอร์ชัน> <inelypath/> <!-ผู้ปกครองค้นหาจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> UTF-8 <Project.Reporting.UutputEncoding> UTF-8 </project.Reporting.UutputUntencoding> <Java.Version> 1.7 </Java.Version> </premerties> <pernceies> </perctency> <predency> <roupId> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Starter-Web </artifactid> </derness> <การพึ่งพา> <roupid> com.h2Database </groupid> <ratifactid> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-test </artifactid> <scope> ทดสอบ </cope> </derness> </derction> </การพึ่งพา <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
แพ็คเกจ com.chhliu.springboot.h2.entity; นำเข้า java.math.bigdecimal; นำเข้า Javax.persistence.column; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.GeneratedValue; นำเข้า Javax.persistence.generationType; นำเข้า Javax.persistence.id; @ENTITY ผู้ใช้คลาสสาธารณะ {@ID @GeneratedValue (Strategy = GenerationType.Auto) ID ยาวส่วนตัว; @Column Private String ชื่อผู้ใช้; @Column ชื่อสตริงส่วนตัว; @Column อายุสั้นส่วนตัว; @Column ความสมดุลส่วนตัว BigDecimal; ... ละเว้นวิธีการ gettter และ setter} แพ็คเกจ com.chhliu.springboot.h2.Repository; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า org.springframework.stereotype.repository; นำเข้า com.chhliu.springboot.h2.entity.user; @Repository Interface UserRepository ขยาย jParePository <ผู้ใช้ยาว> {} แพ็คเกจ com.chhliu.springboot.h2.controller; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.pathvariable; นำเข้า org.springframework.web.bind.annotation.restcontroller; นำเข้า com.chhliu.springboot.h2.entity.user; นำเข้า com.chhliu.springboot.h2.repository.userrepository; @RestController ชั้นเรียนสาธารณะ USERCONTROLLER {@AutoWired USERREPOSITERITION USERREPOSITION CITHER; @getMapping ("/user/{id}") // โปรดทราบว่าการใช้คำอธิบายประกอบ getMapping ถูกใช้ที่นี่และฟังก์ชั่นของคำอธิบายประกอบนี้คล้ายกับ @requestmapping (value = "/user/{id}", method = requestmethod.get) -# หมายเลขพอร์ตเซิร์ฟเวอร์เซิร์ฟเวอร์พอร์ต = 7900 # ไม่ว่าจะสร้างคำสั่ง DDL Spring.jpa.generate-ddl = false # ไม่ว่าจะพิมพ์คำสั่ง SQL Spring.jpa.show-sql = True # สร้าง DDL โดยอัตโนมัติ เนื่องจากมีการระบุ DDL เฉพาะจึงไม่ได้ตั้งค่าเป็น Spring.jpa.hibernate.ddl-Auto = ไม่มี # ใช้ฐานข้อมูล H2 Spring.datasource.platform = H2 # ระบุตำแหน่งไฟล์สคีมา Spring.datasource.data = classpath: data.sql # กำหนดค่าข้อมูลการพิมพ์บันทึกการบันทึกการบันทึก. level.level = ข้อมูล logging.level.org.hibernate = ข้อมูล logging.level.org.hibernate.type.descriptor.sql.basicbinder = Trace logging.level.org.hibernate.type.descriptor.sql.basicextractor = trace logging.level.com.itmuch = debug
ป้อน URL ต่อไปนี้ในเบราว์เซอร์ของคุณ: http: // localhost: 7900/ผู้ใช้/4
คุณสามารถดูผลการทดสอบ
{"id": 4, "ชื่อผู้ใช้": "user4", "ชื่อ": "maliu", "อายุ": 20, "ยอดคงเหลือ": 100.00}
อธิบายว่าการรวมของเราไม่เป็นไร
แพ็คเกจ com.chhliu.springboot.h2; นำเข้า org.junit.assert; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.boot.test.context.springboottest; นำเข้า org.springframework.test.context.junit4.springrunner; นำเข้า com.chhliu.springboot.h2.entity.user; นำเข้า com.chhliu.springboot.h2.repository.userrepository; @runwith (SpringRunner.class) @springboottest ชั้นเรียนสาธารณะ Springbooth2ApplicationTest {@autowired ส่วนตัวที่เก็บ userrepository; @Test Public Void Test () {USER U = Repository.Findone (1L); assert.assertequals ("กรณีทดสอบที่ประสบความสำเร็จ", "Zhang San", U.getName ()); - ฉันพบว่าการทดสอบนั้นโอเค!
เนื่องจาก H2 เป็นฐานข้อมูลหน่วยความจำเชิงสัมพันธ์เมื่อโปรแกรมเริ่มต้นตารางจะถูกสร้างขึ้นในหน่วยความจำและข้อมูลจะถูกเก็บไว้ในหน่วยความจำ หลังจากรีสตาร์ทโปรแกรมข้อมูลในหน่วยความจำจะถูกลบโดยอัตโนมัติซึ่งสามารถใช้งานได้ดีสำหรับการทดสอบหน่วยที่ชั้น DAO และการทดสอบหน่วยที่ชั้นบริการเพื่อให้โปรแกรมทั้งหมดไม่พึ่งพาฐานข้อมูลเฉพาะและปรับปรุงประสิทธิภาพของการทดสอบหน่วย
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น