首页 > 其他分享 >10.30

10.30

时间:2023-12-18 20:58:30浏览次数:25  
标签:studentid String 10.30 Result personInformation public name

今天实现了对于学生个人信息添加的基本功能,我使用的是springboot实现后端的代码,通过springboot加mybatis实现接口类的实现。

POJO包定义类变量以及返回值变量

1、PersonInformation.java

复制代码
package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

//@Data
//@NoArgsConstructor
//@AllArgsConstructor
public class PersonInformation {
    private String studentid;
    private String name;
    private Integer age;
    private String gender;
    private List<String> hobby;
    private String major;

    public PersonInformation() {
    }

    public PersonInformation(String studentid, String name, Integer age, String gender, List<String> hobby, String major) {
        this.studentid = studentid;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.hobby = hobby;
        this.major = major;
    }

    @Override
    public String toString() {
        return "PersonInformation{" +
                "studentid='" + studentid + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", hobby=" + hobby +
                ", major='" + major + '\'' +
                '}';
    }

    public String getStudentid() {
        return studentid;
    }

    public void setStudentid(String studentid) {
        this.studentid = studentid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }
}
复制代码

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;
    }
}
复制代码

Mapper类

PersonMapper.interface

复制代码
package com.example.mapper;

import com.example.pojo.PersonInformation;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface PersonMapper {

     PersonInformation getById(String studentid);

     PersonInformation getByName(String name);
@Update("UPDATE personinformation.person set name=#{name},age=#{age},gender=#{gender},major=#{major},hobby=#{hobby} where studentid=#{studentid}")
     void updatePerson(String studentid,  String name, Integer age, String gender,  String major,  String hobby);

     void deletePersonById(String studentid);

     List<PersonInformation> getAllPerson();

//    void insertPerson(@Param("studentid") String sutdentid, @Param("name") String name, @Param("age") Integer age, @Param("gender") String gender, @Param("major") String major, @Param("hobby") String hobby);
     @Insert("INSERT INTO personinformation.person (studentid,name, age, gender, major,hobby) VALUES (#{studentid},#{name}, #{age}, #{gender},#{major},#{hobby})")
void insertPerson( String studentid,  String name, Integer age, String gender,  String major,  String hobby);
}
复制代码

Service类

复制代码


package com.example.service;

import com.example.mapper.PersonMapper;
import com.example.pojo.PersonInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonService {
@Autowired
private PersonMapper personMapper;

public void addPerson(PersonInformation personInformation) {
String studentid = personInformation.getStudentid();
String name = personInformation.getName();
Integer age = personInformation.getAge();
String gender = personInformation.getGender();
String major = personInformation.getMajor();

}

public PersonInformation getPersonById(String studentid) {
return personMapper.getById(studentid);
}

public PersonInformation getPersonByName(String name) {
return personMapper.getByName(name);
}

public void deletePersonById(String studentid) {
personMapper.deletePersonById(studentid);
}

public void updatePerson(PersonInformation personInformation) {
String studentid = personInformation.getStudentid();
String name = personInformation.getName();
Integer age = personInformation.getAge();
String gender = personInformation.getGender();
String major = personInformation.getMajor();
String hobby = String.join(",", personInformation.getHobby());
if (personInformation.getStudentid() != null) {
personMapper.updatePerson(studentid, name, age, gender, major, hobby);
} else {
System.out.println("studentid不能为空");
}
}

public List<PersonInformation> getAllPerson() {
return personMapper.getAllPerson();
}
}
 
复制代码

Contorller类

PersonController.java

复制代码
package com.example.controller;

