springBoot+mybatisplus+redis实现增删改查
效果图
项目结构
yml
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/spring?useSSL=false&autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
#模板引擎
thymeleaf:
#禁用缓存 默认false
cache: false
mode: HTML5
prefix: classpath:/templates/
suffix: .html
#指定编码
encoding: utf-8
redis:
database: 0
host: 127.0.0.1
port: 6379
lettuce:
pool:
max-wait: 1000ms
max-active: 16
max-idle: 32
min-idle: 8
devtools:
restart:
enabled: true
mvc:
pathmatch:
matching-strategy: ant_path_matcher
main:
allow-bean-definition-overriding: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wang</groupId>
<artifactId>springBoot-plus-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot-plus-redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Velocity(默认-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
<scope>compile</scope>
</dependency>
<!-- 模板引擎 代码生成-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<!-- 添加jedis依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.9.0</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
config
package com.wang.config;
public interface JedisDao {
//查
public String getValue(String key);
//删
public Long delValue(String key);
//增
public String setValue(String key,String value);
//设置时间
public Long expire(String key,int seconds);
}
package com.wang.config.Impl;
import com.wang.config.JedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
public class JedisDaoImpl implements JedisDao {
@Autowired
private JedisPool jedisPool;
@Override
public String getValue(String key) {
//连接redis
Jedis jedis = jedisPool.getResource();
//操作redis
String s = jedis.get(key);
jedis.close();
//返回数据
return s;
}
@Override
public Long delValue(String key) {
//连接redis
Jedis jedis = jedisPool.getResource();
//操作redis
Long s = jedis.del(key);
jedis.close();
//返回数据
return s;
}
@Override
public String setValue(String key, String value) {
//连接redis
Jedis jedis = jedisPool.getResource();
//操作redis
String s = jedis.set(key,value);
jedis.close();
//返回数据
return s;
}
@Override
public Long expire(String key, int seconds) {
//连接redis
Jedis jedis = jedisPool.getResource();
//操作redis
Long s = jedis.expire(key, seconds);
jedis.close();
//返回数据
return s;
}
}
工具包
package com.wang.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
public class JedisClientSingle {
@Autowired
private JedisPool jedisPool;
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
}
package com.wang.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
@Configuration
public class RedisConfig {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public JedisPool jedisPool(){
return new JedisPool();
}
@Bean
@Primary
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(new ObjectMapper());
RedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(stringSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
}
package com.wang.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param class 对象中的object类型
* @return
*/
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
实体类
可以用插件跟你生成对应的包和内容(mybatisplus插件)
这里因为手写的两表联查报错,我用单表单查做的,type进行了each判断
package com.wang.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author wjc
* @since 2022-11-28
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Double price;
private Integer count;
private Integer type;
// private Type types;
}
package com.wang.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* <p>
*
* </p>
*
* @author wjc
* @since 2022-11-28
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class Type implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String typename;
}
mapper接口注入直接调用就行了
service层
package com.wang.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.pojo.Goods;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author wjc
* @since 2022-11-28
*/
public interface GoodsService extends IService<Goods> {
// List<Goods> findAll(Page<Goods> page, Goods goods);
List<Goods> findAll();
}
package com.wang.service;
import com.wang.pojo.Type;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author wjc
* @since 2022-11-28
*/
public interface TypeService extends IService<Type> {
List<Type> allList();
}
实现类Impl
package com.wang.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.mapper.TypeMapper;
import com.wang.pojo.Goods;
import com.wang.mapper.GoodsMapper;
import com.wang.pojo.Type;
import com.wang.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author wjc
* @since 2022-11-28
*/
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Autowired
private RedisTemplate redistemplate;
@Override
public List<Goods> findAll() {
String key="user153";
ValueOperations<String,List<Goods>> operations=redistemplate.opsForValue();
List<Goods> goodsList=null;
boolean hasKey=redistemplate.hasKey(key);
if (hasKey){
goodsList= operations.get(key);
return goodsList;
}else{
goodsList = goodsMapper.selectList(null);
operations.set(key,goodsList);
return goodsList;
}
}
// @Override
// public List<Goods> findAll(Page<Goods> page, Goods goods) {
// return null;
// }
}
package com.wang.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wang.pojo.Goods;
import com.wang.pojo.Type;
import com.wang.mapper.TypeMapper;
import com.wang.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* <p>
* 服务实现类
* </p>
*
* @author wjc
* @since 2022-11-28
*/
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {
@Autowired
private RedisTemplate redistemplate;
@Autowired
private TypeMapper typeMapper;
@Override
public List<Type> allList() {
String key="user351";
ValueOperations<String,List<Type>> operations=redistemplate.opsForValue();
List<Type> typeList=null;
boolean hasKey=redistemplate.hasKey(key);
if (hasKey){
typeList= operations.get(key);
return typeList;
}else{
typeList = typeMapper.selectList(null);
operations.set(key,typeList);
return typeList;
}
}
}
controller层
package com.wang.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.config.Impl.JedisDaoImpl;
import com.wang.pojo.Goods;
import com.wang.pojo.Type;
import com.wang.service.impl.GoodsServiceImpl;
import com.wang.service.impl.TypeServiceImpl;
import com.wang.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author wjc
* @since 2022-11-28
*/
@Controller
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsServiceImpl goodsService;
@Autowired
private TypeServiceImpl typeService;
@Autowired
private RedisTemplate redistemplate;
@Autowired
private JedisDaoImpl jedisDao;
@RequestMapping("/list")
public String findALl(Model model,Goods goods,@RequestParam(value = "pageSize", required = false, defaultValue = "1") Integer pageSize,@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum){
List<Goods> page = goodsService.findAll();
// Page<Goods> pages = new Page<>(pageNum,pageSize);
// List<Goods> page = goodsService.findAll(pages, goods);
// long total = page.size();
// long countpage=total%pageSize==0?total/pageSize:total/pageSize+1;
List<Type> list = typeService.allList();
// String name = goods.getName();
// if (pageNum<0){
// pageNum=1;
// }
// if (pageNum>countpage){
// pageNum= Math.toIntExact(countpage);
// }
// model.addAttribute("pageNum",pageNum);
model.addAttribute("lists",list);
// model.addAttribute("name",name);
// model.addAttribute("countpage",countpage);
model.addAttribute("page",page);
return "allList";
}
@RequestMapping("/add")
public String add(Model model){
List<Type> list = typeService.allList();
model.addAttribute("type",list);
return "add";
}
@RequestMapping("/addG")
public String addG(Goods goods){
for (int i = 0; i < 3; i++) {
goodsService.save(goods);
}
redistemplate.delete("user153");
redistemplate.delete("user351");
return "redirect:/goods/list";
}
@RequestMapping("/upd")
public String upd(Integer id,Model model){
Goods byId = goodsService.getById(id);
List<Type> list = typeService.allList();
model.addAttribute("type",list);
model.addAttribute("all",byId);
return "upd";
}
@RequestMapping("/upda")
public String upda(Goods goods){
goodsService.saveOrUpdate(goods);
redistemplate.delete("user153");
redistemplate.delete("user351");
return "redirect:/goods/list";
}
@GetMapping("/delete")
public String del(Integer id,Integer type){
// List<Type> list = typeService.list();
// for (Type t:list) {
// if (t.getId().equals(type)){
// return "redirect:/goods/list";//相等返回查询页面
// }else{
// goodsService.removeById(id);//不相等删除type表id对应数据
// }
// }
goodsService.removeById(id);
List<Goods> delGood=null;
String del = jedisDao.getValue("delGoods");
if(del==null||del.equals("")){
delGood=new ArrayList<>();
}else {
delGood= JsonUtils.jsonToList(del,Goods.class);
}
List<Goods> goods = JsonUtils.jsonToList(jedisDao.getValue("user153"),Goods.class);
for (int i = 0; i < goods.size(); i++) {
if(goods.get(i).getId().equals(id)){
delGood.add(goods.get(i));
break;
}
}
jedisDao.setValue("delGoods",JsonUtils.objectToJson(delGood));
jedisDao.delValue("user153");
jedisDao.delValue("user351");
return "redirect:/goods/list";
}
@GetMapping("/del")
public String del(Model model){
if (jedisDao.getValue("delGoods")!=null){
List<Goods> gList = JsonUtils.jsonToList(jedisDao.getValue("delGoods"), Goods.class);
model.addAttribute("glist",gList);
return "delGoods";
}else {
return "redirect:/goods/list";
}
}
@GetMapping("/huifu")
public String huifu(Integer id){
//1.拿到所有数据
String delGoods = jedisDao.getValue("delGoods");
List<Goods> goods = JsonUtils.jsonToList(delGoods, Goods.class);
//2.通过id查询出来要恢复的商品
Goods goods1=null;
for (int i = 0; i < goods.size(); i++) {
if(goods.get(i).getId().equals(id)){
goods1=goods.get(i);
goods.remove(i);
break;
}
}
goodsService.save(goods1);
if(goods.isEmpty()){
jedisDao.delValue("delGoods");
}else {
jedisDao.setValue("delGoods", JsonUtils.objectToJson(goods));
}
jedisDao.delValue("user153");
jedisDao.delValue("user351");
return "redirect:/goods/list";
}
}
前端页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<body>
<div style="width: 680px;height:100%; margin:0px auto">
<!-- <p>-->
<!-- <form th:action="@{/goods/list}" method="post">-->
<!-- 食品名称:<input name="name" type="text" th:value="${name}"/>-->
<!-- <input type="submit" value="查询">-->
<!-- </form>-->
<!-- </p>-->
<a type="button" th:href="@{/goods/add}" style="text-decoration: none">添加</a>
<a th:href="@{/goods/del}">恢复</a>
<!-- <span th:text="${session.msg}" style="color: aqua" id="mu"></span>-->
<table border="2px">
<thead>
<tr>
<th>食品名称</th>
<th>价格</th>
<th>库存</th>
<th>食品类型</th>
<th>相关操作</th>
</tr>
</thead>
<tbody>
<tr th:each="list:${page}">
<td th:text="${list.name}"></td>
<td th:text="${list.price}"></td>
<td th:text="${list.count}"></td>
</td><td th:if="${t.id==list.type}" th:each="t:${lists}" th:text="${t.typename}" th:value="${t.id}"></td>
<td>
<a th:href="@{/goods/delete/(id=${list.id})}">删除</a>
<a th:href="@{/goods/upd/(id=${list.id})}">编辑</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:method="post" th:action="@{/goods/addG}">
<div style="width: 450px;height:100%; margin:0px auto">
<h1 style="color: aquamarine">新增</h1>
<table border="2px">
<tr>
<td>零食名称</td>
<td><input type="text" th:name="name" required></td>
</tr>
<tr>
<td>价格</td>
<td><input type="number" th:name="price" required></td>
</tr>
<tr>
<td>库存</td>
<td><input type="text" th:name="count" required></td>
</tr>
<tr>
<td>零食类型</td>
<td>
<select th:name="type">
<option th:value="${t.id}" th:each="t:${type}" th:text="${t.typename}"></option>
</select>
</td>
</tr>
<tr>
<td>相关操作</td>
<td><input th:type="submit" th:value="新增"/><a th:href="@{/goods/list}" style="text-decoration: none"> 返回</a></td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div >
<h2>删除列表</h2>
<table class="table table-striped table-bordered table-hover text-center">
<thead>
<tr style="text-align:center">
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品库存</th>
<th>商品类型</th>
<th>操作</th>
</tr>
</thead>
<tr th:each="glist:${glist}">
<td th:text="${glist.id}"></td>
<td th:text="${glist.name}"></td>
<td th:text="${glist.price}"></td>
<td th:text="${glist.count}"></td>
<td th:text="${glist.type}"></td>
<td><a th:href="@{/goods/huifu/(id=${glist.id})}">恢复</a></td>
</tr>
</table>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/goods/upda}" th:method="post">
<div style="width: 450px;height:100%; margin:0px auto">
<h1 style="color:red">修改页面</h1>
<table border="2px">
<input type="hidden" th:name="id" th:value="${all.id}">
<tr>
<td>零食名称</td>
<td><input type="text" th:name="name" th:value="${all.name}" required ></td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" th:name="price" th:value="${all.price}" required ></td>
</tr>
<tr>
<td>库存</td>
<td><input type="text" th:name="count" th:value="${all.count}" required ></td>
</tr>
<tr>
<td>食品类型</td>
<td>
<select th:name="type">
<option th:value="${t.id}" th:each="t:${type}" th:text="${t.typename}"></option>
</select>
</td>
</tr>
<td>操作</td>
<td><input th:type="submit" th:value="修改"><a th:href="@{/goods/list}" style="text-decoration: none"> 返回</a></td>
</tr>
</table>
</div>
</form>
</body>
</html>
小帆也能远航