springboot CRUD product_表

1.Categoty.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.how2java.springboot.mapper.CategoryMapper">
        <select id="findAll" resultType="Category">
            select * from product_
        </select>   
    </mapper>

2.CategoryMapper

package com.how2java.springboot.mapper;
 
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.how2java.springboot.pojo.Category;
 
@Mapper
public interface CategoryMapper {
 
    @Select("select * from product_ ")
    List<Category> findAll();
     
    @Insert(" insert into product_ ( name ) values (#{name}) ")
    public int save(Category category); 
     
    @Delete(" delete from product_ where id= #{id} ")
    public void delete(int id);
         
    @Select("select * from product_ where id= #{id} ")
    public Category get(int id);
       
    @Update("update product_ set name=#{name} where id=#{id} ")
    public int update(Category category); 
 
}

3.运行

 由于只会写基础的修改,把price和cid加进去之后出现了错误,所以暂时只改了id和name,文件名也没有改变。今天先写到这里。6.8

package com.how2java.springboot.mapper;
 
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.how2java.springboot.pojo.Product;
 
@Mapper
public interface ProductMapper {
 
    @Select("select * from product_ ")
    List<Product> findAll();
     
    @Insert(" insert into product_ ( name,price ) values (#{name},#{price} ) ")
    public int save(Product product); 
     
    @Delete(" delete from product_ where id= #{id} ")
    public void delete(int id);
         
    @Select("select * from product_ where id= #{id} ")
    public Product get(int id);
       
    @Update("update product_ set name=#{name}, price=#{price} where id=#{id} ")
    public int update(Product product); 
 
}
package com.how2java.springboot.pojo;
 
public class Product {
  
    private int id;
    private String name;
    private float price;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
   
}
package com.how2java.springboot.web;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.springboot.mapper.ProductMapper;
import com.how2java.springboot.pojo.Product;
   
@Controller
public class ProductController {
    @Autowired ProductMapper productMapper;
      
    @RequestMapping("/addProduct")
    public String listProduct(Product c) throws Exception {
    	productMapper.save(c);
        return "redirect:listProduct";
    }
    @RequestMapping("/deleteProduct")
    public String deleteProduct(Product c) throws Exception {
    	productMapper.delete(c.getId());
        return "redirect:listProduct";
    }
    @RequestMapping("/updateProduct")
    public String updateProduct(Product c) throws Exception {
    	productMapper.update(c);
        return "redirect:listProduct";
    }
    @RequestMapping("/editProduct")
    public String listProduct(int id,Model m) throws Exception {
    	Product c= productMapper.get(id);
        m.addAttribute("c", c);
        return "editProduct";
    }
     
    @RequestMapping("/listProduct")
    public String listProduct(Model m,@RequestParam(value = "start", defaultValue = "1") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
        PageHelper.startPage(start,size,"id asc");
        List<Product> cs=productMapper.findAll();
        PageInfo<Product> page = new PageInfo<>(cs);
        m.addAttribute("page", page);        
        return "listProduct";
    }
     
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" isELIgnored="false"%>
  
<div style="margin:0px auto; width:500px">
  
<form action="updateProduct" method="post">
  
name: <input name="name" value="${c.name}"> <br>
price: <input name="price" values="${c.price}"><br>

<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
  
</form>
</div>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
<div align="center">
  
</div>
  
<div style="width:500px;margin:20px auto;text-align: center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>price</td>
            <td>编辑</td>
            <td>删除</td>
        </tr>
        <c:forEach items="${page.list}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td> 
                <td>${c.price}</td>
                <td><a href="editProduct?id=${c.id}">编辑</a></td>
                <td><a href="deleteProduct?id=${c.id}">删除</a></td>
            </tr>
        </c:forEach>
          
    </table>
    <br>
    <div>
                <a href="?start=1">[首  页]</a>
            <a href="?start=${page.pageNum-1}">[上一页]</a>
            <a href="?start=${page.pageNum+1}">[下一页]</a>
            <a href="?start=${page.pages}">[末  页]</a>
    </div>
    <br>
    <form action="addProduct" method="post">
      
    name: <input name="name"> <br>
    price: <input price="price"> <br>
    <input name="product.id" value="${product.id}" type="hidden"> <br><br>
    <button type="submit">提交</button>
      
    </form>
</div>

增加:

 删除:

 修改:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值