1:创建student表及在entity包下创建实体类
Student实体类
package com.wen.entity;
//学生实体类
public class Student {
private String stuNo;
private String stuName;
private int stuAge;
private String stuAddress;
public Student() {
}
public Student(String stuNo, String stuName, int stuAge, String stuAddress) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuAddress = stuAddress;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
@Override
public String toString() {
return "Student{" +
"stuNo='" + stuNo + '\'' +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
", stuAddress='" + stuAddress + '\'' +
'}';
}
}
编写Mapper接口
StudentMapper
package com.wen.dao;
//Mapper接口类:方法名返回值及参数类型与StudentMapper.xml文件的id、返回值和输入参数保持一致
import com.wen.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository//注册:将Mapper接口声明成容器组件中的一部分
/*
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
*/
@Mapper//声明为Mapper接口
public interface StudentMapper {
//增加学生
void addStudent(Student student);
//根据学号删除学生
void deleteStudentByStuNo(String stuNo);
//根据学号修改学生
void updateStudentByStuNo(Student student);
//根据学号查询学生
Student queryStudentByStuNo(String stuNo);
//查询全部学生
List<Student> queryAllStudents();
}
编写StudentMapper.xml文件
StudentMapper.xml
<mapper namespace="com.wen.dao.StudentMapper">
<insert id="addStudent" parameterType="com.wen.entity.Student">
insert into student values (#{stuNo},#{stuName},#{stuAge},#{stuAdddress})
insert>
<delete id="deleteStudentByStuNo" parameterType="String">
delete from student where stuNo=#{stuNo}
delete>
<update id="updateStudentByStuNo" parameterType="com.wen.entity.Student">
update student set stuName=#{stuName},stuAge=#{stuAge},stuAddress=#{stuAddress} where stuNo=#{stuNo}
update>
<select id="queryStudentByStuNo" parameterType="String" resultType="com.wen.entity.Student">
select * from student where stuNo=#{stuNo}
select>
<select id="queryAllStudents" resultType="com.wen.entity.Student">
select * from student
select>
mapper>
编写service层
StudentService
package com.wen.service;
import com.wen.dao.StudentMapper;
import com.wen.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
//service层
@Service//将service声明成容器组件中的一部分
/*
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
*/
public class StudentService {
@Autowired//将StudentMapper接口自动注入到容器中,StudentService依赖于StudentMapper接口
private StudentMapper studentMapper;
//增加学生方法
public void addStudent(Student student){
//该学号学生不存在
if(studentMapper.queryStudentByStuNo(student.getStuNo())==null){
//调用增加学生方法
studentMapper.addStudent(student);
System.out.println("增加成功");
}else{
System.out.println("已存在该学生,增加失败");
}
}
//根据学号删除学生
public void deleteStudentByStuNo(String stuNo){
//该学号学生存在
if(studentMapper.queryStudentByStuNo(stuNo)!=null){
//调用删除学生方法
studentMapper.deleteStudentByStuNo(stuNo);
System.out.println("删除成功");
}else{
System.out.println("不存在该学生,删除失败");
}
}
//根据学号修改学生
public void updateStudentByStuNo(Student student){
//该学号学生存在
if(studentMapper.queryStudentByStuNo(student.getStuNo())!=null){
//调用修改学生方法
studentMapper.updateStudentByStuNo(student);
System.out.println("修改成功");
}else{
System.out.println("已存在该学生,修改失败");
}
}
//根据学号查询学生
public Student queryStudentByStuNo(String stuNo){
return studentMapper.queryStudentByStuNo(stuNo);
}
//查询全部学生
public List<Student> queryAllStudents(){
return studentMapper.queryAllStudents();
}
}
编写controller层
StudentController
package com.wen.controller;
import com.wen.entity.Student;
import com.wen.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
//跳转增加学生页面
@RequestMapping("/add")
public String add(){
return "addStudent";
}
//增加学生
@RequestMapping("/addStudent")
public String addStudent(HttpServletRequest request){
//获取增加学生的信息
String stuNo=request.getParameter("stuNo");
String stuName=request.getParameter("stuName");
int stuAge=Integer.parseInt(request.getParameter("stuAge"));
String stuAddress=request.getParameter("stuAddress");
//封装学生信息
Student student=new Student(stuNo,stuName,stuAge,stuAddress);
//增加学生
studentService.addStudent(student);
//增加完之后需调用查询全部学生方法
return "redirect:/queryAllStudents";
}
//根据学号删除学生
@RequestMapping("/deleteStudentByStuNo")
public String deleteStudentByStuNo(String stuNo){
//删除学生
studentService.deleteStudentByStuNo(stuNo);
//删除完之后需调用查询全部学生方法
return "redirect:/queryAllStudents";
}
//根据学号修改学生
@RequestMapping("/updateStudentByStuNo")
public String updateStudentByStuNo(HttpServletRequest request){
//获取修改学生的信息
String stuNo=request.getParameter("stuNo");
String stuName=request.getParameter("stuName");
int stuAge=Integer.parseInt(request.getParameter("stuAge"));
String stuAddress=request.getParameter("stuAddress");
//封装学生信息
Student student=new Student(stuNo,stuName,stuAge,stuAddress);
//修改学生
studentService.updateStudentByStuNo(student);
//修改完之后需调用查询全部学生方法
return "redirect:/queryAllStudents";
}
//根据学号查询学生
@RequestMapping("/queryStudentByStuNo")
public String queryStudentByStuNo(String stuNo,Map<String,Object> map){
Student student=studentService.queryStudentByStuNo(stuNo);
//将学生信息放到map集合中
map.put("student",student);
return "studentInfo";
}
//查询全部学生
public String queryAllStudents(Map<String,Object> map){
List<Student> list=studentService.queryAllStudents();
//将学生信息放到map集合中
map.put("student",list);
return "studentIndex";
}
}
在application.properties中添加Mybatis配置,扫描mapper.xml
application.properties
#数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mvc?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#mapper.xml文件所在包
mybatis.mapper-locations=classpath:/mapper/*.xml
#实体类所在包
mybatis.type-aliases-package=com.wen.entity
编写SpringbootApplication类
package com.wen;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.wen.dao")//扫描Mapper接口包下的所有接口类
@SpringBootApplication
public class SpringbootStudentcrudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootStudentcrudApplication.class, args);
}
}
studentIndex.html(全体学生页面)
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>三年二班全体学生信息页面title>
head>
<body>
<h1>三年二班全体学生信息h1>
<hr><br><br>
<table border="1">
<tr>
<th>学号th>
<th>姓名th>
<th>年龄th>
<th>地址th>
<th>操作th>
<th>操作th>
tr>
<tr th:each="student:${student}">
<th th:text="${student.getStuNo()}">th>
<th th:text="${student.getStuName()}">th>
<th th:text="${student.getStuAge()}">th>
<th th:text="${student.getStuAddress()}">th>
<th><a th:href="@{/queryStudentByStuNo(stuNo=${student.getStuNo()})}">编辑a>th>
<th><a th:href="@{/deleteStudentByStuNo(stuNo=${student.getStuNo()})}">删除a>th>
tr>
table>
<a th:href="@{/add}"><input type="button" value="增加">a>
body>
html>
studentInfo(学生个人信息页面)
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>学生个人信息页面title>
head>
<body>
<h1>学生个人信息h1>
<hr><br><br>
<form action="/updateStudentByStuNo" method="post" th:each="student:${student}">
学号:<input type="text" name="stuNo" th:value="${student.getStuNo()}" readonly="readonly"><br><br>
姓名:<input type="text" name="stuName" th:value="${student.getStuName()}"><br><br>
年龄:<input type="text" name="stuAge" th:value="${student.getStuAge()}"><br><br>
地址:<input type="text" name="stuAddress" th:value="${student.getStuAddress()}"><br><br>
<input type="submit" value="修改学生">
form>
body>
html>
addStudent(增加学生页面)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>增加学生页面title>
head>
<body>
<h1>增加学生h1>
<hr><br><br>
<form action="/addStudent" method="post">
学号:<input type="text" name="stuNo"><br><br>
姓名:<input type="text" name="stuName"><br><br>
年龄:<input type="text" name="stuAge"><br><br>
地址:<input type="text" name="stuAddress"><br><br>
<input type="submit" value="增加学生">
form>
body>
html>
删除学生