欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Data JPA 實現(xiàn)多表關(guān)聯(lián)查詢的示例代碼

 更新時間:2018年07月04日 13:48:05   作者:我飛故我在的專欄  
多表查詢在spring data jpa中有兩種實現(xiàn)方式,第一種是利用hibernate的級聯(lián)查詢來實現(xiàn),第二種是創(chuàng)建一個結(jié)果集的接口來接收連表查詢后的結(jié)果,這里介紹第二種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

多表查詢在spring data jpa中有兩種實現(xiàn)方式,第一種是利用hibernate的級聯(lián)查詢來實現(xiàn),第二種是創(chuàng)建一個結(jié)果集的接口來接收連表查詢后的結(jié)果,這里介紹第二種方式。

一、一對一映射

實體 UserInfo :用戶。

實體 Address:家庭住址。

這里通過外鍵的方式(一個實體通過外鍵關(guān)聯(lián)到另一個實體的主鍵)來實現(xiàn)一對一關(guān)聯(lián)。

實體類

1、實體類 UserInfo.java

package com.johnfnash.learn.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tb_user")
public class UserInfo implements Serializable {
 private static final long serialVersionUID = 8283950216116626180L;

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long userId;
 private String name;
 private int age;
 private String sex;
 private String email;

 // 與 Address 的關(guān)聯(lián) 
 private Long addressId;

 public UserInfo() {
  super();
 }

 public UserInfo(String name, int age, String sex, String email, Long addressId) {
  super();
  this.name = name;
  this.age = age;
  this.sex = sex;
  this.email = email;
  this.addressId = addressId;
 }

 // getter, setter

 @Override
 public String toString() {
  return String.format("UserInfo [userId=%d, name=%s, age=%s, sex=%s, email=%s]", userId, name, age, sex, email);
 }

}

2. 實體類 Address.java

package com.johnfnash.learn.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "tb_address")
public class Address {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long addressId;
 private String areaCode;
 private String country;
 private String province;
 private String city;
 private String area;
 private String detailAddress;

 public Address() {
  super();
 }

 public Address(String areaCode, String country, String province, String city, String area,
   String detailAddress) {
  super();
  this.areaCode = areaCode;
  this.country = country;
  this.province = province;
  this.city = city;
  this.area = area;
  this.detailAddress = detailAddress;
 }

 // getter, setter

 @Override
 public String toString() {
  return "Address [addressId=" + addressId + ", areaCode=" + areaCode + ", country=" + country + ", province="
    + province + ", city=" + city + ", area=" + area + ", detailAddress=" + detailAddress + "]";
 }

}

Dao 層

1、UserInfoRepository.java

package com.johnfnash.learn.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.johnfnash.learn.domain.UserInfo;
import com.johnfnash.learn.domain.ViewInfo;

public interface UserInfoRepository extends JpaRepository<UserInfo, Long> {

 @Query(value = "SELECT new com.johnfnash.learn.domain.ViewInfo(u, a) FROM UserInfo u, Address a WHERE u.addressId = a.addressId")
 public List<ViewInfo> findViewInfo();

}

注:這里的 ViewInfo 類用來一個用來接收多表查詢結(jié)果集的類(使用 new + 完整類名構(gòu)造函數(shù))

代碼如下:

package com.johnfnash.learn.domain;

import java.io.Serializable;

public class ViewInfo implements Serializable {

 private static final long serialVersionUID = -6347911007178390219L;

 private UserInfo userInfo;
 private Address address;

 public ViewInfo() {

 }

 public ViewInfo(UserInfo userInfo) {
  Address address = new Address();
  this.userInfo = userInfo;
  this.address = address;
 }

 public ViewInfo(Address address) {
  UserInfo userInfo = new UserInfo();
  this.userInfo = userInfo;
  this.address = address;
 }

 public ViewInfo(UserInfo userInfo, Address address) {
  this.userInfo = userInfo;
  this.address = address;
 }

 // getter, setter

}

2. AddressRepository.java

package com.johnfnash.learn.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.johnfnash.learn.domain.Address;

public interface AddressRepository extends JpaRepository<Address, Long> {

}

測試代碼

