package com.example.entity;
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String class;
// 省略 getter 和 setter 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getClass() {
return class;
}
public void setClass(String class) {
this.class = class;
}
}
2、Dao层代码
package com.example.dao;
import com.example.entity.Student;
import java.util.List;
public interface StudentsDao {
// 插入学生记录
int insertStudent(Student student);
// 删除指定条件的学生记录
int deleteStudentById(int id);
// 更新学生记录
int updateStudent(Student student);
// 查询所有学生记录
List<Student> selectAllStudents();
// 根据 ID 查询学生记录
Student selectStudentById(int id);
}
3、StudentsMapper.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.dao.StudentsDao">
<!-- 插入学生记录 -->
<insert id="insertStudent">
INSERT INTO students (name, age, gender, class)
VALUES (#{name}, #{age}, #{gender}, #{class})
</insert>
<!-- 删除指定条件的学生记录 -->
<delete id="deleteStudentById">
DELETE FROM students WHERE id = #{id}
</delete>
<!-- 更新学生记录 -->
<update id="updateStudent">
UPDATE students
SET name = #{name}, age = #{age}, gender = #{gender}, class = #{class}
WHERE id = #{id}
</update>
<!-- 查询所有学生记录 -->
<select id="selectAllStudents" resultType="com.example.entity.Student">
SELECT * FROM students
</select>
<!-- 根据 ID 查询学生记录 -->
<select id="selectStudentById" resultType="com.example.entity.Student">
SELECT * FROM students WHERE id = #{id}
</select>
</mapper>