欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot JPA 表关联查询实例

程序员文章站 2024-03-01 13:43:22
今天给大家介绍一下如何利用jpa实现表关联查询。 今天给大家举一个一对多的关联查询,并且是使用jpa原生的findby语句实现的。 例子中总共有两个实体类,一个...

今天给大家介绍一下如何利用jpa实现表关联查询。

今天给大家举一个一对多的关联查询,并且是使用jpa原生的findby语句实现的。

例子中总共有两个实体类,一个是floor(商品楼层类),另一个是floorcontent(商品楼层内容表)。下面看两张表的源代码:

floor类:

package cms.model; 
import cms.model.base.basedomain; 
import org.hibernate.annotations.genericgenerator; 
 
import javax.persistence.*; 
import java.io.serializable; 
import java.util.list; 
/** 
 * created by roney on 2016/10/10. 
 * 楼层管理 
 * 
 */ 
@entity 
@table(indexes = {@index(name = "idx_floor_user",columnlist = "user_id")}) 
public class floor extends basedomain implements serializable { 
 
  @id 
  @genericgenerator(name = "pkuuid", strategy = "uuid2") 
  @generatedvalue(generator = "pkuuid") 
  @column(length = 36) 
  protected string id; 
 
  /** 
   * 发布用户id 
   */ 
  @column(length = 36,name = "user_id") 
  private string userid; 
 
  /** 
   * 楼层名称 
   */ 
  private string name; 
 
  /** 
   * 楼层的模板路径 
   */ 
  private string templateurl; 
 
  /** 
   * 类型 
   * 1.管理端 
   * 2.供应商 
   */ 
  private integer type; 
 
 
  /** 
   * 排序 
   */ 
  @column(name = "show_index", nullable = false) 
  private integer showindex; 
 
  /** 
   * 是否禁用 
   * */ 
 
 
  @column(nullable = false) 
  private boolean isdisable=false; 
 
  @onetomany(fetch = fetchtype.lazy,mappedby = "floor") 
  private list<floorcontent> floorcontents; 
 
  public list<floorcontent> getfloorcontents() { 
    return floorcontents; 
  } 
 
  public void setfloorcontents(list<floorcontent> floorcontents) { 
    this.floorcontents = floorcontents; 
  } 
 