package com.johnfnash.learn;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.johnfnash.learn.domain.Address;
import com.johnfnash.learn.domain.UserInfo;
import com.johnfnash.learn.domain.ViewInfo;
import com.johnfnash.learn.repository.AddressRepository;
import com.johnfnash.learn.repository.UserInfoRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserInfoRepositoryTests {

 @Autowired
  private UserInfoRepository userInfoRepository;

 @Autowired
 private AddressRepository addressRepository;

 @Before
  public void init() {
    Address addr1 = new Address("027","CN","HuBei", "WuHan","WuChang", "123 street");
    Address addr2 = new Address("023","CN","ChongQing", "ChongQing","YuBei", "123 road");
    addressRepository.save(addr1);
    addressRepository.save(addr2);

    UserInfo user1 = new UserInfo("ZS", 21,"Male","123@xx.com", addr1.getAddressId());
    UserInfo user2 = new UserInfo("Ww", 25,"Male","234@xx.com", addr2.getAddressId());
    userInfoRepository.save(user1);
    userInfoRepository.save(user2);
  }

 @After
 public void deleteAll() {
  userInfoRepository.deleteAll();

  addressRepository.deleteAll();
 }

 @Test
 public void testQuery() {
  List<ViewInfo> viewInfos = userInfoRepository.findViewInfo();
  for (ViewInfo viewInfo : viewInfos) {
   System.out.println(viewInfo.getUserInfo());
   System.out.println(viewInfo.getAddress());
  }
 }

}

查詢相關(guān)的 sql 如下:

Hibernate: select userinfo0_.user_id as col_0_0_, address1_.address_id as col_1_0_ from tb_user userinfo0_ cross join tb_address address1_ where userinfo0_.address_id=address1_.address_id
Hibernate: select userinfo0_.user_id as user_id1_4_0_, userinfo0_.address_id as address_2_4_0_, userinfo0_.age as age3_4_0_, userinfo0_.email as email4_4_0_, userinfo0_.name as name5_4_0_, userinfo0_.sex as sex6_4_0_ from tb_user userinfo0_ where userinfo0_.user_id=?
Hibernate: select address0_.address_id as address_1_3_0_, address0_.area as area2_3_0_, address0_.area_code as area_cod3_3_0_, address0_.city as city4_3_0_, address0_.country as country5_3_0_, address0_.detail_address as detail_a6_3_0_, address0_.province as province7_3_0_ from tb_address address0_ where address0_.address_id=?
Hibernate: select userinfo0_.user_id as user_id1_4_0_, userinfo0_.address_id as address_2_4_0_, userinfo0_.age as age3_4_0_, userinfo0_.email as email4_4_0_, userinfo0_.name as name5_4_0_, userinfo0_.sex as sex6_4_0_ from tb_user userinfo0_ where userinfo0_.user_id=?
Hibernate: select address0_.address_id as address_1_3_0_, address0_.area as area2_3_0_, address0_.area_code as area_cod3_3_0_, address0_.city as city4_3_0_, address0_.country as country5_3_0_, address0_.detail_address as detail_a6_3_0_, address0_.province as province7_3_0_ from tb_address address0_ where address0_.address_id=?
Hibernate: select userinfo0_.user_id as user_id1_4_, userinfo0_.address_id as address_2_4_, userinfo0_.age as age3_4_, userinfo0_.email as email4_4_, userinfo0_.name as name5_4_, userinfo0_.sex as sex6_4_ from tb_user userinfo0_
Hibernate: select address0_.address_id as address_1_3_, address0_.area as area2_3_, address0_.area_code as area_cod3_3_, address0_.city as city4_3_, address0_.country as country5_3_, address0_.detail_address as detail_a6_3_, address0_.province as province7_3_ from tb_address address0_

查詢結(jié)果如下:

UserInfo [userId=1, name=ZS, age=21, sex=Male, email=123@xx.com]
Address [addressId=1, areaCode=027, country=CN, province=HuBei, city=WuHan, area=WuChang, detailAddress=123 street]
UserInfo [userId=2, name=Ww, age=25, sex=Male, email=234@xx.com]
Address [addressId=2, areaCode=023, country=CN, province=ChongQing, city=ChongQing, area=YuBei, detailAddress=123 road]

二、多對多映射

實體 Author :作者。

實體 Book :書籍

這里通過關(guān)聯(lián)表的方式來實現(xiàn)多對多關(guān)聯(lián)。

