今天实现了对于学生个人信息添加的基本功能,我使用的是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>-->
<!--<!– <if test="hobby != null and hobby.size() > 0">–>-->
<!--<!– hobby =–>-->
<!--<!– <foreach collection="hobby" item="h" separator=",">–>-->
<!--<!– #{h}–>-->
<!--<!– </foreach>,–>-->
<!--<!– </if>–>-->
<!-- <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实体类的命名不同问题,当然需要我们的实体类的命名方式严格遵循驼峰命名法。

浙公网安备 33010602011771号