对product_
本操作在系列教材(十四)提供的代码的基础上,仿照系列教材十七
对product_表进行增删改查。
一. 对系列教材(十四)原有代码进行修改
系列教材十四是展现出category_表,在对product进行修改之前,先得展现出product_表,再对其进行CRUD操作。
1.application.properties
product_表和category_表在同一数据库how2java中,因此数据库相关配置,如数据库名,用户名,密码等都不需要修改。
2.pom.xml
pom.xml中增加对mysql和MyBatis的支持,也是通用的,不需要修改。
3.Category改为Product类
核心就是增加price和cid两个变量以及对应的get, set方法。
package com.how2java.springboot.pojo;
public class Product {
private int id;
private String name;
private float price;
private int cid;
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;
}
public int getCid(){
return cid;
}
public void setCid(int cid){
this.cid = cid;
}
}
4.CategoryMapper改为ProductMapper
核心代码:
package com.how2java.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.how2java.springboot.pojo.Product;
@Mapper
public interface ProductMapper {
@Select("select * from product_ ")
List<Product> findAll();
}
5.CategoryController改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 com.how2java.springboot.mapper.ProductMapper;
import com.how2java.springboot.pojo.Product;
@Controller
public class ProductController {
@Autowired ProductMapper ProductMapper;
@RequestMapping("/listProduct")
public String listCategory(Model m) throws Exception {
List<Product> cs=ProductMapper.findAll();
m.addAttribute("cs", cs);
return "listProduct";
}
}
6.listCategory.jsp改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”%>
id | name | price | cid |
${c.id} | ${c.name} | ${c.price} | ${c.cid} |
7.测试
相应修改完成后,访问网址可正常显示product_表
到此为止实现了对product_表在页码上的展现,接下来实现对表的增删改查操作。
二.对系列教材十七原有代码修改
1. pom.xml
添加支持pagehelper的依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
2.PageHelperConfig
这个文件写PageHelper插件的相关配置,也不需要修改。
3.CategoryMapper修改为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_ ")
List<Product> findAll();
@Insert(" insert into product_ ( name ) values (#{name}) ")
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} where id=#{id} ")
public int update(Product product);
}
4.CategoryController修改为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 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 = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
PageHelper.startPage(start,size,"id desc");
List<Product> cs=productMapper.findAll();
PageInfo<Product> page = new PageInfo<>(cs);
m.addAttribute("page", page);
return "listProduct";
}
}
5. listCategory.jsp修改为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>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</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>
<button type="submit">提交</button>
</form>
</div>
6.editCategory.jsp改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" value="${c.name}"> <br>
<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
</form>
</div>
7.测试
运行Application.java后,访问网址, 效果如下:
增加:
在name栏输入one piece:
效果, 添加成功:
删除:
删除掉新增加进来的one piece 和 znorz:
修改:
编辑product f为 f
查询
表product已成功查询出来。翻页效果:
对users
由于有好多和对product_表的增删改查重复的步骤,所以此处只写users不同的地方,相同的步骤省去。
一. 创建Users实体类
实体类一般放在pojo包里面。users表比较麻烦的一点是它有很多的属性,所以对应的get,set方法就要写很多行。
package com.how2java.springboot.pojo;
public class Users {
private int id;
private String username;
private String password;
private String gender;
private String email;
private String province;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
二. 创建UsersMapper
package com.how2java.springboot.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.how2java.springboot.pojo.Users;
@Mapper
public interface UsersMapper {
@Select("select * from users_ ")
List<Users> findAll();
}
三. 创建UsersController
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 com.how2java.springboot.mapper.UsersMapper;
import com.how2java.springboot.pojo.Users;
@Controller
public class UsersController {
@Autowired UsersMapper usersMapper;
@RequestMapping("/listUsers")
public String listUsers(Model m) throws Exception {
List<Users> cs= usersMapper.findAll();
m.addAttribute("cs", cs);
return "listUsers";
}
}
四. listUsers.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>username</td>
<td>password</td>
<td>gender</td>
<td>email</td>
<td>province</td>
<td>city</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.username}</td>
<td>${c.password}</td>
<td>${c.gender}</td>
<td>${c.email}</td>
<td>${c.province}</td>
<td>${c.city}</td>
</tr>
</c:forEach>
</table>
五.测试显示效果
这里我没有显示birthday的原因是bitthday在数据库中的类型是date, 我不知道在java中与之相对的数据类型是什么。
不过把显示出表格并且实现增删改查才是关键。
为了显示后续的分页效果,在数据表users中新添加两条数据
六. 修改UsersMapper
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.Users;
@Mapper
public interface UsersMapper {
@Select("select * from users")
List<Users> findAll();
@Insert("insert into users (username, password, gender, email, province, city) values (#{username}, #{password}, #{gender}, #{email}, #{province}, #{city}")
public int save(Users users);
@Delete("delete from users where id = #{id}")
public void delete(int id);
@Select("select * from users where id = #{id}")
public Users get(int id);
@Update("update users set username = #{username}, password = #{password}, gender = #{gender}, email = #{email}, province = #{province}, city = #{city} where id = #{id}")
public int update(Users users);
}
七. 修改UsersController
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.UsersMapper;
import com.how2java.springboot.pojo.Users;
@Controller
public class UsersController {
@Autowired
UsersMapper usersMapper;
@RequestMapping("/addUsers")
public String addUser(Users users) throws Exception {
usersMapper.save(users);
return "redirect:listUsers";
}
@RequestMapping("/deleteUsers")
public String deleteUser(Users users) throws Exception {
usersMapper.delete(users.getId());
return "redirect:listUsers";
}
@RequestMapping("/updateUsers")
public String updateUsers(Users users) throws Exception {
usersMapper.update(users);
return "redirect:listUsers";
}
@RequestMapping("/editUsers")
public String editUsers(int id, Model model) throws Exception {
Users users = usersMapper.get(id);
model.addAttribute("users", users);
return "editUsers";
}
@RequestMapping("/listUsers")
public String listUsers(Model model, @RequestParam(value = "start", defaultValue = "0") int start, @RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
PageHelper.startPage(start, size, "id desc");
List<Users> users = usersMapper.findAll();
PageInfo<Users> page = new PageInfo<>(users);
model.addAttribute("page", page);
return "listUser";
}
}
八. 修改listUsers.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>username</td>
<td>password</td>
<td>gender</td>
<td>email</td>
<td>province</td>
<td>city</td>
<td>编辑</td>
<td>删除</td>
</tr>
<c:forEach items="${page.list}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.username}</td>
<td>${c.password}</td>
<td>${c.gender}</td>
<td>${c.email}</td>
<td>${c.province}</td>
<td>${c.city}</td>
<td><a href="editUsers?id=${c.id}">编辑</a></td>
<td><a href="deleteUsers?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="addUsers" method="post">
name: <input name="username"> <br>
<button type="submit">提交</button>
</form>
</div>
九. 修改editUsers.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<div style="margin:0px auto; width:500px">
<form action="updateUsers" method="post">
name: <input name="username" value="${c.username}"> <br>
<input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button>
</form>
</div>
十. 效果
增加
添加了用户名为serg的用户
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dQuge3h7-1686378672375)(null)]
注意
- 这里反复调试了好久才找出原因。因为我把username统一换成了name(为了代码对应), 但是由于修改的不完全,导致反复报错。
解决方法是所有出现username的地方全部换成name。 - 还有就是这里要插入多条数据,listUser.jsp在增加时不能只写name一条。
删除
删掉id为16, 17的数据
修改
修改孙仪为王芳
查看
切换页面后的显示