實體類

實體類:Author.java

package com.johnfnash.learn.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Author implements Serializable {

 private static final long serialVersionUID = 1227555837798655046L;

 @Id
  @GeneratedValue
  private Integer id;

  private String name;

 public Author() {
  super();
 }

 public Author(String name) {
  super();
  this.name = name;
 }

 // getter, setter

 @Override
  public String toString() {
    return String.format("Author [id=%s, name=%s]", id, name);
  }

}

Book.java 實體類

package com.johnfnash.learn.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book implements Serializable {

 private static final long serialVersionUID = -2470510857424220408L;

 @Id
  @GeneratedValue
  private Integer id;

  private String name;

  public Book() {
    super();
  }

  public Book(String name) {
    super();
    this.name = name;
  }

 //getter, setter

 @Override
 public String toString() {
  return String.format("Book [id=%s, name=%s]", id, name);
 }

}

實體類BookAuthor.java

package com.johnfnash.learn.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

@Entity
@IdClass(BookAuthorPK.class)
@Table(name = "book_author")
public class BookAuthor {

 @Id
 private Integer bookId;

 @Id
 private Integer authorId;

 public BookAuthor() {
  super();
 }

 public BookAuthor(Integer bookId, Integer authorId) {
  super();
  this.bookId = bookId;
  this.authorId = authorId;
 }

 // getter, setter

}

注:這里使用 @IdClass 注解指定一個聯(lián)合主鍵類來映射實體類的多個屬性。這個聯(lián)合主鍵類的代碼如下:

package com.johnfnash.learn.domain;

import java.io.Serializable;

public class BookAuthorPK implements Serializable {

 private static final long serialVersionUID = -1158141803682305656L;

 private Integer bookId;

 private Integer authorId;

 public Integer getBookId() {
  return bookId;
 }

 public void setBookId(Integer bookId) {
  this.bookId = bookId;
 }

 public Integer getAuthorId() {
  return authorId;
 }

 public void setAuthorId(Integer authorId) {
  this.authorId = authorId;
 }

}

Dao 層

BookRepository.java

package com.johnfnash.learn.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.johnfnash.learn.domain.Book;

public interface BookRepository extends JpaRepository<Book, Integer> {

 @Query(nativeQuery = true, value = "SELECT b.id, b.name, GROUP_CONCAT(a.name) as authorName from book b, author a, book_author ba"
   + " where b.id = ba.book_id and a.id = ba.author_id and b.name like ?1 group by b.id, b.name")
  List<Object[]> findByNameContaining(String name);

}

注:

1)這里使用 nativeQuery = true 指定使用原生 SQL 進(jìn)行查詢(個人覺得復(fù)雜的查詢使用原生SQL更好

2)這里使用了 mysql 的內(nèi)置函數(shù) GROUP_CONCAT 進(jìn)行行轉(zhuǎn)列, HQL 無法直接識別??赡軙霈F(xiàn) Caused by: org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 的錯誤

JpaRepository.java

package com.johnfnash.learn.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.johnfnash.learn.domain.Author;

public interface AuthorRepository extends JpaRepository<Author, Integer> {

}

BookAuthorRepository.java

package com.johnfnash.learn.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.johnfnash.learn.domain.BookAuthor;

public interface BookAuthorRepository extends JpaRepository<BookAuthor, Integer> {

}

測試代碼

