# CURD **Repository Path**: haha221/curd ## Basic Information - **Project Name**: CURD - **Description**: 本项目基于尚硅谷javaweb阶段水果管理系统,使用了SpringBoot重构整个项目 前端:Vue+axios +ElementUI 后端:SpringBoot +Mybatis +thymeleaf 未来想加入的功能:基于Spring Security的登录注册,基于MyBatis-Plus的分页,用户头像上传等等 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-04-24 - **Last Updated**: 2026-04-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 一.工程简介 本项目基于尚硅谷javaweb阶段水果管理系统,使用了SpringBoot重构整个项目 系统ER图如下: ![](images/ER.png) 目前实现的功能:增删改查分页 目前用到的技术点:SpringBoot + Mybatis + thymeleaf + vue + axios ## 二.具体的步骤 #### 1.项目的搭建 SpringBoot创建一个初始的项目 ![](images/create.png) #### 2.代码编写 项目结构如图所示 ![](images/struts.png) ##### 实体类fruit ``` java package com.itheima.springboot.pojo; public class Fruit { private Integer fid ; private String fname ; private Integer price ; private Integer fcount ; private String remark ; public Fruit(){} public Fruit(Integer fid, String fname, Integer price, Integer fcount, String remark) { this.fid = fid; this.fname = fname; this.price = price; this.fcount = fcount; this.remark = remark; } public Integer getFid() { return fid; } public void setFid(Integer fid) { this.fid = fid; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Integer getFcount() { return fcount; } public void setFcount(Integer fcount) { this.fcount = fcount; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return fid + "\t\t" + fname + "\t\t" + price +"\t\t" + fcount +"\t\t" + remark ; } } ``` ##### FruitMapper ```java package com.itheima.springboot.mapper; import com.itheima.springboot.pojo.Fruit; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import java.util.List; public interface FruitMapper { @Insert("insert into t_fruit values(null,#{fname},#{price},#{fcount},#{remark})") public int save(Fruit fruit); @Update("update t_fruit set fid=#{fid},fname=#{fname},price=#{price},fcount=#{fcount},remark=#{remark} where fid =#{fid}") public int update(Fruit fruit); @Delete("delete from t_fruit where fid =#{id}") public int delete(Integer id); @Select("select * from t_fruit where fid =#{id}") public Fruit getById(Integer id); @Select("select * from t_fruit") public List getAll(); } ``` ##### FruitService ```java package com.itheima.springboot.service; import com.itheima.springboot.pojo.Fruit; import java.util.List; public interface FruitService { /** * 保存 * @param fruit * @return */ public boolean save(Fruit fruit); /** * 修改 * @param fruit * @return */ public boolean update(Fruit fruit); /** * 按id删除 * @param id * @return */ public boolean delete(Integer id); /** * 按id查询 * @param id * @return */ public Fruit getById(Integer id); /** * 查询全部 * @return */ public List getAll(); } ``` ##### FruitServiceImpl ```java package com.itheima.springboot.service.impl; import com.itheima.springboot.mapper.FruitMapper; import com.itheima.springboot.pojo.Fruit; import com.itheima.springboot.service.FruitService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class FruitServiceImpl implements FruitService { @Autowired private FruitMapper fruitmapper; @Override public boolean save(Fruit fruit) { return fruitmapper.save(fruit) > 0; } @Override public boolean update(Fruit fruit) { return fruitmapper.update(fruit) >0; } @Override public boolean delete(Integer id) { return fruitmapper.delete(id) >0; } @Override public Fruit getById(Integer id) { return fruitmapper.getById(id); } @Override public List getAll() { return fruitmapper.getAll(); } } ``` ##### FruitController ```java package com.itheima.springboot.controller; import com.itheima.springboot.pojo.Fruit; import com.itheima.springboot.service.FruitService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/fruits") public class FruitController { @Autowired private FruitService fruitService; @GetMapping("/") public String index(){ return "index"; } @PostMapping public boolean save(@RequestBody Fruit fruit) { return fruitService.save(fruit); } @PutMapping public boolean update(@RequestBody Fruit fruit) { return fruitService.update(fruit); } @DeleteMapping("/{id}") public boolean delete(@PathVariable Integer id) { return fruitService.delete(id); } @GetMapping("/{id}") public Fruit getById(@PathVariable Integer id) { return fruitService.getById(id); } @GetMapping public List getAll() { return fruitService.getAll(); } } ``` 注意:Application入口类需要加上包扫描,要不然启动会报错 ![](images/scan.png) #### 3.测试 - 业务层接口测试(Juint)通过 ```java package com.itheima.springboot; import com.itheima.springboot.pojo.Fruit; import com.itheima.springboot.service.FruitService; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest("classes= SpringbootPro01Application.class") class SpringbootPro01ApplicationTests { @Autowired private FruitService fruitService; @Test void testGetById() { Fruit byId = fruitService.getById(10); System.out.println(byId); } @Test void testGetByAll() { List all = fruitService.getAll(); System.out.println(all); } } ``` - 表现层接口测试(APIpost)通过 ![](images/api.png) #### 4.数据层的封装 在controller 包下面创建两个类 - Code类 ```java package com.itheima.springboot.controller; //状态码 public class Code { public static final Integer SAVE_OK = 20011; public static final Integer DELETE_OK = 20021; public static final Integer UPDATE_OK = 20031; public static final Integer GET_OK = 20041; public static final Integer SAVE_ERR = 20010; public static final Integer DELETE_ERR = 20020; public static final Integer UPDATE_ERR = 20030; public static final Integer GET_ERR = 20040; } ``` - result类 ```java package com.itheima.springboot.controller; public class Result { private Object data; private Integer code; private String msg; public Result() { } public Result(Object data, Integer code) { this.data = data; this.code = code; } public Result(Object data, Integer code, String msg) { this.data = data; this.code = code; this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } ``` - Controller的代码要发生改变,如下: ```java package com.itheima.springboot.controller; import com.itheima.springboot.pojo.Fruit; import com.itheima.springboot.service.FruitService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/fruits") public class FruitController { @Autowired private FruitService fruitService; @GetMapping("/") public String index(){ return "index"; } @PostMapping public Result save(@RequestBody Fruit fruit) { boolean flag = fruitService.save(fruit); return new Result(flag,flag ? Code.SAVE_OK : Code.SAVE_ERR ); } @PutMapping public Result update(@RequestBody Fruit fruit) { boolean flag = fruitService.update(fruit); return new Result(flag,flag? Code.UPDATE_OK :Code.UPDATE_ERR); } @DeleteMapping("/{id}") public Result delete(@PathVariable Integer id) { boolean flag = fruitService.delete(id); return new Result(flag ,flag ? Code.DELETE_OK : Code.DELETE_ERR); } @GetMapping("/{id}") public Result getById(@PathVariable Integer id) { Fruit fruit = fruitService.getById(id); Integer code= fruit !=null ? Code.GET_OK :Code.GET_ERR; String msg=fruit != null ? "" : " 数据查询失败,请重试 " ; return new Result(fruit,code,msg); } @GetMapping public Result getAll() { List fruitList = fruitService.getAll(); Integer code= fruitList !=null ? Code.GET_OK :Code.GET_ERR; String msg=fruitList != null ? "" : "数据查询失败,请重试 "; return new Result(fruitList,code,msg); } } ``` #### 5.加入异常处理器 #### 6.前端页面 引入文件 ![](images/index.png) ***注意:一定要开启SpingBoot对静态资源的拦截放行,要不然样式无法生效,引入的样式也一定要动态引入*** ```xml #开启对静态资源的访问 spring.mvc.static-path-pattern=/** spring.web.resources.static-locations=classpath:/static,classpath:/resources ``` #### 7.使用Mybatis的分页插件 ## 三.延伸阅读 #### 整合Mybatis逆向工程 pom.xml文件中添加插件 ```xml org.mybatis.generator mybatis-generator-core org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true src/main/resources/generator/generatorConfig.xml mysql mysql-connector-java 8.0.15 ``` 在项目resource目录下创建generator文件,里面创建generatorConfig.xml ```xml ``` 启动generator插件 ![](images/generator.png)