Jpa多表查询

这篇博客详细介绍了如何使用JPA进行多表查询,包括model层的Food类设计,repository层的TypeItemRepository接口定义,以及controller层的TypeItemController实现。通过示例API展示了商品列表、创建订单、订单列表、查询订单详情、取消订单等操作,同时提到了解决1+N问题的策略。博客还包含了数据模型Result和CodeAndMsg的定义,以及一个简单的测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  1. 作业题目~~~~

API
###商品列表

GET /sell/buyer/product/list
参数


返回

{
“code”: 0,
“msg”: “成功”,
“data”: [
{
“name”: “热榜”,
“type”: 1,
“foods”: [
{
“id”: “123456”,
“name”: “皮蛋粥”,
“price”: 1.2,
“description”: “好吃的皮蛋粥”,
“icon”: “http://xxx.com”,
}
]
},
{
“name”: “好吃的”,
“type”: 2,
“foods”: [
{
“id”: “123457”,
“name”: “慕斯蛋糕”,
“price”: 10.9,
“description”: “美味爽口”,
“icon”: “http://xxx.com”,
}
]
}
]
}
###创建订单

POST /sell/buyer/order/create
参数

name: “张三”
phone: “18868822111”
address: “慕课网总部”
openid: “ew3euwhd7sjw9diwkq” //用户的微信openid
items: [{
productId: “1423113435324”,
productQuantity: 2 //购买数量
}]

返回

{
“code”: 0,
“msg”: “成功”,
“data”: {
“orderId”: “147283992738221”
}
}
###订单列表

GET /sell/buyer/order/list
参数

openid: 18eu2jwk2kse3r42e2e
page: 0 //从第0页开始
size: 10
返回

{
“code”: 0,
“msg”: “成功”,
“data”: [
{
“orderId”: “161873371171128075”,
“buyerName”: “张三”,
“buyerPhone”: “18868877111”,
“buyerAddress”: “慕课网总部”,
“buyerOpenid”: “18eu2jwk2kse3r42e2e”,
“orderAmount”: 0,
“orderStatus”: 0,
“payStatus”: 0,
“createTime”: 1490171219,
“updateTime”: 1490171219,
“orderDetailList”: null
},
{
“orderId”: “161873371171128076”,
“buyerName”: “张三”,
“buyerPhone”: “18868877111”,
“buyerAddress”: “慕课网总部”,
“buyerOpenid”: “18eu2jwk2kse3r42e2e”,
“orderAmount”: 0,
“orderStatus”: 0,
“payStatus”: 0,
“createTime”: 1490171219,
“updateTime”: 1490171219,
“orderDetailList”: null
}]
}
###查询订单详情

GET /sell/buyer/order/detail
参数

openid: 18eu2jwk2kse3r42e2e
orderId: 161899085773669363
返回

{
“code”: 0,
“msg”: “成功”,
“data”: {
“orderId”: “161899085773669363”,
“buyerName”: “李四”,
“buyerPhone”: “18868877111”,
“buyerAddress”: “慕课网总部”,
“buyerOpenid”: “18eu2jwk2kse3r42e2e”,
“orderAmount”: 18,
“orderStatus”: 0,
“payStatus”: 0,
“createTime”: 1490177352,
“updateTime”: 1490177352,
“orderDetailList”: [
{
“detailId”: “161899085974995851”,
“orderId”: “161899085773669363”,
“productId”: “157875196362360019”,
“productName”: “招牌奶茶”,
“productPrice”: 9,
“productQuantity”: 2,
“productIcon”: “http://xxx.com”,
“productImage”: “http://xxx.com
}
]
}
}
###取消订单

POST /sell/buyer/order/cancel
参数

openid: 18eu2jwk2kse3r42e2e
orderId: 161899085773669363
返回

{
“code”: 0,
“msg”: “成功”,
“data”: null
}
###获取openid

重定向到 /sell/wechat/authorize
参数

returnUrl: http://xxx.com/abc //【必填】
返回

http://xxx.com/abc?openid=oZxSYw5ldcxv6H0EU67GgSXOUrVg
###支付订单

重定向 /sell/pay/create
参数

orderId: 161899085773669363
returnUrl: http://xxx.com/abc/order/161899085773669363
返回

http://xxx.com/abc/order/161899085773669363

5个表
创建第一个表
package com.zhj.foodorder.result;

import lombok.Data;

@Data
public class Result{
private int code;
private String msg;
private T data;
/**
* 构造函数
*/
private Result(T data) {
this.data = data;
}

private Result(int code, String msg) {
    this.code = code;
    this.msg = msg;
}

private Result(CodeAndMsg codeAndMsg) {
    if(codeAndMsg != null) {
        this.code = codeAndMsg.getCode();
        this.msg = codeAndMsg.getMsg();
    }
}


/**
 *  成功时候的调用
 * */
public static  <T> Result<T>  success(T data){
    return new Result<T>(data);
}

/**
 *  失败时候的调用
 * */
public static  <T> Result<T>  error(CodeAndMsg codeAndMsg){
    return new Result<T>(codeAndMsg);
}

}

package com.zhj.foodorder.result;

public class CodeAndMsg {
private int code;
private String msg;

//构造函数
private CodeAndMsg( ) {
}

private CodeAndMsg( int code,String msg ) {
    this.code = code;
    this.msg = msg;
}

//通用的错误码
public static CodeAndMsg SUCCESS = new CodeAndMsg(0, "success");
public static CodeAndMsg SERVER_ERROR = new CodeAndMsg(500100, "服务端异常");
public static CodeAndMsg BIND_ERROR = new CodeAndMsg(500101, "参数校验异常:%s");
public static CodeAndMsg REQUEST_ILLEGAL = new CodeAndMsg(500102, "请求非法");
public static CodeAndMsg ACCESS_LIMIT_REACHED= new CodeAndMsg(500104, "访问太频繁!");




public int getCode() {
    return code;
}
public void setCode(int code) {
    this.code = code;
}
public String getMsg() {
    return msg;
}
public void setMsg(String msg) {
    this.msg = msg;
}

}


model层创建food类


@Entity
@Data
public class Food {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增
private Long id;
private String name;
private double price;
private String description;
private String icon;
@ManyToOne(cascade=CascadeType.ALL)//解决1+N,级联用ALL
@JoinColumn(name=“type_item_type”)//指定外键名
@JsonBackReference
private TypeItem typeItem;

}


repository层


public interface TypeItemRepository extends CrudRepository<TypeItem,Long> {
public List findAllByOrderByTypeDesc();

}


controller层


@RestController
public class TypeItemController {
@Resource
private TypeItemRepository typeItemRepository;
@RequestMapping(“hanhanniu”)
public Iterable findAll(){
return typeItemRepository.findAll();
}

第一个创建完成了,去测试一下
结果如下
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值