首页 > 其他分享 >11.2

11.2

时间:2023-11-02 19:45:50浏览次数:30  
标签: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/zzqq1314/p/17806128.html

相关文章

  • 11.2
    11.2数据类型(内置)内置数据类型字符整型浮点型布尔类型表示真假#include<stdbool.h>_Boolflag=true;_Boolunflag=false;signedorunsignedsigned表示一个类型带有正负号unsigned表示不带正负号,能表示的位数更大void类型转换隐式类型转......
  • 11.2日记
    内聚分类   定义   记忆关键字偶然内聚   一个模块内各处理元素之间没有任何联系   无直接关系逻辑内聚   模块内执行若干个逻辑上相似的功能,通过参数确定改模块完成哪一个功能   逻辑相似,参数决定时间内聚   把需要同时执行的动作组合在一起形成模块......
  • 刘铭诚:11.2美元黄金、期货原油行情走势分析及日内交易策略布局
    昨夜黄金受美元指数上涨导致下跌,低点给到1969.7一线,刘铭诚给出的防守1973-1970区域多单目前拿到1987一线,思路策略精准无误!今日周四,白盘黄金价格还是关注1992以及2000关口阻力,另外4小时布林带中轨与MA30均线粘合承压位置在1990一线,而SAR停损转向指标向下发展至1995位置,日内这两......
  • 11.2算法
    两数相加给你两个 非空的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字0之外,这两个数都不会以0 开头。 示例1:输入:l1=[2,4,3],l2=......
  • pytorch(11.2) Transformer 代码
         ......
  • DPDK-22.11.2 [五] 多进程
    dpdk支持多进程运行,不过要指定参数打开,如果没有设定,但开启第二个dpdk程序是会报错,告诉你相关系统资源被占用。EAL:Cannotcreatelockon'/var/run/dpdk/rte/config'.Isanotherprimaryprocessrunning?EAL:FATAL:CannotinitconfigEAL:Cannotinitconfigdpdk有两......
  • Patch 12419353 - 11.2.0.2.3 GI Patch Set Update (Includes Database PSU 11.2.0.2.
    Released:July19,2011,LastUpdated:August9,2011Thisdocumentisaccurateatthetimeofrelease.ForanychangesandadditionalinformationregardingGIPSU11.2.0.2.3,seetheserelateddocumentsthatareavailableatMyOracleSupport(http://supp......
  • DPDK-22.11.2 [四] Virtio_user as Exception Path
    因为dpdk是把网卡操作全部拿到用户层,与原生系统驱动不再兼容,所以被dpdk接管的网卡从系统层面(ipa/ifconfig)无法看到,同样数据也不再经过系统内核。如果想把数据再发送到系统,就要用到virtiouser。这种把数据从dpdk再发送到内核的步骤,就叫做exceptionpath。有关virtiouser,又有一......
  • 华为云oracle11.2.4安装
    centos7.932核64g200g2Thostnamectl set-hostnameo11gecho"10.240.0.200o11g">>/etc/hostswgethttps://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.shbashLinuxVMDataDiskAutoInitialize.sh  ......
  • Springboot项目中pom.xml配置文件无法解析下载oracl数据库解决办法(Cannot resolve com
    网上说是因Oracle的版权问题,导致maven下载不下来ojdbc各个版本的jar包。就会报错Cannotresolvecom.oracle:ojdbc6:11.2.0.1.0 经过一番百度,找到了一个适用的解决方法,如下操作即可:1.在终端或客户端机器上找到oracle安装驱动目录:例如:E:\myorcl\product\11.2.0\dbhome_1\j......