import com.example.pojo.PersonInformation;
import com.example.pojo.Result;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService personService;

    @GetMapping("/getAll")
    public Result getAllPersons() {

        List<PersonInformation> persons = personService.getAllPerson();
        return Result.success(persons);
    }

    @PostMapping("/add")
    public Result addPerson(@RequestBody PersonInformation personInformation) {
        try {
            System.out.println(personInformation);
            personService.addPerson(personInformation);
            return Result.success(personInformation);
        } catch (Exception e) {
            e.printStackTrace(); // 输出异常信息到控制台
            throw e; // 抛出异常以查看更多详细信息
        }
    }

    @GetMapping("/getById/{studentid}")
    public Result getById(@PathVariable String studentid) {
        PersonInformation personInformation = personService.getPersonById(studentid);
        if (personInformation != null) {
            return Result.success(personInformation);
        } else {
            return Result.error("未查询到相关信息");
        }
    }

    @GetMapping("/getByName/{name}")
    public Result getByName(@PathVariable String name) {
        PersonInformation personInformation = personService.getPersonByName(name);
        System.out.println(personInformation);
        return Result.success(personInformation);

    }

    @PutMapping("/update")
    public Result updatePerson(@RequestBody PersonInformation personInformation) {
        personService.updatePerson(personInformation);
        PersonInformation personInformation1 = personService.getPersonById(personInformation.getStudentid());
        if (personInformation1 != null) {
            return Result.success(personInformation1);
        } else {
            return Result.error("无法查询到信息,无法修改");
        }
    }

    @DeleteMapping("/delete/{studentid}")
    public Result deletePerson(@PathVariable String studentid) {
        personService.deletePersonById(studentid);
        PersonInformation personInformation = personService.getPersonById(studentid);
        if (personInformation != null) {
            return Result.success(personInformation);
        } else {
            return Result.error("并未查询到信息");
        }
    }
}
复制代码

同时在资源包resources里创建一个和Mapper类相同的包位置以及名字

创建一个PersonMapper的映射类xml。

PersonMapper.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.PersonMapper">
    <!--    <insert id="insertPerson" parameterType="com.example.pojo.PersonInformation">-->
    <!--        INSERT INTO personinformation.person (studentid,name, age, gender, major,hobby)-->
    <!--        VALUES (#{studentid},#{name}, #{age}, #{gender},#{major},#{hobby})-->
    <!--    </insert>-->
    <!--        <foreach collection="hobby" item="h" open="(" separator="," close=")">-->
    <!--            #{h}-->
    <!--        </foreach>)-->
    <!--    </insert>-->
    <resultMap id="PersonInformationResultMap" type="com.example.pojo.PersonInformation">
        <id property="studentid" column="studentid"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="gender" column="gender"/>
        <result property="major" column="major"/>
        <collection property="hobby" ofType="java.lang.String">
            <result column="hobby"/>
        </collection>
    </resultMap>


    <select id="getById" parameterType="java.lang.String" resultMap="PersonInformationResultMap">
        SELECT *
        FROM personinformation.person
        WHERE studentid = #{studentid}
    </select>
    <select id="getByName" parameterType="java.lang.String" resultMap="PersonInformationResultMap">
        SELECT *
        FROM personinformation.person
        WHERE name = #{name}
    </select>
    <!--    <update id="updatePerson" parameterType="com.example.pojo.PersonInformation">-->
    <!--        UPDATE personinformation.person-->
    <!--        <set>-->
    <!--            <if test="name != null">name = #{name},</if>-->
    <!--            <if test="age != null">age = #{age},</if>-->
    <!--            <if test="gender != null">gender = #{gender},</if>-->
    <!--&lt;!&ndash;            <if test="hobby != null and hobby.size() > 0">&ndash;&gt;-->
    <!--&lt;!&ndash;                hobby =&ndash;&gt;-->
    <!--&lt;!&ndash;                <foreach collection="hobby" item="h" separator=",">&ndash;&gt;-->
    <!--&lt;!&ndash;                    #{h}&ndash;&gt;-->
    <!--&lt;!&ndash;                </foreach>,&ndash;&gt;-->
    <!--&lt;!&ndash;            </if>&ndash;&gt;-->
    <!--            <if test="hobby != null">hobby = #{hobby}</if>-->
    <!--            <if test="major != null">major = #{major},</if>-->
    <!--        </set>-->
    <!--        <where>-->
    <!--            <if test="studentid != null">studentid = #{studentid}</if>-->
    <!--        </where>-->
    <!--    </update>-->
    <delete id="deletePersonById" parameterType="java.lang.String">
        DELETE
        FROM personinformation.person
        WHERE studentid = #{studentid}
    </delete>
    <select id="getAllPerson" resultMap="PersonInformationResultMap">
        SELECT *
        FROM personinformation.person
    </select>
