一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot Data JPA 關聯表查詢的方法

SpringBoot Data JPA 關聯表查詢的方法

2021-05-12 15:52Duebasso Java教程

這篇文章主要介紹了SpringBoot Data JPA 關聯表查詢的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

springboot data jpa實現 一對多、多對一關聯表查詢

開發環境

  1. idea 2017.1
  2. java1.8
  3. springboot 2.0
  4. mysql 5.x

功能需求

通過關聯關系查詢商店store中所有的商品shop,商店對商品一對多,商品對商店多對一,外鍵 store_id存在于多的一方。使用數據庫的內連接語句。

表結構

SpringBoot Data JPA 關聯表查詢的方法

tb_shop

SpringBoot Data JPA 關聯表查詢的方法

tb_store

實體類,通過注解實現

1.商店類store.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.gaolei.entity;
import javax.persistence.*;
import java.util.hashset;
import java.util.set;
 
/**
 * created by gaolei on 2018/6/25.
 */
@entity
@table(name = "tb_store")
public class store {
  @id
  @generatedvalue(strategy = generationtype.identity)
  private integer id;//商鋪號
 
  private string name;//商鋪姓名
 
  private string address;//商鋪地址
 
  private int tel ;//商鋪聯系
 
  private string info;//商鋪信息
 
  @onetomany(cascade = cascadetype.all,mappedby = "store")
  private set<shop> shops = new hashset<shop>();
  // 省略set()和get()方法;
}

商品類shop.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.gaolei.entity;
 
import javax.persistence.*;
import java.util.hashset;
import java.util.set;
 
/**
 * created by gaolei on 2018/6/25.
 */
@entity
@table(name = "tb_shop")
public class shop {
 
  @id
  @generatedvalue(strategy = generationtype.identity)
  private integer id ; //商品id
 
  private string name;//商品名
 
  private int price;// 商品價格
 
  private int num;//商品數量
 
  private string info;//商品信息
 
  @manytoone
  @joincolumn(name = "store_id")//外鍵
  private store store;
  // 省略set()和get()方法;
}

storedao.java

crudrepository 接口繼承于 repository 接口,并新增了簡單的增、刪、查等方法。其中封裝好了很多的方法,這里不再概述,自行百度,這里通過自定義hql語句完成復雜的操作。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.gaolei.dao;
import com.gaolei.entity.store;
import org.springframework.data.jpa.repository.query;
import org.springframework.data.repository.crudrepository;
import org.springframework.stereotype.repository;
import java.util.list;
/**
 * created by gaolei on 2018/6/25.
 */
@repository
public interface storedao extends crudrepository<store,integer> {
 
 
  //此方法通過內連接查詢店鋪id=?中的所有商品
  @query("select distinct s from store s inner join s.shops where s.id = ?1")
  list<store> findbyshoplist(integer id);
}

storeservice.java

通過@autowired注入storedao來實現方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.gaolei.service;
import com.gaolei.dao.storedao;
import com.gaolei.entity.shop;
import com.gaolei.entity.store;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.transaction.annotation.transactional;
import java.util.list;
 
/**
 * created by gaolei on 2018/6/25.
 */
@controller
@transactional
public class storeservice {
  @autowired
  private storedao storedao;
  /**
   * 展示商店商品
   * */
  public list<store> findbyshoplist(integer id){
    return storedao.findbyshoplist(id);
  }
}

storeaction.java

實現具體數據操作操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.gaolei.action;
import com.gaolei.entity.shop;
import com.gaolei.entity.store;
import com.gaolei.service.shopservice;
import com.gaolei.service.storeservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.util.arraylist;
import java.util.list;
/**
 * created by gaolei on 2018/6/26.
 */
@controller
@requestmapping("/store")
public class storeaction {
 
  @autowired
  private storeservice storeservice;
 
 /**
   * store_shop_menu展示店鋪商品
   * */
  @requestmapping("showshop")
  public string showshop(httpservletresponse response ,httpservletrequest request,model model){
    string id = request.getparameter("store_id");
    //通過hql語句拿到id=?的商鋪,并拿到該店鋪下所有的商品
    list<store> list = storeservice.findbyshoplist(integer.valueof(id));
    //返回的為一個store集合,store類和shop類為一對多,store下的shops為list<shop>。
    list<shop> shoplist = new arraylist<shop>();
//循環遍歷拿到每一個shop,添加到一個新的list<shop>中,用于將數據在前臺展示。
    for (store store:list){
        system.out.println(store.getname());
      for (shop shop: store.getshops()) {
        system.out.println(shop.getname());
        shoplist.add(shop);
      }
    }
    model.addattribute("list",shoplist);
    return "admin/showshop";
  }
}

前臺頁面跳轉

SpringBoot Data JPA 關聯表查詢的方法

查看的店鋪

SpringBoot Data JPA 關聯表查詢的方法

店鋪商品

省略前端代碼,主要的是@query("****************")中語句使用,配合數據庫的各種連接能實現復雜的操作。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/1666ac29eb90

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 五月婷婷俺也去开心 | 欧美一级欧美三级在线 | 成人精品一区二区三区中文字幕 | 亚洲色欲色欲综合网站 | 亚洲精品无码久久不卡 | 久久久无码精品亚洲A片猫咪 | 99这里只有精品66视频 | 午夜性色一区二区三区不卡视频 | 第四色男人天堂 | 欧美人鲁交大全 | 青青草原国产 | 青草色视频| 成人免费福利网站在线看 | www红色一片在线观看版 | 91青青在线视频 | 欧美老肥妇bbbw | 午夜神器老司机高清无码 | 美女被躁爽死 | juliaann丝袜精品系列 | 成年性香蕉漫画在线观看 | 国产成人在线播放视频 | 亚洲欧美在线免费 | 精品蜜臀AV在线天堂 | 好男人在线观看免费高清2019韩剧 | 精品一区二区三区免费毛片 | 996热在线视频 | 公妇乱淫在线播放免费观看 | 草莓视频在线观看免费 | 1717国产精品视频免费 | 亚洲第一区在线观看 | 免费在线观看亚洲 | 日本在线视| 国产亚洲精品一区在线播 | 亚洲经典激情春色另类 | 国产成人精品免费久久久久 | 四虎影视地址 | 草女人逼 | 四虎影业 | 30分钟的高清视频在线观看 | 日本视频观看 | 国产精品香蕉夜间视频免费播放 |