package com.johnfnash.learn;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.johnfnash.learn.domain.Author;
import com.johnfnash.learn.domain.Book;
import com.johnfnash.learn.domain.BookAuthor;
import com.johnfnash.learn.repository.AuthorRepository;
import com.johnfnash.learn.repository.BookAuthorRepository;
import com.johnfnash.learn.repository.BookRepository;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BookRepositoryTests {

 @Autowired
 private BookRepository bookRepository;

 @Autowired
 private AuthorRepository authorRepository;

 @Autowired
 private BookAuthorRepository bookAuthorRepository;

 @Before
 public void init() {
   Author lewis = new Author("Lewis");
   Author mark = new Author("Mark");
   Author peter = new Author("Peter");
   authorRepository.save(lewis);
   authorRepository.save(mark);
   authorRepository.save(peter);

   Book spring = new Book("Spring in Action");
   Book springboot = new Book("Spring Boot in Action");
   bookRepository.save(spring);
   bookRepository.save(springboot);

   bookAuthorRepository.save(new BookAuthor(spring.getId(), lewis.getId()));
   bookAuthorRepository.save(new BookAuthor(spring.getId(), mark.getId()));
   bookAuthorRepository.save(new BookAuthor(springboot.getId(), mark.getId()));
   bookAuthorRepository.save(new BookAuthor(springboot.getId(), peter.getId()));
 }

 @After
 public void deleteAll() {
  bookAuthorRepository.deleteAll();
  bookRepository.deleteAll();
  authorRepository.deleteAll();
 }

 @Test
 public void findAll() {
  assertEquals(bookRepository.findAll().size(), 2);
  assertEquals(authorRepository.findAll().size(), 3);

  List<Object[]> books = bookRepository.findByNameContaining("Spring%");
  for (Object[] book : books) {
   for (Object object : book) {
    System.out.print(object + ", ");
   }
   System.out.println();
  }
 }

}

執(zhí)行 findAll 方法后,查詢的相關(guān) SQL 如下:

Hibernate: SELECT b.id, b.name, GROUP_CONCAT(a.name) as authorName from book b, author a, book_author ba where b.id = ba.book_id and a.id = ba.author_id and b.name like ? group by b.id, b.name

輸出的結(jié)果如下:

3652, Spring in Action, Lewis,Mark,
3653, Spring Boot in Action, Mark,Peter,

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java JDK 1.8 lambda的用法詳解

    Java JDK 1.8 lambda的用法詳解

    這篇文章主要介紹了Java JDK 1.8 lambda的用法詳解,文中給大家提到了jdk 1.8 Lambda 表達(dá)式 遍歷數(shù)組的方法,需要的朋友可以參考下
    2019-09-09
  • 基于@GetMapping注解攜帶參數(shù)的方式

    基于@GetMapping注解攜帶參數(shù)的方式

    這篇文章主要介紹了基于@GetMapping注解攜帶參數(shù)的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Spring?Boot項目中使用?TrueLicense?生成和驗證License的詳細(xì)步驟

    Spring?Boot項目中使用?TrueLicense?生成和驗證License的詳細(xì)步驟

    這篇文章主要介紹了Spring?Boot項目中使用?TrueLicense?生成和驗證License,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-10-10
  • springcloud feign調(diào)其他微服務(wù)時參數(shù)是對象的問題

    springcloud feign調(diào)其他微服務(wù)時參數(shù)是對象的問題

    這篇文章主要介紹了springcloud feign調(diào)其他微服務(wù)時參數(shù)是對象的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring的Aware接口你知道多少

    Spring的Aware接口你知道多少

    這篇文章主要為大家詳細(xì)介紹了Spring的Aware接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java基礎(chǔ)教程之繼承詳解

    Java基礎(chǔ)教程之繼承詳解

    這篇文章主要介紹了Java基礎(chǔ)教程之繼承詳解,繼承是除組合(composition)之外,提高代碼重復(fù)可用性(reusibility)的另一種重要方式,本文對繼承做了詳細(xì)講解,需要的朋友可以參考下
    2014-08-08
  • 模擬打印機(jī)排隊打印效果

    模擬打印機(jī)排隊打印效果

    本節(jié)主要介紹了模擬打印機(jī)排隊打印效果的具體實現(xiàn),感興趣的朋友可以參考下
    2014-07-07
  • springcloud項目占用內(nèi)存好幾個G導(dǎo)致服務(wù)器崩潰的問題

    springcloud項目占用內(nèi)存好幾個G導(dǎo)致服務(wù)器崩潰的問題

    這篇文章主要介紹了springcloud項目占用內(nèi)存好幾個G導(dǎo)致服務(wù)器崩潰的問題,本文給大家分享解決方案供大家參考,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • 解決springcloud集成nacos遇到的問題

    解決springcloud集成nacos遇到的問題

    這篇文章介紹了如何解決springcloud集成nacos遇到的問題,文章中有詳細(xì)的代碼示例,需要的朋友可以參考一下
    2023-04-04
  • 詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇

    詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇

    這篇文章主要介紹了詳解Spring Boot Mysql 版本驅(qū)動連接池方案選擇,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評論