从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