今天我们来实现上次期中考试的代码,本次实现的是后端
Pojo类
1、Plan.java类
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor public class Plan { private String id; private String name; private String overview; private String productway; private LocalDateTime begin; private LocalDateTime end; private List<String> technology; }
2、Result.java
package com.example.pojo; /** * 统一响应结果封装类 */ public class Result { private Integer code;//1 成功 , 0 失败 private String msg; //提示信息 private Object data; //数据 data public static Result success(Object data) { return new Result(1, "success", data); } public static Result success() { return new Result(1, "success", null); } public static Result error(String msg) { return new Result(0, msg, null); } public Result() { } public Result(Integer code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } @Override public String toString() { return "Result{" + "code=" + code + ", msg='" + msg + '\'' + ", 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; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
3、Select.java
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.List; @Data @NoArgsConstructor @AllArgsConstructor public class Select { private String selectId; private String selectName; private String selectWay; private List<String> selectTechnology; }
Mapper类
PlanMapper.interface
package com.example.mapper; import com.example.pojo.Plan; import org.apache.ibatis.annotations.*; import java.time.LocalDateTime; import java.util.List; @Mapper public interface PlanMapper { @Insert("INSERT INTO product_plan(id, name, overview, productway, begin, end, technology) VALUES(#{id},#{name},#{overview},#{productway},#{begin},#{end},#{technology})") void insert(String id, String name, String overview, String productway, LocalDateTime begin, LocalDateTime end, String technology); @Select("delete from product_plan where id=#{id}") void deleteByID(String id); List<Plan> selectByCondition(String id, String name, String productway, String technology); @Select("select * from product_plan") List<Plan> selectAll(); @Select("select * from product_plan where id=#{id}") Plan selectById(String id); @Update("update product_plan set id=#{id},name=#{name},overview=#{overview},productway=#{productway},begin=#{begin},end=#{end},technology=#{technology} ") void updateById(String id, String name, String overview, String productway, LocalDateTime begin, LocalDateTime end, String technology); }
Service
PlanService.java
package com.example.service; import com.example.mapper.PlanMapper; import com.example.pojo.Select; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.pojo.Plan; import java.time.LocalDateTime; import java.util.List; @Service public class PlanService { @Autowired private PlanMapper planMapper; //增加 public void addPlan(Plan plan) { String id = plan.getId(); String name = plan.getName(); String overview = plan.getOverview(); String productway = plan.getProductway(); LocalDateTime begin = plan.getBegin(); LocalDateTime end = plan.getEnd(); String technology = String.join(",", plan.getTechnology()); planMapper.insert(id, name, overview, productway, begin, end, technology); } //删除 public void deletePlan(String id) { planMapper.deleteByID(id); } //查找 public Plan selectPlan(String id) { return planMapper.selectById(id); } public List<Plan> getAll() { return planMapper.selectAll(); } public List<Plan> selectByCondition(Select select) { String selectId = select.getSelectId(); String selectName = select.getSelectName(); String selectWay = select.getSelectWay(); String selectTechnology = String.join(",", select.getSelectTechnology()); return planMapper.selectByCondition(selectId, selectName, selectWay, selectTechnology); } //更改 public void updatePlan(Plan plan) { String id = plan.getId(); String name = plan.getName(); String overview = plan.getOverview(); String productway = plan.getProductway(); LocalDateTime begin = plan.getBegin(); LocalDateTime end = plan.getEnd(); String technology = String.join(",", plan.getTechnology()); if (plan.getId() != null) { planMapper.updateById(id, name, overview, productway, begin, end, technology); } else { System.out.println("id为空,无法修改"); } } }
Controller类
PlanController.java
package com.example.controller; import com.example.pojo.Plan; import com.example.pojo.Result; import com.example.pojo.Select; import com.example.service.PlanService; import org.apache.ibatis.annotations.Insert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/plan") public class PlanController { @Autowired private PlanService planService; //增加 @PostMapping("/add") public Result addPlan(@RequestBody Plan plan) { try { System.out.println(plan); planService.addPlan(plan); return Result.success(plan); } catch (Exception e) { e.printStackTrace(); // 输出异常信息到控制台 throw e; // 抛出异常以查看更多详细信息 } } //删除 @DeleteMapping("/delete/{id}") public Result deletePlan(@PathVariable String id) { Plan plan = planService.selectPlan(id); if (plan != null) { planService.deletePlan(id); return Result.success(plan); } else { return Result.error("并未查询到信息"); } } //修改 @PutMapping("/update") public Result updatePlan(@RequestBody Plan plan) { Plan plan1 = planService.selectPlan(plan.getId()); if (plan1 != null) { planService.updatePlan(plan); return Result.success(plan1); } else { return Result.error("无法查询到信息,无法修改"); } } //查看 @GetMapping("/getAll") public Result getAll() { List<Plan> plans = planService.getAll(); return Result.success(plans); } @GetMapping("/getById/{id}") public Result getById(@PathVariable String id) { Plan plan = planService.selectPlan(id); if (plan != null) { return Result.success(plan); } else { return Result.error("未查询到相关信息"); } } @GetMapping("/getByCondition") public Result getByCondition(@RequestBody Select select) { List<Plan> plan = planService.selectByCondition(select); if (plan != null) { return Result.success(plan); } else { return Result.error("未查询到相关信息"); } } }
我们在资源包里建立mapper类的映射xml类
resource.com.example.mapper
PlanMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.PlanMapper"> <!-- 定义一个模糊查询的SQL语句 --> <select id="selectByCondition" parameterType="map" resultType="com.example.pojo.Plan"> SELECT id, name, overview, productway, begin, end, technology FROM plan.product_plan WHERE 1 = 1 <if test="id != null and id != ''"> AND id LIKE CONCAT('%', #{id}, '%') </if> <if test="name != null and name != ''"> AND name LIKE CONCAT('%', #{name}, '%') </if> <if test="productway != null and productway != ''"> AND productway LIKE CONCAT('%', #{productway}, '%') </if> <if test="technology != null and technology != ''"> AND technology LIKE CONCAT('%', #{technology}, '%') </if> </select> </mapper>
连接数据库
#????? spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #???????url spring.datasource.url=jdbc:mysql://localhost:3306/plan #????????? spring.datasource.username=root #?? spring.datasource.password=123456789 #??mybatis????????????? mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #kaiqi tuofengmingming zidongyingshe kaiguan mybatis.configuration.map-underscore-to-camel-case=true
标签:String,11.2,public,plan,import,id,Result From: https://www.cnblogs.com/zzqq1314/p/17806128.html