</mapper>
复制代码

当然也不要忘了配置数据库的链接

application.properties

复制代码
#?????
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#???????url
spring.datasource.url=jdbc:mysql://localhost:3306/personinformation
#?????????
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
复制代码

第一行的代码意思为确定我这个项目工程所需要使用的数据库驱动mysql,第二行为链接数据库的url,第三行为连接数据库的用户名,第四行为连接数据库的密码,第五行为配置mybatis框架,使得我的运行日志可以在控制台上输出出来。最后一行是自动匹配数据库表中变量与java实体类的命名不同问题,当然需要我们的实体类的命名方式严格遵循驼峰命名法。

标签:studentid,String,10.30,Result,personInformation,public,name
From: https://www.cnblogs.com/gjsgjs/p/17912210.html

相关文章

  • 10.30
    今日学习内容<%--CreatedbyIntelliJIDEA.TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</ti......
  • 10.30
    MySQL的数据类型有大概可以分为5种,分别是整数类型、浮点数类型和定点数类型、日期和时间类型、字符串类型、二进制类型等。注意:整数类型和浮点数类型可以统称为数值数据类型。1)数值类型整数类型包括TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT,浮点数类型包括FLOAT和DOUB......
  • 10.30-11.5总结
    packagesimplePhone;publicabstractclassPhone{publicabstractvoidvoice();}packagesimplePhone;publicclassSimplePhoneextendsPhone{publicSimplePhone(){System.out.println("这是一部简单手机");}@Overridepublicvoidvoice(){Sys......
  • 上周热点回顾(10.30-11.5)
    热点随笔:· 张良计诉园子侵权案一审结束:需7天内证明转载博文是用户发布 (博客园团队)· 开源2年、打磨13年、300万行代码的开源项目 (削微寒)· 用1100天做一款通用的管理后台框架 (胡尐睿丶)· C#/.NET/.NETCore优秀项目和框架2023年10月简报 (追逐时光者)· 记......
  • 大二快乐日记10.30
    MySQL的数据类型有大概可以分为5种,分别是整数类型、浮点数类型和定点数类型、日期和时间类型、字符串类型、二进制类型等。注意:整数类型和浮点数类型可以统称为数值数据类型。1)数值类型整数类型包括TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT,浮点数类型包括FLOAT和DOUB......
  • 10.30
    今日时长:5h今日代码:200h学习内容:解决python问题   异常处理当发生错误(或我们称之为异常)时,Python通常会停止执行并生成错误消息。try 块用于测试一段代码是否存在错误。except 块用于处理错误。else 块用于在没有错误时执行代码。finally 块用于无论 try 和 ......
  • 10.30
    今天实现了对于学生个人信息添加的基本功能,我使用的是springboot实现后端的代码,通过springboot加mybatis实现接口类的实现。POJO包定义类变量以及返回值变量1、PersonInformation.javapackagecom.example.pojo;importlombok.AllArgsConstructor;importlombok.Data;imp......
  • 10.30 CF1685 题解
    10.30CF1685A.CircularLocalMiniMax题意给你\(n\)个整数$a_1,a_2,\ldots,a_n$。问有没有可能将它们排列在一个圆上,使每个数字严格地大于其相邻的两个数字或严格地小于其相邻的两个数字?题解直接排序然后按照\(1,4,2,5,3,6\)的规律放,check一下合不合法就行了。......
  • 「Log」做题记录 2023.10.30-
    \(2023.10.30-2023.11.1\)\(\color{blueviolet}{AT\_abc285\_g}\)神秘题。网络流是显著的,建边方式如下:所有边容量都为\(1\)。每个点拆为入点和出点,\(S\)向入点连边,出点向\(T\)连边。1的入点向出点连边。2的出点向四联通的2或?的入点连边?当做上两个处理。考......
  • 每日总结10.30
    今天是充实而多样化的一天,上午的工程实训让我体验到了一项实际的工作任务,但也让我感到相当劳累。锯铁片花费了我整整两个小时,这不仅锻炼了我的体力,也培养了耐心和解决问题的能力。这个经历教会了我不仅要有技术,还要有毅力和决心来应对挑战。下午的Java考试揭示了我在这门课程中的......