基于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');
创建Spring Boot项目,项目名称为springboot-student02。
pom文件如下所示:
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.3.0
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
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
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 = "系统异常";
}
com.wfit.boot.commons目录下创建Result.java。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
//响应码
private int code;
//响应消息
private String msg;
//响应结果
private T data;
public static Result success(T data){
return new Result<>(Constants.SUCCESS_CODE,Constants.SUCCESS_MSG,data);
}
public static Result error(T data){
return new Result<>(Constants.ERROR_CODE,Constants.ERROR_MSG,data);
}
}
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;
}
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 studentList = studentService.queryStudent();
return Result.success(studentList);
}
}
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 queryStudent();
}
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 queryStudent() {
return studentMapper.queryStudent();
}
}
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 queryStudent();
}
resources.mapper目录下创建StudentMapper.xml。
insert into t_student values(
#{id},
#{name},
#{age},
#{sex},
#{birthday},
#{idCard},
#{phone}
)
update t_student
set name = #{name}, age = #{age}
where id = #{id}
delete from t_student where id = #{id}