SpringBoot Data JPA實現一對多、多對一關聯表查詢
開發環境
功能需求
通過關聯關係查詢商店Store中所有的商品Shop,商店對商品一對多,商品對商店多對一,外鍵store_id存在於多的一方。使用數據庫的內連接語句。
表結構
tb_shop
tb_store
實體類,通過註解實現
1.商店類Store.java
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
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語句完成複雜的操作。
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. */@Repositorypublic 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來實現方法
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@Transactionalpublic class StoreService { @Autowired private StoreDao storeDao; /** * 展示商店商品* */ public List<Store> findByShopList(Integer id){ return storeDao.findByShopList(id); }} StoreAction.java
實現具體數據操作操作
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("****************")中語句使用,配合數據庫的各種連接能實現複雜的操作。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。