实验五: Mybatis注解 实验内容: 创建学生表和班级表,学生表:student(id,name,age,class_id),班级表:class(id,classname), 并输入几行数据。 使用MyBatis注解实现下列要求; (1)MyBatis注解实现查询操作。查询id为2的学生的信息。 (2)MyBatis注解实现修改操作。修改id为4的学生的姓名修改为李雷,年龄修改为21。 (3)MyBatis注解实现一对多查询。查询出二班所有学生的信息。实验步骤
- 1.Classes
- 2.Student
- 3.ClassMapper
- 4.StudentMapper
- 5.MybatisUtils
- 6.db.properties
- 7.mybatis-config.xml
- 8.Test
package pojo;
import java.util.List;
public class Classes {
private int id;
private String classname;
private List studentList; // 学生集合
public List getStudentList() {
return studentList;
}
public void setStudentList(List studentList) {
this.studentList = studentList;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
@Override
public String toString() {
return "Classes{" +
"id=" + id +
", classname='" + classname + ''' +
", studentList=" + studentList +
'}';
}
}
2.Student
package pojo;
public class Student {
private Integer id;
private String name;
private Integer age;
private Integer class_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getClass_id() {
return class_id;
}
public void setClass_id(Integer class_id) {
this.class_id = class_id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", class_id=" + class_id +
'}';
}
}
3.ClassMapper
package mapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import pojo.Classes;
import java.util.List;
public interface ClassMapper {
@Select("select * from classes ")
List selectAllClass();
@Select("select * from classes where id=#{id}")
@Results({@Result(id=true,column = "id",property = "id"),
@Result(column = "classname",property = "classname"),
@Result(column = "id",property = "studentList",
many=@Many(select = "mapper.StudentMapper.selectStudentByClassId"))
})
Classes selectClassByClassId(int id);
}
4.StudentMapper
package mapper;
import org.apache.ibatis.annotations.*;
import pojo.Student;
import java.util.List;
public interface StudentMapper {
@Select("select * from student ")
List selectAllStudent();
@Select("select * from student where id=#{id}")
Student selectStudent(int id);
@Update("update student set name=#{name},age=#{age} where id=#{id}")
int updateStudent(Student student);
@Select("select * from student where class_id=#{id}")
@Results({@Result(id=true,column = "id",property = "id"),
@Result(column = "name",property = "name"),
@Result(column = "age",property = "age"),
@Result(column = "class_id",property = "class_id"),
})
List selectStudentByClassId(int class_id);
}
5.MybatisUtils
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
catch (IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlsession(){
return sqlSessionFactory.openSession();
}
}
6.db.properties
mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:43306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true mysql.username=root mysql.password=1237.mybatis-config.xml
8.Test
import mapper.ClassMapper;
import mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Classes;
import pojo.Student;
import utils.MybatisUtils;
import java.util.List;
public class Test {
@org.junit.Test
public void findAllStudentTest(){
SqlSession session= MybatisUtils.getSqlsession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List students =mapper.selectAllStudent();
for(Student student:students) {
System.out.println(student);
}
session.close();
}
@org.junit.Test
public void findAllClassTest(){
SqlSession session= MybatisUtils.getSqlsession();
ClassMapper mapper = session.getMapper(ClassMapper.class);
List classess =mapper.selectAllClass();
for(Classes class1:classess) {
System.out.println(class1);
}
session.close();
}
@org.junit.Test
public void findStudentByIdTest(){
SqlSession session= MybatisUtils.getSqlsession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = mapper.selectStudent(2);
System.out.println(student.toString());
session.close();
}
@org.junit.Test
public void updateStudentByIdTest(){
SqlSession session= MybatisUtils.getSqlsession();
Student student = new Student();
student.setId(4);
student.setName("李雷");
student.setAge(21);
StudentMapper mapper = session.getMapper(StudentMapper.class);
int result = mapper.updateStudent(student);
if(result>0)
System.out.println("成功更新"+result+"条数据,其内容为"+student.toString());
else
System.out.println("更新失败");
session.commit();
session.close();
}
@org.junit.Test
public void findStudentbyClassTest(){
SqlSession session= MybatisUtils.getSqlsession();
ClassMapper mapper = session.getMapper(ClassMapper.class);
Classes class2 = mapper.selectClassByClassId(1002);
System.out.println(class2.toString());
session.close();
}
}
实验截图:
原始数据
(1)MyBatis注解实现查询操作。查询id为2的学生的信息。
(2)MyBatis注解实现修改操作。修改id为4的学生的姓名修改为李雷,年龄修改为21。
(3)MyBatis注解实现一对多查询。查询出二班所有学生的信息。
实验小结:
本章掌握了基于注解的单表增删改查,并熟悉了基于注解的一对一、一对多、多对多的关联查询。



