pom.xml 文件中的配置和上一篇的配置相同,数据表自行创建
一、对 Product 表进行 CRUD 操作
1、创建实体类 Product
与数据表中的字段名对应
package com.how2java.springboot.pojo;
public class Product {
public int id;
public String name;
public float price;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return this.price;
}
public void setPrice(float price) {
this.price = price;
}
}
2、创建映射类 ProductMapper
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")
public List<Product> list();
@Insert("insert into product(name,price) values(#{name},#{price})")
public void add(Product product);
@Delete("delete from product where id = #{id}")
public void delete(Product product);
@Update("update product set name = #{name},price = #{price} where id = #{id}")
public void update(Product product);
}
3、创建控制器类 ProductController
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 addProduct(Product product) {
productMapper.add(product);
return "redirect:listProduct";
}
@RequestMapping("/deleteProduct")
public String deleteProduct(Product product) {
productMapper.delete(product);
return "redirect:listProduct";
}
@RequestMapping("/editProduct")
public String editProduct(Product product, Model m) {
m.addAttribute("product", product);
return "editProduct";
}
@RequestMapping("/updateProduct")
public String updateProduct(Product product) {
productMapper.update(product);
return "redirect:listProduct";
}
@RequestMapping("/listProduct")
public String listProduct(Model m, @RequestParam(value = "start", defaultValue = "0") int start,
@RequestParam(value = "size", defaultValue = "5") int size) {
// 启动 PageHelper
PageHelper.startPage(start, size, "id asc");
List<Product> listproList = productMapper.list();
PageInfo<Product> pageInfo = new PageInfo<Product>(listproList);
m.addAttribute("page", pageInfo);
return "listProduct";
}
}
4、创建 listProduct.jsp 页面
<%@ 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>s
<br>
<div>
<a href="?start=1">[首 页]</a>
<a href="?start=${page.pageNum-1 < 1 ? 1 : page.pageNum-1}">[上一页]</a>
<a href="?start=${page.pageNum+1 < page.pages ? page.pageNum+1 : page.pages}">[下一页]</a>
<a href="?start=${page.pages}">[末 页]</a>
</div>
<br>
<form action="addProduct" method="post">
name: <input name="name"> <br>
price:<input name="price"><br>
<button type="submit">提交</button>
</form>
</div>
5、创建 editProduct.jsp 页面
<%@ 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"> <br>
price:<input name="price"><br>
<input name="id" type="hidden" value="${product.id}">
<button type="submit">提交</button>
</form>
</div>
运行 Application 类,在浏览器中输入 http://127.0.0.1:8080/listProduct 就可以成功运行了,具体实现功能和 SSM 中实现的功能相同
这样 SpringBoot 基于 Mybatis 注解方式的 CRUD 练习就完成了