day01_springboot综合案例(二)https://developer.aliyun.com/article/1433640
添加产品
编写ProductController
/** * 新增产品 * @param product * @return */ @PostMapping("save") public String save(Product product){ this.productService.save(product); return "redirect:findAll"; }
编写ProductService
/** * 添加产品 * @param product */ void save(Product product);
/** * 添加产品 * @param product */ @Override public void save(Product product) { this.productMapper.save(product); }
编写ProductMapper
/** * 添加产品 * @param product */ void save(Product product);
编写ProductMapper.xml
<!--添加产品--> <insert id="save"> insert into product(productNum, productName, cityName, departureTime, productPrice, productDesc, productStatus) values(#{productNum}, #{productName}, #{cityName}, #{departureTime}, #{productPrice}, #{productDesc}, #{productStatus}) </insert>
产品修改
编写ProductController
/** * 修改产品 * @return */ @PostMapping("edit") public String edit( @RequestParam("id") String id, @RequestParam("productNum") String productNum, @RequestParam("productName") String productName, @RequestParam("departureTime") String departureTime, @RequestParam("cityName") String cityName, @RequestParam("productPrice") Integer productPrice, @RequestParam("productStatus") String productStatus, @RequestParam("productDesc") String productDesc){ Product product = new Product(); product.setId(id); product.setProductNum(productNum); product.setProductName(productName); product.setDepartureTimeStr(departureTime); product.setCityName(cityName); product.setProductPrice(productPrice); product.setProductStatusStr(productStatus); product.setProductDesc(productDesc); this.productService.edit(product); return "redirect:findAll"; } /** * 修改之前进行查询 * @param id * @param model * @return */ @GetMapping("editById") public String editById(@RequestParam("id") Integer id , Model model){ Product product = this.productService.editById(id); model.addAttribute("product",product); return "product-edit"; }
编写ProductService
/** * 修改产品之前进行查询用于回显 * @param pid * @return */ Product findById(Integer pid); /** * 修改产品 * @param product * @return */ void edit(Product product);
/** * 修改产品之前进行查询用于回显 * @param pid * @return */ @Override public Product findById(Integer pid) { return this.productMapper.findById(pid); } /** * 修改产品 * @param product * @return */ @Override public void edit(Product product) { this.productMapper.edit(product); }
编写ProductMapper
/** * 修改产品之前进行查询用于回显 * @param pid * @return */ Product findById(Integer pid); /** * 修改产品 * @param product * @return */ void edit(Product product);
编写ProductMapper.xml
<!--修改产品之前进行查询--> <select id="findById" resultType="Product"> select * from product where id=#{pid} </select> <!--修改产品--> <update id="edit"> update product set productNum=#{productNum}, productName=#{productName}, cityName=#{cityName}, departureTime=#{departureTime}, productPrice=#{productPrice}, productDesc=#{productDesc}, productStatus=#{productStatus} where id=#{id} </update>
产品详情
编写ProductController
/** * 产品详情 * @param id * @param model * @return */ @GetMapping("findById") public String findById(@RequestParam("id") Integer id, Model model){ Product product = this.productService.editById(id); model.addAttribute("product",product); return "product-show"; }
编写ProductService
/** * 修改产品之前进行查询用于回显,之前修改写过无需再写 * @param pid * @return */ Product findById(Integer pid);
/** * 修改产品之前进行查询用于回显 * @param pid * @return */ @Override public Product findById(Integer pid) { return this.productMapper.findById(pid); }
编写ProductMapper
/** * 修改产品之前进行查询用于回显 * @param pid * @return */ Product findById(Integer pid);
编写ProductMapper.xml
<!--修改产品之前进行查询--> <select id="findById" resultType="Product"> select * from product where id=#{pid} </select>
产品删除
编写ProductController
/** * 指定删除 * @param id * @return * TODO 待优化:订单中有产品的相关信息,产品删除了,订单中依然有产品的信息 */ @GetMapping("deleteById") public String deleteById(@RequestParam("id") Integer id){ this.productService.deleteById(id); return "redirect:findAll"; }
编写ProductService
/** * 删除产品 * @param pid * @return */ void deleteById(Integer pid);
@Service public class ProductServiceImpl implements ProductService { @Autowired private ProductMapper productMapper; @Autowired private OrderMapper orderMapper; /** * 删除产品 * @param pid * @return */ @Override public void deleteById(Integer pid) { //删除订单中商品外键 this.orderMapper.updateById(pid); //删除商品本身 this.productMapper.deleteById(pid); } }
编写ProductMapper
/** * 删除订单中的商品 */ void updateById(Integer pid);
/** * 删除产品 * @param pid * @return */ void deleteById(Integer pid);
编写ProductMapper.xml
<!--删除产品--> <delete id="deleteById"> delete from product where id=#{pid} </delete>
<?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="cn.yanqi.ssm.mapper.OrderMapper"> <!--删除订单中商品--> <update id="updateById"> update orders set productId = null where productId = #{id} </update> </mapper>
批量删除
/** * 批量删除 * @param ids * @return */ @PostMapping("selectDelete") public String selectDelete(@RequestParam("ids") int[] ids){ for (int id : ids) { this.productService.deleteById(id); } return "redirect:findAll"; }
tMapper;
@Autowired private OrderMapper orderMapper; /** * 删除产品 * @param pid * @return */ @Override public void deleteById(Integer pid) { //删除订单中商品外键 this.orderMapper.updateById(pid); //删除商品本身 this.productMapper.deleteById(pid); }
}
### 编写ProductMapper ```java /** * 删除订单中的商品 */ void updateById(Integer pid);
/** * 删除产品 * @param pid * @return */ void deleteById(Integer pid);
编写ProductMapper.xml
<!--删除产品--> <delete id="deleteById"> delete from product where id=#{pid} </delete>
<?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="cn.yanqi.ssm.mapper.OrderMapper"> <!--删除订单中商品--> <update id="updateById"> update orders set productId = null where productId = #{id} </update> </mapper>
批量删除
/** * 批量删除 * @param ids * @return */ @PostMapping("selectDelete") public String selectDelete(@RequestParam("ids") int[] ids){ for (int id : ids) { this.productService.deleteById(id); } return "redirect:findAll"; }