二、创建数据库表实体类以及实现对数据库表的增删改查操作 分别创建三个表: 
 customs表:用于存放用户信息 
 goods表:用于存放商品信息及库存量 
 orders表:用于存放订单信息 
 设定触发器reduce:用于根据订单增减商品库存1 2 create trigger reduce after insert on orders for each row update orders t1,goods t2 set t2.num=t2.num-t1.num where t1.gid=t2.gid and id=new.id; 
 
 
创建相应的包: 
1.创建用户表的实体类CustomInfo.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 package tech.codinglink.demo.entity; /**  * 用户表实体类  */ public class CustomsInfo {     private Integer uid;     private String name;     private String phone;     public CustomsInfo(Integer uid,String name, String phone) {         this.uid=uid;         this.name=name;         this.phone=phone;     }     public CustomsInfo(){}     @Override     public String toString() {         return "CustomsInfo{" +                 "uid=" + uid +                 ", name='" + name + ''' +                 ", phone='" + phone + ''' +                 '}';     }     public Integer getUid() {         return uid;     }     public void setUid(Integer uid) {         this.uid = uid;     }     public String getName() {         return name;     }     public void setName(String name) {         this.name = name;     }     public String getPhone() {         return phone;     }     public void setPhone(String phone) {         this.phone = phone;     } } 
创建其他两张表的情况类似,源代码在文章末尾提供。
2. 实现数据库的操作表:操作CustomsMapper.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 package tech.codinglink.demo.mapper; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Component; import tech.codinglink.demo.entity.CustomsInfo; import java.util.List; /**  * Customs表操作类  */ @Component public interface CustomsMapper {     //查询所有用户     @Select("select * from customs")     List<CustomsInfo> getAllCustoms();     //通过uid查找用户     @Select("select * from customs where uid=#{uid}")     List<CustomsInfo> getCustomsByUid(@Param("uid") Integer uid);     @Select("select * from customs where name=#{name}")     List<CustomsInfo> getCustomsByName(@Param("name") String name);     @Select("select * from customs where phone=#{phone}")     List<CustomsInfo> getCustomsByPhone(@Param("phone") String phone);     //新增用户信息     @Insert("insert into customs values(#{uid},#{name},#{phone})")     int addCustom(CustomsInfo customsInfo);     //通过uid删除用户信息     @Delete("delete from customs where uid=#{uid}")     int deleteCustomByUid(Integer uid);     //修改用户信息     @Update("update customs set name=#{name} ,phone=#{phone} where uid=#{uid};")     int updateCustom(String name,String phone,Integer uid); } 
3. 实现json封装类 a. 创建基类BaseModle.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package tech.codinglink.demo.modle; /**  * json封装的基类  */ public class BaseModle {     /**      *      code 为响应码,这里 0表示成功,1表示失败      *      msg  返回信息,成功者返回成功的信息,错误者返回为什么错了      *      count 返回数据的长度      *      data  里存放我们的真实数据的集合  ,所以我们只需动态的改变他就可以了,当是文章这里填的是文章的集合,当时个人信息是则填个人信息的集合。      */     public int code;     public String msg;     public int count;     public BaseModle(){     }     public BaseModle(int code,String msg){         this.code=code;         this.msg=msg;     }     public BaseModle(int code,String msg,int count){         this.code=code;         this.msg=msg;         this.count=count;     } } 
b. 用户表json封装类——继承自BaseModle类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package tech.codinglink.demo.modle; /**  * 用户表Json封装类  */ import com.fasterxml.jackson.annotation.JsonProperty; import tech.codinglink.demo.entity.CustomsInfo; import java.util.List; public class CustomsModle extends BaseModle{     @JsonProperty("data")     List<CustomsInfo> data;     public CustomsModle(int code,String msg,List<CustomsInfo> customsInfos){         this.code=code;         this.msg=msg;         this.count=customsInfos.size();         this.data=customsInfos;     } } 
注:设定json自定义字段:@JsonProperty注解 4. 创建Service类,调用Mapper类实现操作 CustomsService.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 package tech.codinglink.demo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import tech.codinglink.demo.entity.CustomsInfo; import tech.codinglink.demo.mapper.CustomsMapper; import java.util.List; /**  * 调用CustomersMapper实现服务  */ @Service public class CustomsService {     private CustomsMapper customsMapper;     @Autowired     public CustomsService(CustomsMapper customsMapper){         this.customsMapper=customsMapper;     }     /**      * 显示所有用户的集合      */     public List<CustomsInfo> getAllCustoms(){         List<CustomsInfo> customsInfos=customsMapper.getAllCustoms();         if(customsInfos!=null){             System.out.println(customsInfos);             return customsInfos;         }         return null;     }     /**      * 查找某个用户      */     public List<CustomsInfo> getCustomsByUid(Integer uid){         List<CustomsInfo> customsInfos=customsMapper.getCustomsByUid(uid);         if(customsInfos!=null){             return customsInfos;         }         return null;     }     /**      * 增加用户信息      */     public boolean addCustom(CustomsInfo customsInfo){         if(customsMapper.addCustom(customsInfo)>0) {             return true;         }         return false;     }     /**      * 删除用户信息      */     public boolean deleteCustomByUid(Integer uid){         if(customsMapper.deleteCustomByUid(uid)>0){             return true;         }         return false;     }     /**      * 修改用户信息      */     public boolean updateCustom(String name,String phone,Integer uid){         if(customsMapper.updateCustom(name,phone,uid)>0){             return true;         }         return false;     } } 
5. 创建Controller类——调用Service类实现功能 CustomsController.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 package tech.codinglink.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import tech.codinglink.demo.entity.CustomsInfo; import tech.codinglink.demo.modle.BaseModle; import tech.codinglink.demo.modle.CustomsModle; import tech.codinglink.demo.service.CustomsService; import javax.servlet.*; import javax.servlet.http.*; import java.io.BufferedReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.Principal; import java.util.*; @CrossOrigin @Controller @RestController @RequestMapping("/Customs") public class CustomsController extends HttpServlet{     @Autowired     private CustomsService customsService;     @Autowired     HttpServletRequest request;     @Autowired     HttpSession httpSession;     public CustomsModle returnCustoms(List<CustomsInfo> customsInfos){         if(customsInfos!=null){             return new CustomsModle(0,"customs success",customsInfos);         }         return new CustomsModle(1,"customs error",null);     }     /**      * 返回所有用户的信息      */     @RequestMapping("/getAllCustoms")     @ResponseBody     public CustomsModle getAllCustoms(){         return returnCustoms(customsService.getAllCustoms());     }     /**      * 新增用户信息      */     @RequestMapping("/addCustom")     @ResponseBody     public BaseModle addCustom(){         try {             request.setCharacterEncoding("utf-8");         } catch (UnsupportedEncodingException e) {             e.printStackTrace();         }         Integer uid=Integer.valueOf(request.getParameter("uid"));         String name=request.getParameter("name");         String phone=request.getParameter("phone");         String bir=request.getParameter("bir");         if(customsService.addCustom(new CustomsInfo(uid,name,phone))){             return  new BaseModle(0,"添加用户成功",1);         }         return new BaseModle(1,"添加用户失败",0);     }     /**      * 删除用户信息      */     @RequestMapping("/deleteCustomByUid")     @ResponseBody     public BaseModle deleteCustomByUid(){         try {             request.setCharacterEncoding("utf-8");         } catch (UnsupportedEncodingException e) {             e.printStackTrace();         }         Integer uid= Integer.valueOf(request.getParameter("uid"));         if(customsService.deleteCustomByUid(uid)){             return new BaseModle(0,"删除用户成功",1);         }         return new BaseModle(1,"删除用户失败",0);     }     /**      * 修改用户信息      */     @RequestMapping("/updateCustom")     @ResponseBody     public BaseModle updateCustom(){         try {             request.setCharacterEncoding("utf-8");         } catch (UnsupportedEncodingException e) {             e.printStackTrace();         }         Integer uid= Integer.valueOf(request.getParameter("uid"));         String name=request.getParameter("name");         String phone=request.getParameter("phone");         if(customsService.updateCustom(name,phone,uid)){             return new BaseModle(0,"修改用户成功",1);         }         return new BaseModle(1,"修改用户失败",0);     } } 
测试接口: 查询所有客户 
  参考自:《一个列子让你弄懂SpringBoot实现后台框架的搭建》  GitHub : https://github.com/CodingLink/springboot-zero-foundation-to-build-simple-background-exercises