SpringBoot Data JPA 关联表查询的方法
作者:Duebasso 时间:2021-08-08 13:59:23
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
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.
*/
@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来实现方法
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
实现具体数据操作操作
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
标签:SpringBoot,Data,JPA
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java编程实现获取当前代码行行号的方法示例
2021-06-28 06:51:14
Spring集成Quartz的简单配置的方法
2023-08-24 02:52:27
Java 8中字符串拼接新姿势StringJoiner详解
2022-03-09 03:00:28
Springboot整合支付宝支付功能
2023-07-02 17:38:09
解读@RequestBody与post请求的关系
2022-10-07 02:02:51
![](https://img.aspxhome.com/file/2023/7/95037_0s.png)
Unity3D运行报DllNotFoundException错误的解决方案
2021-09-13 00:22:35
![](https://img.aspxhome.com/file/2023/0/78150_0s.png)
Android 判断日期是否在一年以内的算法实例
2023-03-22 06:04:13
java实现上传网络图片到微信临时素材
2022-09-21 08:43:23
Java中的AQS同步队列问题详解
2021-08-16 19:50:23
![](https://img.aspxhome.com/file/2023/3/82003_0s.jpg)
feign 如何获取请求真实目的ip地址
2021-08-13 15:47:49
![](https://img.aspxhome.com/file/2023/1/76281_0s.jpg)
Java自定义异常类的实例详解
2021-10-21 07:18:33
Java中对象与C++中对象的放置安排的对比
2022-05-31 15:07:18
Android开发Compose框架使用开篇
2023-06-08 19:14:19
![](https://img.aspxhome.com/file/2023/8/94988_0s.jpg)
Android TextView中文字通过SpannableString设置属性用法示例
2023-07-26 07:11:51
![](https://img.aspxhome.com/file/2023/8/84748_0s.jpg)
java语言自行实现ULID过程底层原理详解
2023-04-13 22:38:50
![](https://img.aspxhome.com/file/2023/7/69097_0s.png)
idea maven pom不自动更新的解决方法
2021-11-19 14:56:46
![](https://img.aspxhome.com/file/2023/9/62209_0s.jpg)
Spring IOC基于注解启动示例详析
2022-04-19 22:49:55
![](https://img.aspxhome.com/file/2023/9/87219_0s.jpg)
java发送kafka事务消息的实现方法
2022-05-17 01:18:35
![](https://img.aspxhome.com/file/2023/4/75514_0s.png)
基于JAVA中Jersey处理Http协议中的Multipart的详解
2021-06-15 13:43:57
mybatis-plus中lambdaQuery()与lambdaUpdate()比较常见的使用方法总结
2023-11-24 22:43:04
![](https://img.aspxhome.com/file/2023/6/60156_0s.jpg)