CRUD - SpringBoot
1、简单的SQL脚本
create database cruddb;
use cruddb;
create table `student`
(
`student_id` int primary key auto_increment comment 'ID',
`student_name` varchar(20) not null comment '姓名',
`student_age` int not null comment '年龄',
`student_gender` int not null comment '性别(0女,1男)'
);
insert into `student`
values (0, '张三', 17, 1),
(0, '李四', 18, 1),
(0, '王五', 17, 1),
(0, '小红', 18, 0),
(0, '星期八领证', 17, 1);
2、创建项目
3、代码编写
1、yaml 配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cruddb?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
thymeleaf:
cache: false
mybatis:
type-aliases-package: com.xqblz.crud.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
cache-enabled: true
map-underscore-to-camel-case: true
2、创建pojo 包,编写实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer studentId;
private String studentName;
private Integer studentAge;
private Integer studentGender;
}
3、创建 mapper 包,写 mapper 文件
@Mapper
public interface StudentMapper {
List<Student> findStudentByName(@Param("name") String name);
Student findStudentById(@Param("id") Integer id);
int addStudent(Student student);
int updateStudent(Student student);
int deleteStudentById(@Param("id") Integer id);
}
4、创建 mybatis/mapper 目录,编写 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.it.crud.mapper.StudentMapper">
<select id="findStudentByName" resultType="com.it.crud.pojo.Student">
select student_id, student_name, student_age, student_gender
from student
<where>
<if test="name != null and name != ''">
student_name like concat('%', #{name} , '%')
</if>
</where>
</select>
<insert id="addStudent">
insert into `student`
values (0, #{studentName}, #{studentAge}, #{studentGender})
</insert>
<update id="updateStudent">
update student
set student_name = #{studentName},
student_age = #{studentAge},
student_gender = #{studentGender}
where student_id = #{studentId}
</update>
<delete id="deleteStudentById">
delete
from student
where student_id = #{id}
</delete>
<select id="findStudentById" resultType="com.it.crud.pojo.Student">
select student_id, student_name, student_age, student_gender
from student
where student_id = #{id}
</select>
</mapper>
5、创建 service
public interface StudentService {
List<Student> findStudentByName(String name);
Student findStudentById(Integer id);
boolean addStudent(Student student);
boolean updateStudent(Student student);
boolean deleteStudentById(Integer id);
}
6、创建 impl 实现 service 接口
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public List<Student> findStudentByName(String name) {
return studentMapper.findStudentByName(name);
}
@Override
public Student findStudentById(Integer id) {
return studentMapper.findStudentById(id);
}
@Override
public boolean addStudent(Student student) {
return studentMapper.addStudent(student) > 0;
}
@Override
public boolean updateStudent(Student student) {
return studentMapper.updateStudent(student) > 0;
}
@Override
public boolean deleteStudentById(Integer id) {
return studentMapper.deleteStudentById(id) > 0;
}
}
7、创建 controller
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/findStudent")
public String findStudent(@RequestParam(name = "name", required = false) String name, Model model) {
model.addAttribute("studentList", studentService.findStudentByName(name));
model.addAttribute("name", name);
return "index";
}
@GetMapping("/goAddStudent")
public String goAddStudent() {
return "add";
}
@PostMapping("/addStudent")
public String addStudent(Student student, Model model) {
if (studentService.addStudent(student)) {
model.addAttribute("info", "success");
} else {
model.addAttribute("info", "error");
}
return "add";
}
@GetMapping("/goUpdateUpdate")
public String goUpdateUpdate(@RequestParam("id") Integer id, Model model) {
model.addAttribute("student", studentService.findStudentById(id));
return "update";
}
@PostMapping("/updateStudent")
public String updateStudent(Student student, Model model) {
if (studentService.updateStudent(student)) {
model.addAttribute("info", "success");
} else {
model.addAttribute("info", "error");
}
return "update";
}
@RequestMapping("/deleteStudent")
public String deleteStudent(@RequestParam("id") Integer id, Model model) {
if (studentService.deleteStudentById(id)) {
model.addAttribute("info", "success");
} else {
model.addAttribute("info", "error");
}
return "index";
}
}
8、页面创建
9、index 页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<div style="text-align: center">
<form method="get" action="/student/findStudent">
学生姓名: <input name="name" th:value="${ name }">
<input type="submit" value="查询"> <a href="/student/goAddStudent">添加</a>
</form>
<table width="500" border="1" style="margin: 0 auto">
<tr>
<td>ID</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>操作</td>
</tr>
<tr th:each="student : ${studentList}">
<td th:text="${student.studentId}"></td>
<td th:text="${student.studentName}"></td>
<td th:text="${student.studentAge}"></td>
<td>
<span th:if="${ student.studentGender == 0 }">女</span>
<span th:if="${ student.studentGender == 1 }">男</span>
</td>
<td>
<a th:href="'/student/goUpdateUpdate?id='+${student.studentId}">修改</a>
<a th:href="'/student/deleteStudent?id='+${student.studentId}">删除</a>
</td>
</tr>
</table>
</div>
<script th:inline="javascript">
let info = [[ ${ info }]];
if (info != null){
if (info === 'success') {
alert("删除成功");
} else {
alert("出现异常情况,删除失败");
}
location.href = '/student/findStudent';
}
</script>
</body>
</html>
10、add 页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加</title>
</head>
<body>
<h1>添加信息</h1>
<form action="/student/addStudent" method="post">
<input type="hidden" name="studentId">
姓名: <input type="text" name="studentName"><br>
年龄: <input type="number" name="studentAge"><br>
性别:
<input type="radio" value="1" name="studentGender"> 男
<input type="radio" value="0" name="studentGender"> 女 <br>
<input value="添加" type="submit">
<a href="/student/findStudent">返回</a>
</form>
<script th:inline="javascript">
let info = [[ ${ info }]];
if (info != null){
if (info === 'success') {
alert("添加成功");
} else {
alert("出现异常情况,添加失败");
}
location.href = '/student/findStudent';
}
</script>
</body>
</html>
11、update 页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改</title>
</head>
<body>
<h1>修改信息</h1>
<form action="/student/updateStudent" method="post">
<input type="hidden" name="studentId" th:value="${ student.studentId }">
姓名: <input type="text" name="studentName" th:value="${ student.studentName }"><br>
年龄: <input type="number" name="studentAge" th:value="${ student.studentAge }"><br>
性别:
<input type="radio" value="1" name="studentGender" th:checked="${ student.studentGender == 1 }"> 男
<input type="radio" value="0" name="studentGender" th:checked="${ student.studentGender == 0 }"> 女 <br>
<input value="修改" type="submit">
<a href="/student/findStudent">返回</a>
</form>
<script th:inline="javascript">
let info = [[ ${ info }]];
if (info != null){
if (info === 'success') {
alert("修改成功");
} else {
alert("出现异常情况,修改失败");
}
location.href = '/student/findStudent';
}
</script>
</body>
</html>
4、代码完成启动测试