springclouddemo3.3-整合menu

从b站学习springcloud项目,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒

b站链接:https://www.bilibili.com/video/av55629580?p=1
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru

上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103593066
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103600462

下面的内容总结:
修改menu/MenuApplication.java
在menu/entity中新建type视图类,在entity/Menu添加type→在client对entity同样的操作→在menu/reposotory添加TypeRepository.xml→index.html

实现细节:
1.进入 http://localhost:8030/client/redirect/index
在这里插入图片描述
可以看到有18条记录,但是它还可以往后翻页,当然后面的页面就是空的了,现在后面空的页面我不想让它出现!但是为什么是10页呢?
在这里插入图片描述
因为100条 / 10条 = 10页
100 和 10是menu/MenuHandler.java中findAll给的死数据,故修改之:将100改为menuRepository.count()
menu/MenuHandler.java中findAll改为:

@GetMapping("/findAll/{index}/{limit}")
    public MenuVO findAll(@PathVariable("index") int index , @PathVariable("limit") int limit){
        return new  MenuVO(0,"",menuRepository.count(),menuRepository.findAll(index , limit));
    }

2.进入http://localhost:8030/client/redirect/index
在这里插入图片描述
这样页数就对了!

分类是三鲜,这是死数据,所以现在实现两个表的关联查询,来修改分类:
1.在menu/entity中新建Type.java,加入代码:

package com.southwind.entity;

import lombok.Data;

@Data
public class Type {
    private long id;
    private String name;
}

在menu/entity/Menu中添加private Type type;

package com.southwind.entity;

import lombok.Data;

@Data
public class Menu {
    private long id;
    private String msg;
    private double price;
    private String flavor;
    private Type type;
}

同时在client/entity中同样的新建Type和修改Menu,操作一样。
2.在menu/repository中新建接口TypeRepository.java加入代码:

package com.southwind.repository;

import com.southwind.entity.Type;

import java.util.List;

public interface TypeRepository {
    public Type findById(long id);
    public List<Type> findAll();
}

3.在menu/resources中复制MenuRepository.xml,但改名字为TypeRepository.xml,修改内容:
a.< select >中只保留findById
b.< mapper>中后面的MenuRepository改为TypeRepository
c.< select> 中resultType=“Menu” 改为 resultType=“Type”
且t_menu 改为 t_type

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.repository.TypeRepository">

	<select id="findById" parameterType="long" resultType="Type">
		select * from t_type where id = #{id}
	</select>
</mapper>

4.修改menu/resources/mapping/MenuRepository.xml
在< select>上面添加:

	<resultMap id="menuMap" type="menu">
		<id column="id" property = "id"></id>
		<result column="name" property="name"></result>
		<result column="price" property="price"></result>
		<result column="flavor" property="flavor"></result>
		<association property="type" select="com.southwind.repository.TypeRepository.findById" column="tid"></association>
	</resultMap><!-- association 中的column是传入字段 -->

修改< select> 中findAll:
resultType=“Menu” 改为 resultMap=“menuMap”,其他的< select>不变

5.重启menu,进入http://localhost:8020/menu/findAll/0/10
在这里插入图片描述
就成功分类了
6.回到client/index.html中修改代码:
35-37行代码中将return ‘三鲜’ 改为 return data.type.name

重启client 再次进入 http://localhost:8030/client/redirect/index
在这里插入图片描述
分类就好了!

上一节链接:
https://blog.csdn.net/qq_40893824/article/details/103593066
下一节链接:
https://blog.csdn.net/qq_40893824/article/details/103600462

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值