  public string getid() { 
    return id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getuserid() { 
    return userid; 
  } 
 
  public void setuserid(string userid) { 
    this.userid = userid; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public string gettemplateurl() { 
    return templateurl; 
  } 
 
  public void settemplateurl(string templateurl) { 
    this.templateurl = templateurl; 
  } 
 
  public integer getshowindex() { 
    return showindex; 
  } 
 
  public void setshowindex(integer showindex) { 
    this.showindex = showindex; 
  } 
 
  public boolean getdisable() { 
    return isdisable; 
  } 
 
  public void setdisable(boolean disable) { 
    isdisable = disable; 
  } 
 
  @override 
  public boolean equals(object o) { 
    if (this == o) return true; 
    if (o == null || getclass() != o.getclass()) return false; 
 
    floor floor = (floor) o; 
 
    return id != null ? id.equals(floor.id) : floor.id == null; 
 
  } 
 
  @override 
  public int hashcode() { 
    return id != null ? id.hashcode() : 0; 
  } 
} 

floorcontent类:

package cms.model; 
 
import cms.model.base.basedomain; 
import org.hibernate.annotations.genericgenerator; 
 
import javax.persistence.*; 
import java.io.serializable; 
 
/** 
 * created by roney on 2016/10/10. 
 * 楼层的内容 
 */ 
 
@entity 
@table(indexes = {@index(name = "idx_floor_content_user", columnlist = "user_id")}) 
public class floorcontent extends basedomain implements serializable { 
 
  @id 
  @genericgenerator(name = "pkuuid", strategy = "uuid2") 
  @generatedvalue(generator = "pkuuid") 
  @column(length = 36) 
  protected string id; 
 
  /** 
   * 发布用户id 
   */ 
  @column(length = 36, name = "user_id") 
  private string userid; 
 
  /** 
   * 內容名稱 
   */ 
  private string name; 
 
  /** 
   * 
   * 內容圖片 
   */ 
  @column(length = 256) 
  private string contentimageurl; 
 
  /** 
   * 類型 
   * 1.超鏈接 
   * 2.圖片檢索 
   */ 
  private integer type; 
 
  /** 
   * 超鏈接url 
   */ 
  private string linkurl; 
 
  /** 
   * 圖片檢索內容 
   */ 
  private string picsearchcontent; 
 
  /** 
   * 排序 
   */ 
  @column(name = "show_index", nullable = false) 
  private integer showindex; 
 
  /** 
   * 是否禁用 
   */ 
 
  @column(nullable = false) 
  private boolean isdisable = false; 
 
  @manytoone 
  @joincolumn(name = "floor_id",foreignkey = @foreignkey(name = "fk_floor_fc")) 
  private floor floor; 
 
  public floor getfloor() { 
    return floor; 
  } 
 
  public void setfloor(floor floor) { 
    this.floor = floor; 
  } 
 
  public string getid() { 
    return id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getuserid() { 
    return userid; 
  } 
 
  public void setuserid(string userid) { 
    this.userid = userid; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public string getcontentimageurl() { 
    return contentimageurl; 
  } 
 
  public void setcontentimageurl(string contentimageurl) { 
    this.contentimageurl = contentimageurl; 
  } 
 
  public integer gettype() { 
    return type; 
  } 
 
  public void settype(integer type) { 
    this.type = type; 
  } 
 
  public string getlinkurl() { 
    return linkurl; 
  } 
 
  public void setlinkurl(string linkurl) { 
    this.linkurl = linkurl; 
  } 
 
  public string getpicsearchcontent() { 
    return picsearchcontent; 
  } 
 
  public void setpicsearchcontent(string picsearchcontent) { 
    this.picsearchcontent = picsearchcontent; 
  } 
 
  public integer getshowindex() { 
    return showindex; 
  } 
 
  public void setshowindex(integer showindex) { 
    this.showindex = showindex; 
  } 
 
  public boolean getdisable() { 
    return isdisable; 
  } 
 
  public void setdisable(boolean disable) { 
    isdisable = disable; 
  } 
 
  @override 
  public boolean equals(object o) { 
    if (this == o) return true; 
    if (o == null || getclass() != o.getclass()) return false; 
 
    floorcontent that = (floorcontent) o; 
 
    return id != null ? id.equals(that.id) : that.id == null; 
 
  } 
 
  @override 
  public int hashcode() { 
    return id != null ? id.hashcode() : 0; 
  } 
} 

实体类已经出来了,现在具体说说怎么利用jpa中findby来实现关联查询:

package cms.model.repository; 
import cms.model.floor; 
import cms.model.floorcontent; 
import org.springframework.data.domain.page; 
import org.springframework.data.domain.pageable; 
import org.springframework.data.jpa.repository.jparepository; 
/** 
 * created by roney on 2016/10/10. 
 * created by roney on 2016/10/10. 
 * 楼层内容管理dao类 
 */ 
public interface floorcontentrepos extends jparepository<floorcontent,string>{ 
  public page<floorcontent> findbyfloor_idandisdeleteorderbyshowindexasc(string floorid,boolean b, pageable pageable); 
} 

从例子中就可以看出jpa关联查询主要在“_”这个符号的使用,下面来给大家具体的介绍一下这个符号到底代表什么含义。

首先findby是必须写的,表示使用jpa规则进行查询。

如果查询的是本张表中的内容,例如查询本张表中的name字段就可以这么写:findbyname()。

如果查询的是楼层中的name字段就可以这么写:findbyfloor_name()。

如果是既要查询本张表中的name字段,也要查询楼层中的name字段,就可以这么写:findbyfloor_nameandname()。

从上面的案例就可以看出可以在findby后面添加要关联的实体类,然后在实体类后面写上“_”,"_"符号后面是添加关联表的字段而不是本身表的字段,这点要记住。如何还想关联更多的表可以在后面添加:and+表名字+“_”+表中要查询的字段。或者只是想关联本身的查询字段可以在后面添加:and+查询的字段。

千万不要写错了,写错的话运行都运行不起来的。所以写的时候要多看看是否符合规则。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。