在数据库ssm中建立一张学生 表student,该表有如下字段: id, name, stuid, age; 利用mybatis知识,完成数据库表的增删改操作。核心java代码文件即可。
时间: 2023-04-07 15:05:13 浏览: 111
以下是核心Java代码文件:
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.example.mapper.StudentMapper">
<resultMap id="studentMap" type="com.example.entity.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="stuid" column="stuid"/>
<result property="age" column="age"/>
</resultMap>
<select id="getStudentById" resultMap="studentMap">
SELECT * FROM student WHERE id=#{id}
</select>
<select id="getAllStudents" resultMap="studentMap">
SELECT * FROM student
</select>
<insert id="addStudent" parameterType="com.example.entity.Student">
INSERT INTO student(name, stuid, age) VALUES(#{name}, #{stuid}, #{age})
</insert>
<update id="updateStudent" parameterType="com.example.entity.Student">
UPDATE student SET name=#{name}, stuid=#{stuid}, age=#{age} WHERE id=#{id}
</update>
<delete id="deleteStudentById" parameterType="int">
DELETE FROM student WHERE id=#{id}
</delete>
</mapper>
```
StudentMapper.java
```
package com.example.mapper;
import com.example.entity.Student;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student WHERE id=#{id}")
Student getStudentById(int id);
@Select("SELECT * FROM student")
List<Student> getAllStudents();
@Insert("INSERT INTO student(name, stuid, age) VALUES(#{name}, #{stuid}, #{age})")
void addStudent(Student student);
@Update("UPDATE student SET name=#{name}, stuid=#{stuid}, age=#{age} WHERE id=#{id}")
void updateStudent(Student student);
@Delete("DELETE FROM student WHERE id=#{id}")
void deleteStudentById(int id);
}
```
Student.java
```
package com.example.entity;
public class Student {
private int id;
private String name;
private String stuid;
private int age;
public Student() {}
public Student(int id, String name, String stuid, int age) {
this.id = id;
this.name = name;
this.stuid = stuid;
this.age = age;
}
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 String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
阅读全文