springboot data jpa實現 一對多、多對一關聯表查詢
開發環境
- idea 2017.1
- java1.8
- springboot 2.0
- mysql 5.x
功能需求
通過關聯關系查詢商店store中所有的商品shop,商店對商品一對多,商品對商店多對一,外鍵 store_id存在于多的一方。使用數據庫的內連接語句。
表結構
tb_shop
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" ; } } |
前臺頁面跳轉
查看的店鋪
店鋪商品
省略前端代碼,主要的是@query("****************")中語句使用,配合數據庫的各種連接能實現復雜的操作。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/1666ac29eb90