ssm(三)----购物车(简)

pom.xml导入一下依赖包


    
      org.springframework
      spring-context
      5.2.0.RELEASE
    

    
    
      org.springframework
      spring-webmvc
      5.2.1.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      5.2.1.RELEASE
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    

    
    
      org.mybatis
      mybatis
      3.5.2
    
    
      mysql
      mysql-connector-java
      8.0.17
    

    
    
      org.mybatis
      mybatis-spring
      2.0.1
    
    
      com.alibaba
      fastjson
      1.2.62
    

    
    
      org.thymeleaf
      thymeleaf-spring5
      3.0.9.RELEASE
    

build标签下导入

      
        ${basedir}/src/main/java
        
          **/*.xml
        
      
    

GoodsInfo.java


public class GoodsInfo {
    private Integer goodsId;
    private String goodsName;
    private Float goodsPrice;
    private Integer goodsState;
    private Integer goodsNum;//用户选择商品的数量
    private StockInfo si;

    public GoodsInfo() {
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Float getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(Float goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public Integer getGoodsState() {
        return goodsState;
    }

    public void setGoodsState(Integer goodsState) {
        this.goodsState = goodsState;
    }

    public Integer getGoodsNum() {
        return goodsNum;
    }

    public void setGoodsNum(Integer goodsNum) {
        this.goodsNum = goodsNum;
    }

    public StockInfo getSi() {
        return si;
    }

    public void setSi(StockInfo si) {
        this.si = si;
    }

    @Override
    public String toString() {
        return "GoodsInfo{" +
                "goodsId=" + goodsId +
                ", goodsName='" + goodsName + '\'' +
                ", goodsPrice=" + goodsPrice +
                ", goodsState=" + goodsState +
                ", goodsNum=" + goodsNum +
                ", si=" + si +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        GoodsInfo goodsInfo = (GoodsInfo)obj;
        return this.getGoodsId() == goodsInfo.getGoodsId();
    }
}

StockInfo.java


public class StockInfo {
    private Integer stockId;
    private Integer goodsId;
    private Integer goodsStockNum;

    public StockInfo() {
    }

    public Integer getStockId() {
        return stockId;
    }

    public void setStockId(Integer stockId) {
        this.stockId = stockId;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public Integer getGoodsStockNum() {
        return goodsStockNum;
    }

    public void setGoodsStockNum(Integer goodsStockNum) {
        this.goodsStockNum = goodsStockNum;
    }

    @Override
    public String toString() {
        return "StockInfo{" +
                "stockId=" + stockId +
                ", goodsId=" + goodsId +
                ", goodsStockNum=" + goodsStockNum +
                '}';
    }
}

GoodsMapper.java

public interface GoodsMapper {
    List findAllGoods();
    void updateStock(GoodsInfo goodsInfo);
    void addGwc(GoodsInfo goodsInfo);
    GoodsInfo findGoodsById(int goodsId);
    void deleteGwc(int goodsId);
}

GoodsMapper.xml




    
        
        
        
        
        
            
            
            
        
    
    

    
        update stock set goodsStockNum=goodsStockNum-#{goodsNum} where goodsId = #{goodsId}
    
    
        insert into gwc(userid,goodsid,gwcnumber) values(1,#{goodsId},#{goodsNum})
    
    
    
        delete from gwc where goodsId=#{goodsId}
    

GoodsBiz.java

public interface GoodsBiz {
    List findAllGoods();
    void updateStock(GoodsInfo goodsInfo);
    void addGwc(GoodsInfo goodsInfo);
    GoodsInfo findGoodsById(int goodsId);
    void deleteGwc(int goodsId);
}

GoodsBizImpl.java

@Repository
public class GoodsBizImpl implements GoodsBiz {
    @Autowired
    private GoodsMapper gm;

    @Override
    public List findAllGoods() {
        return gm.findAllGoods();
    }


    @Override
    public void updateStock(GoodsInfo goodsInfo) {
        gm.updateStock(goodsInfo);
    }

    @Override
    public void addGwc(GoodsInfo goodsInfo) {
        gm.addGwc(goodsInfo);
    }

    @Override
    public GoodsInfo findGoodsById(int goodsId) {
        return gm.findGoodsById(goodsId);
    }

    @Override
    public void deleteGwc(int goodsId) {
        gm.deleteGwc(goodsId);
    }
}

GoodsController.java

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsBiz gb;

    @RequestMapping("/findallgoods")
    public String findAllGoods(HttpSession session){
        session.setAttribute("GOODS_LIST",gb.findAllGoods());
        return "index";
    }

    @RequestMapping("/addgwc")
    public String addGwc(GoodsInfo goodsInfo,HttpSession session){
        //更新库存
        gb.updateStock(goodsInfo);
        gb.addGwc(goodsInfo);
        List list = (List) session.getAttribute("GOODS_LIST");
        int index1 =  list.indexOf(goodsInfo);
        GoodsInfo gi = list.get(index1);
        StockInfo si = gi.getSi();
        si.setGoodsStockNum(si.getGoodsStockNum()-goodsInfo.getGoodsNum());//库存量减去传过来选的数量
        session.setAttribute("GOODS_LIST",list);
        //加入购物车
        List gwcList;
        Object obj = session.getAttribute("GWC_LIST");
        if (obj == null){
            gwcList = new ArrayList<>();
        }else{
            gwcList = (List) obj;
        }
        int index2 = gwcList.indexOf(goodsInfo);
        if (index2 == -1){
            GoodsInfo g2 = gb.findGoodsById(goodsInfo.getGoodsId());
            g2.setGoodsNum(goodsInfo.getGoodsNum());
            gwcList.add(g2);
        }else{
            GoodsInfo g2 = gwcList.get(index2);
            g2.setGoodsNum(g2.getGoodsNum()+goodsInfo.getGoodsNum());
        }
        session.setAttribute("GWC_LIST",gwcList);
        return "index";
    }

    @RequestMapping("/togwc")
    public String toGwc(){
        return "gwc";
    }

    @RequestMapping("/removegwc")
    public String removeGwc(GoodsInfo gi,HttpSession session){
        List gwcList = (List) session.getAttribute("GWC_LIST");;
        gi = gwcList.get(gwcList.indexOf(gi));
        // 更新库存
        gi.setGoodsNum(0-gi.getGoodsNum());
        gb.updateStock(gi);
        // 更新购物车
        gwcList.remove(gi);
        session.setAttribute("GWC_LIST",gwcList);
        gb.deleteGwc(gi.getGoodsId());
        return "gwc";
    }
}

index.html




    
    Title
    
    


查看购物车
    
序号 商品名称 商品价格 商品数量 商品库存 操作

gwc.html




    
    Title
    
    


返回商品页面
序号 名称 价格 数量 删除

applicationContext.xml




        
        
        

        
        

        
        
                
                
                
                
        

        
        
                
                
        

        
        
                
                
        

        
        
                
                
                
                
        
        
                
        
        
                
                
        

config.xml




    
        
    

web.xml




  Archetype Created Web Application
  
    springMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:applicationContext.xml
    
  
  
    springMVC
    *.do
  

你可能感兴趣的:(SSM)