将数据库商品数据进行-增.删.改.查

一、创建项目并添加依赖

*创建项目并设置基本信息
*指定项目核心依赖
image
*项目结构
image
*项目配置文件
image

二、业务实现

*Pojo类定义

*Dao接口方法及映射定义

package com.cy.pj.goods.dao;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.cy.pj.goods.pojo.Goods;

@Mapper
public interface GoodsDao {
    //查询
    @Select("select * from tb_goods")
    List<Goods> findGoods();
    //删除
    @Delete("delete from tb_goods where id=#{id}")
    int deleteById(Integer id);
    //添加
    @Insert("insert into tb_goods(name,remark,createdTime) value(#{name},#{remark},now())")
    int insertObject(Goods entity);
    //修改
    @Select("select * from tb_goods where id=#{id}")
    Goods findById(Integer id);
    @Update("update tb_goods set name=#{name},remark=#{remark} where id=#{id} ")
    int updateGoods(Goods goods);
}

*Service接口方法定义及实现

package com.cy.pj.goods.service;

import java.util.List;
import com.cy.pj.goods.pojo.Goods;

public interface GoodsService {
    //查询
    List<Goods> findGoods();
    //删除
    int deleteById(Integer id);    
    //添加
    int saveGoods(Goods entity);
    //修改
    Goods findById(Integer id);
    int updateGoods(Goods goods);
}

*定义接口实现类GoodsServiceImpl及方法实现

package com.cy.pj.goods.service.impl;

import java.util.List;
importorg.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cy.pj.goods.dao.GoodsDao;
import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Override  //查询
    public List<Goods> findGoods() {
        return goodsDao.findGoods();
    }
    @Override  //删除
    public int deleteById(Integer id) {
        int rows = goodsDao.deleteById(id);
        return rows;
    }
    @Override  //添加
    public int saveGoods(Goods entity) {
        int rows = goodsDao.insertObject(entity);
        return rows;
    }
    @Override  //修改
    public Goods findById(Integer id) {        
        return goodsDao.findById(id);
    }
    @Override
    public int updateGoods(Goods goods) {        
        return goodsDao.updateGoods(goods);
    }
    
}

*Controller对象方法定义及实现

package com.cy.pj.goods.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;

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

    @Autowired
    private GoodsService goodsService;
    @RequestMapping("doGoodsUI")  //查詢
    public String doGoodsUI(Model model) {
        List<Goods> list = goodsService.findGoods();
        model.addAttribute("list", list);
        return "goods";        
    }
    
    @RequestMapping("doDeleteById/{id}")   //刪除
    public String doDeleteById(@PathVariable Integer id) {
        goodsService.deleteById(id);
        return "redirect:/goods/doGoodsUI";        
    }
    
    @RequestMapping("doSaveGoods")   //添加
    public String doSaveGoods(Goods entity) {
        goodsService.saveGoods(entity);
        return "redirect:/goods/doGoodsUI";        
    }
    @RequestMapping("doGoodsAddUI")
    public String doGoodsAddUI() {
        return "goods-add";        
    }
    
    @RequestMapping("doUpdateGoods")   //修改
    public String doUpdateGoods(Goods goods) {
        goodsService.updateGoods(goods);
        return "redirect:/goods/doGoodsUI";        
    }
    @RequestMapping("doFindById/{id}")
    public String doFindById(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        return "goods-update";        
    }
}

*设计Goods列表页面及实现

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>The Goods Page</h1>
    <table width="50%">
    <a th:href="@{/goods/doGoodsAddUI}">添加商品</a>  
      <thead>
         <th>id</th>
         <th>name</th>
         <th>remark</th>
         <th>createdTime</th>
         <th colspan="2">operation</th>
       </thead>
       <tbody>
         <tr th:each="g:${list}">
           <td th:text="${g.id}">1</td>
           <td th:text="${g.name}">fbb</td>
           <td th:text="${g.remark}">fbbbb</td>
           <td th:text="${#dates.format(g.createdTime,'yyyy/MM/dd HH:mm')}">2020/09/01</td>
           <td><a th:href="@{/goods/doDeleteById/{id}(id=${g.id})}">delete</a></td>
           <td><a th:href="@{/goods/doFindById/{id}(id=${g.id})}">update</a></td>
         </tr>   
       </tbody>
    </table>
</body>
</html>

*增--创建goods-add.html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
ul li {list-style-type:none}
</style>
</head>
<body>
  <h1>The Goods Add Page</h1>
    <form th:action="@{/goods/doSaveGoods}" method="post">
    <ul>
      <li>name:
      <li><input type="text" name="name">
      <li>remark:
      <li><textarea rows="5" cols="50" name="remark"></textarea>
      <li><input type="submit" value="Save">
    </ul>
    </form>
</body>
</html>

*改--创建goods-update.html页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
 ul li {list-style-type:none}
</style>
</head>
<body>
    <h1>The Goods Update Page</h1>
    <form th:action="@{/goods/doUpdateGoods}" method="post">
        <input type="hidden" name="id" th:value="${goods.id}">
        <ul>
        <li>name:
        <li><input type="text" name="name" th:value="${goods.name}">
        <li>remark:
        <li><textarea rows="3" cols="30" name="remark" th:text="${goods.remark}"></textarea>
        <li><input type="submit" value="Update Goods">
        </ul>
    </form>
</body>
</html>
三、易错分析
  • 404--服务器依据请求资源路径找不到对应的资源。
    1)错误原因:

            a,请求地址写错了
            b,<servlet-name>不一致
 2)解决方式:
             a,依据http://ip:port/appname/servlet-url检查请求地址。
             b,检查/.xml文件。
* 500--运行时出错。
1)错误原因:
            a,配置文件类名写错了。
            b,程序代码写错。
 2)解决方式:
            a,检查/.xml文件。有没有把类名写错(必须是完整类名)
            b,检查程序代码。
*


xiaowei
1 声望1 粉丝

下一篇 »
Ajax 小结