มีสองวิธีการใช้งานสำหรับแบบสอบถามหลายตารางใน Spring Data JPA สิ่งแรกคือการใช้แบบสอบถามคาสเคดไฮเบอร์เนตเพื่อนำไปใช้งานและอย่างที่สองคือการสร้างอินเทอร์เฟซชุดผลลัพธ์เพื่อรับผลลัพธ์หลังจากการสืบค้นตารางที่เชื่อมต่อ นี่คือวิธีที่สอง
1. การทำแผนที่แบบตัวต่อตัว
Entity UserInfo: ผู้ใช้
ที่อยู่เอนทิตี: ที่อยู่บ้าน
ที่นี่การเชื่อมโยงแบบหนึ่งต่อหนึ่งทำได้ผ่านวิธีการสำคัญต่างประเทศ (เอนทิตีหนึ่งรายการเชื่อมโยงกับคีย์หลักของนิติบุคคลอื่นผ่านคีย์ต่างประเทศ)
ชั้นเรียน
1. เอนทิตีคลาส userinfo.java
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า java.io.serializable; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.generatedValue; นำเข้า Javax.persistence.generationType; userInfo ใช้ serializable {ส่วนตัวคงที่สุดท้าย long serialversionuid = 8283950216116626180L; @ID @GeneratedValue (Strategy = GenerationType.Identity) ผู้ใช้ยาวส่วนตัว; ชื่อสตริงส่วนตัว; อายุ int ส่วนตัว; เซ็กซ์สตริงส่วนตัว; อีเมลสตริงส่วนตัว // การเชื่อมโยงกับที่อยู่ส่วนตัวที่อยู่อาศัยส่วนตัว public userInfo () {super (); } public userInfo (ชื่อสตริงอายุ int, String Sex, String Email, Long addressId) {super (); this.name = ชื่อ; this.age = อายุ; this.sex = เพศ; this.email = อีเมล; this.addressid = addressId; } // getter, setter @Override สตริงสาธารณะ toString () {return string.format ("userInfo [userId =%d, ชื่อ =%s, อายุ =%s, เพศ =%s, อีเมล =%s]", userid, ชื่อ, อายุ, เพศ, อีเมล); - 2. ที่อยู่คลาส Entity.java
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.generatedValue; นำเข้า Javax.persistence.generationType; นำเข้า Javax.persistence.id; นำเข้า Javax.persistence.Table; @GeneratedValue (Strategy = GenerationType.Identity) Private Long addressId; สตริงส่วนตัวรหัสพื้นที่; ประเทศสตริงส่วนตัว จังหวัดสตริงส่วนตัว เมืองสตริงส่วนตัว; พื้นที่สตริงส่วนตัว รายละเอียดสตริงส่วนตัว; ที่อยู่สาธารณะ () {super (); } ที่อยู่สาธารณะ (String AreaCode, String Country, String Province, String City, พื้นที่สตริง, String Detailaddress) {super (); this.areAcode = areaCode; this.country = ประเทศ; สิ่งนี้ province = จังหวัด; this.city = เมือง; นี่คือพื้นที่ = พื้นที่; this.detailaddress = detailaddress; } // getter, setter @Override สตริงสาธารณะ toString () {return "address [addressId =" + addressId + ", areaCode =" + areapode + ", ประเทศ =" + ประเทศ + ", จังหวัด =" + จังหวัด + ", เมือง =" + เมือง + ", พื้นที่ =" + พื้นที่ - ชั้น Dao
1. userinforepository.java
แพ็คเกจ com.johnfnash.learn.Repository; นำเข้า java.util.list; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า org.springframework.data.jpa.repository.porery; com.johnfnash.learn.domain.viewinfo; อินเตอร์เฟสสาธารณะผู้ใช้งานด้านการขยายตัวขยาย jparepository <userinfo, long> {@query (value = "เลือก com.johnfnash.learn.domain.viewInfo (u, a) findViewInfo ();} หมายเหตุ: คลาส ViewInfo ที่นี่ใช้เพื่อรับชุดผลลัพธ์แบบสอบถามหลายตาราง (ใช้ตัวสร้างชื่อคลาสใหม่ + เต็มรูปแบบ)
รหัสมีดังนี้:
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า java.io.serializable; คลาสสาธารณะ ViewInfo ใช้ serializable {ส่วนตัวคงที่สุดท้าย Long Serialversionuid = -6347911007178390219L; ผู้ใช้ส่วนตัว userinfo userinfo; ที่อยู่ส่วนตัว Public ViewInfo () {} Public ViewInfo (userInfo userInfo) {ที่อยู่ = ที่อยู่ใหม่ (); this.userinfo = userInfo; this.address = ที่อยู่; } Public ViewInfo (ที่อยู่ที่อยู่) {userInfo userInfo = ใหม่ userInfo (); this.userinfo = userInfo; this.address = ที่อยู่; } Public ViewInfo (userInfo userInfo, ที่อยู่ที่อยู่) {this.UserInfo = userInfo; this.address = ที่อยู่; } // getter, setter} 2. addressRepository.java
แพ็คเกจ com.johnfnash.learn.repository; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า com.johnfnash.learn.domain.address;
รหัสทดสอบ
แพ็คเกจ com.johnfnash.learn; นำเข้า java.util.list; นำเข้า org.junit.fter; นำเข้า org.junit.Before; นำเข้า org.junit.test; นำเข้า org.junit.runner.runwith; นำเข้า org.springframework.beans.beans. org.springframework.boot.test.context.springboottest; นำเข้า org.springframework.test.context.junit4.springrunner; นำเข้า com.johnfnash.learn.domain.address; com.johnfnash.learn.domain.viewinfo; นำเข้า com.johnfnash.learn.repository.addressrepository; นำเข้า com.johnfnash.learn.repository.userinforepository; @runwith UserInforePository UserinforePository; @autowired ที่อยู่ส่วนตัวที่อยู่ที่อยู่อาศัย @Before Public Void Init () {ที่อยู่ addr1 = ที่อยู่ใหม่ ("027", "CN", "Hubei", "Wuhan", "Wuchang", "123 Street"); ที่อยู่ addr2 = ที่อยู่ใหม่ ("023", "CN", "Chongqing", "Chongqing", "Yubei", "123 Road"); addressRepository.save (addr1); addressRepository.save (addr2); userInfo user1 = ใหม่ userInfo ("zs", 21, "ชาย", "[email protected]", addr1.getAddressid ()); userInfo user2 = ใหม่ userInfo ("ww", 25, "ชาย", "[email protected]", addr2.getAddressid ()); userInforePository.save (user1); userInforePository.save (user2); } @After โมฆะสาธารณะ deleteAll () {userInforePository.deleteAll (); addressRepository.deleteAll (); } @Test Public Void TestQuery () {list <ViewInfo> viewInfos = userInforePository.findViewInfo (); สำหรับ (viewInfo viewInfo: viewInfos) {system.out.println (viewInfo.getUserInfo ()); System.out.println (ViewInfo.getAddress ()); -SQL ที่เกี่ยวข้องกับแบบสอบถามมีดังนี้:
Hibernate: เลือก userinfo0_.user_id เป็น col_0_0_, address1_.address_id เป็น col_1_0_ จาก tb_user userinfo0_ cross เข้าร่วม tb_address ที่อยู่ 1_ ที่ userinfo0_.address_id = address1_.address_idhibernate userInfo0_.address_id เป็นที่อยู่ adder_2_4_0_, userinfo0_.age เป็นอายุ 3_4_0_, userinfo0_.email เป็น email4_4_0_, userinfo0_.name เป็นชื่อ 5_4_0_, userinfo0_.sex userinfo0_.user_id =? hibernate: เลือกที่อยู่ 0_.address_id เป็นที่อยู่ address_1_3_0_, ที่อยู่ 0_.area เป็นพื้นที่ 2_3_0_, ที่อยู่ 0_.area_code เป็นพื้นที่ rea_cod3_3_0_, ที่อยู่ Detail_a6_3_0_, ที่อยู่ 0_.province เป็น Province7_3_0_ จากที่อยู่ tb_address0_ ที่ที่อยู่ 0_.address_id =? hibernate: เลือก userinfo0_.user_id เป็น user_id1_4_0_, userinfo0_.address_id userInfo0_.Email เป็น email4_4_0_, userinfo0_.name เป็น name5_4_0_, userinfo0_.sex เป็น sex6_4_0_ จาก tb_user userinfo0_ ที่ที่อยู่ userinfo0_.user_id =? Area2_3_0_, ที่อยู่ 0_.area_code เป็น AREA_COD3_3_0_, ที่อยู่ 0_.city เป็น City4_3_0_, ที่อยู่ 0_.country เป็น Country5_3_0_, ที่อยู่ 0_.detail_address address0_.address_id =? hibernate: เลือก userinfo0_.user_id เป็น user_id1_4_, userinfo0_.address_id เป็น address_2_4_, userinfo0_.age เป็นอายุ 3_4_, userinfo0_.email เป็นอีเมล sex6_4_ จาก tb_user userinfo0_hibernate: เลือกที่อยู่ 0_.address_id เป็น address_1_3_, address0_.area เป็นพื้นที่ 2_3_, address0_.area_code เป็น Area_cod3_3_, ที่อยู่ รายละเอียด _a6_3_, address0_.province เป็น Province7_3_ จากที่อยู่ tb_address0_
ผลลัพธ์การสืบค้นมีดังนี้:
userInfo [userId = 1, name = zs, อายุ = 21, sex = male, [email protected]]
ที่อยู่ [addressId = 1, AreaCode = 027, Country = CN, Province = Hubei, City = Wuhan, พื้นที่ = Wuchang, Detailaddress = 123 Street]
userInfo [userId = 2, name = ww, อายุ = 25, sex = male, [email protected]]
ที่อยู่ [addressId = 2, areaCode = 023, Country = CN, Province = Chongqing, City = Chongqing, พื้นที่ = Yubei, Detailaddress = 123 Road]
2. การทำแผนที่หลายต่อหลายครั้ง
ผู้เขียนนิติบุคคล: ผู้แต่ง
หนังสือเอนทิตี: หนังสือ
ที่นี่สมาคมหลายถึงหลายคนสามารถทำได้ผ่านตารางสมาคม
ชั้นเรียน
ระดับเอนทิตี: uther.java
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า java.io.serializable; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.generatedValue; นำเข้า Javax.persistence.id; @EntityPublic @ID @GeneratedValue ID จำนวนเต็มส่วนตัว; ชื่อสตริงส่วนตัว; ผู้เขียนสาธารณะ () {super (); } ผู้เขียนสาธารณะ (ชื่อสตริง) {super (); this.name = ชื่อ; } // getter, setter @Override สตริงสาธารณะ toString () {return string.format ("ผู้แต่ง [id =%s, ชื่อ =%s]", id, ชื่อ); - Book.java Entity Class
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า java.io.serializable; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.generatedValue; นำเข้า Javax.persistence.id; -2470510857424220408L; @ID @GeneratedValue ID จำนวนเต็มส่วนตัว; ชื่อสตริงส่วนตัว; หนังสือสาธารณะ () {super (); } หนังสือสาธารณะ (ชื่อสตริง) {super (); this.name = ชื่อ; } // getter, setter @Override สตริงสาธารณะ toString () {return string.format ("หนังสือ [id =%s, ชื่อ =%s]", id, ชื่อ); - Bookauthor.java ชั้นเรียน
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า Javax.persistence.entity; นำเข้า Javax.persistence.id; นำเข้า Javax.persistence.idclass; นำเข้า Javax.persistence.table;@entity@iDclass (bookauthorpk.class) @table @ID ส่วนตัวผู้แต่ง Public Bookauthor () {super (); } Public Bookauthor (Integer Bookid, Integer Authorid) {super (); this.bookid = bookid; this.authorid = ผู้เขียน; } // getter, setter}หมายเหตุ: ที่นี่เราใช้คำอธิบายประกอบ @IDCLASS เพื่อระบุคลาสคีย์หลักของยูเนี่ยนเพื่อแมปคุณสมบัติหลายอย่างของคลาสเอนทิตี รหัสสำหรับคลาสคีย์หลักร่วมนี้มีดังนี้:
แพ็คเกจ com.johnfnash.learn.domain; นำเข้า java.io.serializable; Bookauthorpk ระดับสาธารณะใช้งาน Serializable {ส่วนตัวคงที่สุดท้าย Long Serialversionuid = -1158141803682305656LONL; บุ๊คด์จำนวนเต็มส่วนตัว; ผู้เขียนจำนวนเต็มส่วนตัว; Public Integer getBookid () {return bookid; } โมฆะสาธารณะ setBookid (จำนวนเต็ม bookid) {this.bookid = bookid; } จำนวนเต็มสาธารณะ getauthorid () {return utherid; } โมฆะสาธารณะ setauthorid (จำนวนผู้เขียนจำนวนเต็ม) {this.authorid = ผู้เขียน; - ชั้น Dao
bookrepository.java
แพ็คเกจ com.johnfnash.learn.repository; นำเข้า java.util.list; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า org.springframework.data.jpa.Repository. JParePository <Book, Integer> {@Query (NativeQuery = true, value = "Select b.id, b.name, group_concat (a.name) เป็นชื่อ Authorname จาก Book B, Author A, Book_author ba" + "โดยที่ b.id = ba.book_id และ a.id = ba.author_id findBynameContaining (ชื่อสตริง);} บันทึก:
1) ที่นี่ NativeQuery = True ระบุว่าการใช้ SQL ดั้งเดิมใช้สำหรับการสืบค้น (โดยส่วนตัวแล้วฉันคิดว่าเป็นการดีกว่าที่จะใช้ SQL ดั้งเดิมสำหรับการสืบค้นที่ซับซ้อน
2) ที่นี่ฟังก์ชั่นในตัวของ MySQL ใช้สำหรับการแปลงแบบแถวเป็นคอลัมน์และ HQL ไม่สามารถรับรู้ได้โดยตรง เกิดจาก: org.hibernate.QueryException: ไม่มีประเภทข้อมูลสำหรับโหนด: org.hibernate.hql.internal.ast.tree.methodnode อาจเกิดขึ้น
jparepository.java
แพ็คเกจ com.johnfnash.learn.repository; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า com.johnfnash.learn.domain.author; อินเทอร์เฟซสาธารณะ
bookauthorrepository.java
แพ็คเกจ com.johnfnash.learn.repository; นำเข้า org.springframework.data.jpa.repository.jparepository; นำเข้า com.johnfnash.learn.domain.bookauthor;
รหัสทดสอบ
แพ็คเกจ com.johnfnash.learn; นำเข้า org.junit.assert.assertequals; นำเข้า java.util.list; นำเข้า org.junit.after; นำเข้า org.junit.before; นำเข้า org.junit.test; org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.boot.test.context.springboottest; นำเข้า org.springframework.test.context.junit4.springrunner; com.johnfnash.learn.domain.book; นำเข้า com.johnfnash.learn.domain.bookauthor; นำเข้า com.johnfnash.learn.repository.authorrepository; นำเข้า com.johnfnash.learn.Repository.Bookauthorrepository; com.johnfnash.learn.repository.bookrepository; @runwith (SpringRunner.class) @springboottestpublic Class BookrepositoryTests {@autowired Bookrepository Bookrepository; @autowired ส่วนตัว Authorrepository Authorrepository; @autoWired BookauthorRepository BookauthorRepository; @Before Public Void Init () {ผู้แต่ง Lewis = ผู้เขียนใหม่ ("Lewis"); ผู้แต่ง Mark = ผู้เขียนใหม่ ("Mark"); ผู้แต่งปีเตอร์ = ผู้เขียนใหม่ ("ปีเตอร์"); Authorrepository.save (Lewis); Authorrepository.save (Mark); Authorrepository.save (Peter); Book Spring = หนังสือเล่มใหม่ ("Spring in Action"); Book SpringBoot = หนังสือเล่มใหม่ ("Spring Boot in Action"); bookrepository.save (ฤดูใบไม้ผลิ); bookrepository.save (Springboot); bookauthorrepository.save (หนังสือใหม่ (Spring.getId (), lewis.getid ())); BookauthorRepository.save (หนังสือใหม่ (Spring.getId (), Mark.getId ())); bookauthorrepository.save (หนังสือใหม่ (springboot.getid (), mark.getid ())); BookauthorRepository.save (หนังสือใหม่ (springboot.getid (), peter.getid ())); } @After โมฆะสาธารณะ deleteAll () {bookauthorRepository.deleteAll (); BookRepository.deleteAll (); AuthorRepository.deleteAll (); } @test โมฆะสาธารณะ findall () {assertequals (bookrepository.findall (). size (), 2); assertequals (Authorrepository.findall (). size (), 3); รายการ <object []> books = bookrepository.findbynamecontaining ("Spring%"); สำหรับ (วัตถุ [] หนังสือ: หนังสือ) {สำหรับ (วัตถุวัตถุ: หนังสือ) {system.out.print (วัตถุ + ","); } system.out.println (); -หลังจากดำเนินการวิธีการค้นหา SQL ที่เกี่ยวข้องสำหรับการสืบค้นมีดังนี้:
Hibernate: Select B.ID, B.Name, Group_concat (A.Name) เป็นชื่อ Authorname จาก Book B, Author A, Book_author BA โดยที่ b.id = ba.book_id และ a.id = ba.author_id และ b.name ชอบ? กลุ่มโดย b.id, b.name
ผลลัพธ์ผลลัพธ์มีดังนี้:
3652, Spring in Action, Lewis, Mark,
3653, Spring Boot in Action, Mark, Peter,
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น