目录
一.前台产品详情
1.查询产品
2.收藏按钮
二.前台收藏
本章的代码会统一放到最后。
一.前台产品详情
1.查询产品
即点击“查看详情”即可得到这个产品的详细描述。
并且项目运行起来之后,可以在后台新建新的产品,在前台可以实时查询到。
2.收藏按钮
详情页中有收藏按钮。如果用户没有收藏该产品,显示立即收藏按钮;如果用户已经收藏该产品,显示取消收藏按钮,所以在查询产品详情时还要查询用户是否收藏该产品。
二.前台收藏
***************************************************************************************************
下面是相关代码和前台页面展示:
ProductMapper.java:
public interface ProductMapper extends BaseMapper<Product> {
//查询产品,参数是一个查询条件,即(page,size)
Page<Product> findProductPage(Page<Product> page);
Product findOne(int pid);
int findFavoritePidAndMid(@Param("pid")Integer pid,@Param("mid")Integer mid);
void addFavorite(@Param("pid")Integer pid,@Param("mid")Integer mid);
void delFavorite(@Param("pid")Integer pid,@Param("mid")Integer mid);
Page<Product> findMemberFavorite(Page<Product> page,Integer mid);
}
ProductMapper.xml:
<?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.first.travel.mapper.ProductMapper">
<resultMap id="productMapper" type="com.first.travel.pojo.Product">
<id property="pid" column="pid"></id>
<result property="productName" column="productName"></result>
<result property="price" column="price"></result>
<result property="status" column="status"></result>
<result property="hotline" column="hotline"></result>
<result property="productDesc" column="productDesc"></result>
<result property="pImage" column="pImage"></result>
<result property="cid" column="cid"></result>
<association property="category" column="cid" javaType="com.first.travel.pojo.Category">
<id property="cid" column="cid"></id>
<result property="cname" column="cname"></result>
</association>
</resultMap>
<select id="findProductPage" resultMap="productMapper">
SELECT *
FROM product
LEFT JOIN category ON product.cid = category.cid
ORDER BY product.pid DESC
</select>
<select id="findOne" resultMap="productMapper">
SELECT *
FROM product
LEFT JOIN category ON product.cid = category.cid
where pid = #{pid}
</select>
<select id="findFavoritePidAndMid" resultType="int">
SELECT COUNT(*)
FROM favorite
where pid = #{pid}
AND mid = #{mid}
</select>
<insert id="addFavorite">
INSERT INTO favorite(pid,mid) values(#{pid},#{mid})
</insert>
<delete id="delFavorite">
DELETE from favorite where pid=#{pid} and mid=#{mid}
</delete>
<select id="findMemberFavorite" parameterType="int" resultType="com.first.travel.pojo.Product">
SELECT product.*
FROM product LEFT JOIN favorite
on product.pid = favorite.pid
WHERE favorite.mid = #{mid}
</select>
</mapper>
FavoriteService.java:
@Service
public class FavoriteService {
@Autowired
private ProductMapper productMapper;
// 查询用户是否收藏线路
public boolean findFavorite(Integer pid,Integer mid){
int result = productMapper.findFavoritePidAndMid(pid, mid);
if(result == 0){
return false;
}else {
return true;
}
}
// 收藏
public void addFavorite(Integer pid,Integer mid){
productMapper.addFavorite(pid,mid);
}
// 取消收藏
public void delFavorite(Integer pid,Integer mid){
productMapper.delFavorite(pid,mid);
}
// 我的收藏
public Page<Product> findMemberFavorite(int page,int size,Integer mid){
Page favoriteProduct = productMapper.findMemberFavorite(new Page(page, size), mid);
return favoriteProduct;
}
}
FavoriteController.java:
package com.first.travel.controller.frontdesk;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.travel.pojo.Member;
import com.first.travel.pojo.Product;
import com.first.travel.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/frontdesk/favorite")
public class FavoriteController {
@Autowired
private FavoriteService favoriteService;
// 收藏
@RequestMapping("/add")
public String add(Integer pid, HttpSession session, @RequestHeader("Referer")String referer){
Member member = (Member)session.getAttribute("member");
favoriteService.addFavorite(pid,member.getMid());
return "redirect:"+referer;
}
// 取消收藏
@RequestMapping("/del")
public String del(Integer pid, HttpSession session, @RequestHeader("Referer")String referer){
Member member = (Member)session.getAttribute("member");
favoriteService.delFavorite(pid,member.getMid());
return "redirect:"+referer;
}
// 我的收藏
@RequestMapping("/myFavorite")
public ModelAndView myFavorite(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size,
HttpSession session){
ModelAndView modelAndView = new ModelAndView();
Member member = (Member) session.getAttribute("member");
Page<Product> productPage = favoriteService.findMemberFavorite(page, size, member.getMid());
modelAndView.addObject("productPage",productPage);
modelAndView.setViewName("/frontdesk/my_favorite");
return modelAndView;
}
}
前端代码略。
接下来展示实现的功能:
没登陆之前点击某个产品的的‘查看详情’,会自动跳转到登陆界面让用户登录之后才能收藏
登录之后,进入产品详情界面,即可点击收藏
收藏之后还可以取消收藏