一、案例需求
基于Spring Boot+MyBatis实现学生信息的新增、修改、删除、查询功能,并实现MySQL数据库的操作。
MySQL数据库创建学生表(t_student),有主键、姓名、年龄、性别、出生日期、身份证号、电话号码信息。
二、数据初始化
-
创建t_student表
create table t_student (
id varchar(32) not null,
name varchar(255),
age int,
sex varchar(2),
birthday varchar(20),
idCard varchar(20),
phone varchar(20)
);
-
初始化数据
insert into t_student
values('1001','zhangsan',20,'1','2000-01-01','370701200001011111','15800000001');
insert into t_student
values('1002','lisi',20,'1','2000-01-01','370701200001011112','15800000002');
三、代码实现
1. 创建Spring Boot项目
创建Spring Boot项目,项目名称为springboot-student02。
2. 选择依赖
pom文件如下所示:
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3. 创建配置文件
resources目录下创建application.yml。
# 配置端口号
server:
port: 8090
# 配置数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student
username: root
password: root
# 配置MyBatis
mybatis:
mapper-locations: classpath*:mapper/**/*Mapper.xml
type-aliases-package: com.wfit
4. 创建Constants常量类
com.wfit.boot.commons目录下创建Constants.java。
public class Constants {
// 默认成功码
public static final int SUCCESS_CODE = 200;
public static final String SUCCESS_MSG = "操作成功";
// 默认失败码
public static final int ERROR_CODE = 500;
public static final String ERROR_MSG = "系统异常";
}
5. 创建Result类
com.wfit.boot.commons目录下创建Result.java。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
//响应码
private int code;
//响应消息
private String msg;
//响应结果
private T data;
public static <T> Result<T> success(T data){
return new Result<>(Constants.SUCCESS_CODE,Constants.SUCCESS_MSG,data);
}
public static <T> Result<T> error(T data){
return new Result<>(Constants.ERROR_CODE,Constants.ERROR_MSG,data);
}
}
6. 创建Student实体类
com.wfit.boot.model目录下创建Student.java。
@Data
public class Student {
private String id;
private String name;
private int age;
private String sex;
private String birthday;
private String idCard;
private String phone;
}
7. 创建StudentController类
com.wfit.boot.controller目录下创建StudentController.java。
@RestController //@Controller和@ResponseBody两个注解的结合
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 新增学生信息
*/
@PostMapping("/add") //@RequestMapping(method = {RequestMethod.POST})
public Result addStudent(@RequestBody Student student){
studentService.saveStudent(student);
return Result.success("新增成功!");
}
/**
* 修改学生信息
*/
@PostMapping("/update")
public Result updateStudent(@RequestBody Student student){
studentService.updateStudent(student);
return Result.success("修改成功!");
}
/**
* 删除学生信息
*/
@GetMapping("/del") //@RequestMapping(method = {RequestMethod.GET})
public Result delStudent(String id){
studentService.delStudent(id);
return Result.success("删除成功!");
}
/**
* 查询学生信息
*/
@GetMapping("/query")
public Result queryStudent(){
List<Student> studentList = studentService.queryStudent();
return Result.success(studentList);
}
}
8. 创建StudentService接口
com.wfit.boot.service目录下创建StudentService.java。
public interface StudentService {
public void saveStudent(Student student);
public void updateStudent(Student student);
public void delStudent(String id);
public List<Student> queryStudent();
}
9. 创建StudentServiceImpl类
com.wfit.boot.service.impl目录下创建StudentServiceImpl.java。
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private StudentMapper studentMapper;
@Override
public void saveStudent(Student student) {
studentMapper.saveStudent(student);
}
@Override
public void updateStudent(Student student) {
studentMapper.updateStudent(student);
}
@Override
public void delStudent(String id) {
studentMapper.delStudent(id);
}
@Override
public List<Student> queryStudent() {
return studentMapper.queryStudent();
}
}
10. 创建StudentMapper接口
com.wfit.boot.mapper目录下创建StudentMapper.java。
@Mapper
public interface StudentMapper {
public void saveStudent(Student student);
public void updateStudent(Student student);
public void delStudent(String id);
public List<Student> queryStudent();
}
11. 创建StudentMapper.xml文件
resources.mapper目录下创建StudentMapper.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.wfit.boot.mapper.StudentMapper">
<!--新增学生信息-->
<insert id="saveStudent" parameterType="com.wfit.boot.model.Student">
insert into t_student values(
#{id},
#{name},
#{age},
#{sex},
#{birthday},
#{idCard},
#{phone}
)
</insert>
<!--修改学生信息-->
<update id="updateStudent" parameterType="com.wfit.boot.model.Student">
update t_student
set name = #{name}, age = #{age}
where id = #{id}
</update>
<!--删除学生信息-->
<delete id="delStudent" parameterType="java.lang.String">
delete from t_student where id = #{id}
</delete>
<!--查询学生信息-->
<select id="queryStudent" resultType="com.wfit.boot.model.Student">
select *
from t_student
</select>
</mapper>
12. 项目结构
四、案例测试
1. 测试新增学生信息
2. 测试修改学生信息
3. 测试删除学生信息