JAVA全系列 教程
3762个小节阅读:7088.2k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
详情页中有收藏按钮。如果用户没有收藏该产品,显示立即收藏
按钮;如果用户已经收藏该产品,显示取消收藏
按钮,所以在查询产品详情时还要查询用户是否收藏该产品。
编写ProductMapper
xxxxxxxxxx
int findFavoriteByPidAndMid(@Param("pid") Integer pid,@Param("mid") Integer mid);
编写ProductMapper.xml
xxxxxxxxxx
<select id="findFavoriteByPidAndMid" resultType="int">
SELECT COUNT(*)
FROM favorite
where pid = #{pid}
AND mid = #{mid}
</select>
编写FavoriteService
xxxxxxxxxx
@Service
public class FavoriteService {
@Autowired
private ProductMapper productMapper;
/**
* 查询用户是否收藏线路
*/
public boolean findFavorite(Integer pid,Integer mid){
int result = productMapper.findFavoriteByPidAndMid(pid, mid);
if (result == 0){ // 没有收藏
return false;
}else{
return true; // 收藏了
}
}
}
编写FrontdeskProductController
xxxxxxxxxx
// 线路详情
@RequestMapping("/routeDetail")
public ModelAndView findOne(Integer pid, HttpSession session){
ModelAndView modelAndView = new ModelAndView();
Product product = productService.findOne(pid);
// 查询用户是否收藏该线路
Object member = session.getAttribute("member");
if (member == null){ // 未登录认为未收藏
modelAndView.addObject("favorite",false);
}else { // 已登录查询是否收藏
Member member1 = (Member) member;
boolean favorite = favoriteService.findFavorite(pid, member1.getMid());
modelAndView.addObject("favorite",favorite);
}
modelAndView.addObject("product",product);
modelAndView.setViewName("/frontdesk/route_detail");
return modelAndView;
}
修改详情页
xxxxxxxxxx
<div class="opera">
<a th:href="@{/frontdesk/favorite/add(pid=${product.pid})}" th:if="!${favorite}">
<button class="btn btn-danger"><i style="vertical-align:middle;margin-right:5px" class="glyphicon glyphicon-heart-empty"></i>点击收藏</button>
</a>
<a th:href="@{/frontdesk/favorite/del(pid=${product.pid})}" th:if="${favorite}">
<button class="btn already"><i style="vertical-align:middle;margin-right:5px" class="glyphicon glyphicon-heart-empty"></i>取消收藏</button>
</a>
</div>