首页 > 其他分享 >11.2

11.2

时间:2023-12-18 21:03:19浏览次数:25  
标签:String 11.2 public plan import id Result

今天我们来实现上次期中考试的代码,本次实现的是后端

 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/gjsgjs/p/17912233.html

相关文章

  • 11.2随笔
    SQL INSERTINTO 语句INSERTINTO语句用于向表中插入新记录。SQLINSERTINTO语句INSERTINTO语句用于向表中插入新记录。SQLINSERTINTO语法INSERTINTO语句可以有两种编写形式。第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:INSERTINTOtable_na......
  • 11.29
    SELECT*fromAWHEREidin(SELECTidfromB)SELECT*fromAWHEREidEXISTS(SELECT1fromA.id=B.id)in是在内存中遍历比较exist需要查询数据库,所以当B的数据量比较大时,exists效率优于in.in()只执行一次,把B表中的所有id字段缓存起来,之后检查A表的id是否与......
  • 11.22判断是否存在
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@pageimport="java.sql.*"%><%@pageimport="javax.naming.*"%><%@pageimport="javax.*"%><html><body&g......
  • 11.21浏览一行信息
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@pageimport="java.sql.*"%><%@pageimport="javax.naming.*"%><%@pageimport="javax.*"%><html><body&g......
  • 11.20
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@pageimport="java.sql.*"%><%@pageimport="javax.naming.*"%><%@pageimport="javax.*"%><html><body&g......
  • 11.27
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>修改用户密码</title><s......
  • 11.24
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@pageimport="java.sql.*"%><%@pageimport="javax.naming.*"%><%@pageimport="javax.*"%><html><body&g......
  • 11.23
    <%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@pageimport="java.sql.*"%><%@pageimport="javax.naming.*"%><%@pageimport="javax.*"%><html><body&g......
  • 11.29
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>添加课程</title></head&g......
  • 11.28
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>教师功能页面</title></head>......