购物车加减商品后台

来源:互联网 发布:centos 7 ftp服务器 编辑:程序博客网 时间:2024/04/30 19:40

购物车加减

dao层

public interface GoodsMapper {


    //定义查询所有商品的方法
    public List<Goods> selectGoods();
    
    //定义根据商品ID 查询数量的方法
    public Integer selectCount(Integer id);
    
    //定义一个根据商品ID 修改数量的方法
    public void updateCount(@Param("id")Integer id,@Param("count")Integer count);
    
    //定义根据商品ID 删除信息的方法
    public void delete(Integer id);
}

<!-- 定义查询所有商品的方法 -->
    <select id="selectGoods" resultType="goods">
        select * from t_goods
    </select>
    
    <!-- 定义根据商品ID 查询数量的方法 -->
    <select id="selectCount" parameterType="int" resultType="int">
        select account from t_goods where id=#{id}
    </select>
    
    <!-- 定义一个根据商品ID修改数量的方法 -->
    <update id="updateCount" >    
        update t_goods set account=#{count} where id=#{id}
    </update>
    <!-- 定义根据商品ID 删除信息的方法 -->
    <delete id="delete" parameterType="int" >
        delete from t_goods where id=#{id}
    </delete>

service层

public interface GoodsService {

    //定义查询所有商品的方法
    public List<Goods> select();
    
    //定义一个根据商品ID增加数量的方法
    public void jiaCount(Integer id);
    
    //定义一个根据商品ID删减数量的方法
    public void jianCount(Integer id);
    
    //定义根据商品ID 修改数量的方法
    public void updateCount(Integer id,Integer count);
    
    
    //定义根据商品id删除的方法
    public void delete(Integer id);
}

实现类

public class GoodsServiceimpl implements GoodsService{

    //注入工具类
    @Autowired
    private GoodsMapper gm;
    
    
    //定义查询所有商品的方法
    @Override
    public List<Goods> select() {
        // TODO Auto-generated method stub
        //调用查询所有商品的方法
        List<Goods> list = gm.selectGoods();
        
        return list;
    }


    //实现增加数量的方法
    @Override
    public void jiaCount(Integer id) {
        // TODO Auto-generated method stub
        
        //调用根据ID查询数量的方法
        Integer cou = gm.selectCount(id);
        
        gm.updateCount(id, cou+1);
    }


    //实现删减数量的方法
    @Override
    public void jianCount(Integer id) {
        // TODO Auto-generated method stub
        
        //定义根据ID查询数量的方法
        Integer cou = gm.selectCount(id);
        
        gm.updateCount(id, cou-1);
    }


    //定义根据商品ID 删除商品的方法
    @Override
    public void delete(Integer id) {
        // TODO Auto-generated method stub
        //调用删除的方法
        gm.delete(id);
    }


    //定义根据用户ID 修改数量的方法
    @Override
    public void updateCount(Integer id, Integer count) {
        // TODO Auto-generated method stub
        
        //调用修改的方法
        gm.updateCount(id, count);
        
    }


}

controller层



@Controller
@RequestMapping("goods")
public class GoodsController {

    //注入服务类
    @Autowired
    private GoodsService gs;
    
    //定义查询的方法
    @RequestMapping("select")
    @ResponseBody
    public List select(HttpServletRequest request)
    {
        //调用方法
        List<Goods> list = gs.select();
        
        return list;
    }
    
    //定义增加数量的方法
    @RequestMapping("jia")
    @ResponseBody
    public String jiacou(Integer id)
    {
        //调用增加数量的方法
        gs.jiaCount(id);
        
        return "ok";
    }
    //定义删减的方法
    @RequestMapping("jian")
    @ResponseBody
    public String jiancou(Integer id)
    {
        //调用删减数量的方法
        gs.jianCount(id);
        return "ok";
        
    }
    
    //定义删除的方法
    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id)
    {
        //调用删除的方法
        gs.delete(id);
        
        return "ok";
    }
    
    //定义根据商品ID 修改数量的方法
    @RequestMapping("updateCount")
    @ResponseBody
    public String uupdateCount(Integer id,Integer count)
    {
        //调用方法
        gs.updateCount(id, count);
        
        return "ok";
    }
}