课后作业
一、用Navicat Premium 12软件创建三张表:学生表、选课表、课程表
学生表(学号, 姓名, 性别, 年龄, 电话) - 学号是字符串类型
选课表(学号, 课程号, 成绩)
课程表(课程号, 课程名, 课时数) - 课程号是字符串类型
学生表:
选课表:
课程表:
二、创建实体类
1、创建学生实体类
package net.lhf.mybatis.bean;/*** 功能:学生实体类* 作者:李红芙* 日期:2021年03月14日*/
public class Student {private int id;private String name;private String gender;private int age;private Clazz clazz;private int phone;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 getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Clazz getClazz() {return clazz;}public void setClazz(Clazz clazz) {this.clazz = clazz;}public int getPhone() {return phone;}public void setPhone(int phone) {this.phone = phone;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", gender='" + gender + '\'' +", age=" + age +", clazz=" + clazz.getName() +",phone="+ phone +'}';}
}
2、创建选课实体类
package net.lhf.mybatis.bean;/*** 功能:选课实体类* 作者:李红芙* 日期:2021年3月14日*/
public class Curr {private int id;private int number;private int grade;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public int getGrade() {return grade;}public void setGrade(int grade) {this.grade = grade;}@Overridepublic String toString() {return "Clazz{" +"id=" + id +", number='" + number + '\'' +", grade=" + grade +'}';}
}
3、创建课程实体类
package net.lhf.mybatis.bean;/*** 功能:课程实体类* 作者:李红芙* 日期:2021年3月14日*/public class Course {private int number;private String name;private int hour;public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHour() {return hour;}public void setHour(int hour) {this.hour = hour;}@Overridepublic String toString() {return "Course{" +"number=" + number +", name='" + name + '\'' +", hour=" + hour +'}';}
}
三、创建映射器接口
创建课程映射器接口
package net.lhf.mybatis.mapper;import net.lhf.mybatis.bean.Course;
import net.lhf.mybatis.bean.Curr;
import net.lhf.mybatis.bean.Student;import java.util.List;/***功能:课程映射器接口* 作者:李红芙* 日期:2021年03月14日*/
public interface CourseMapper {Course findById(int id);List<Course> findAll();int number(Curr number);int id(Student id);
}
四、创建测试类TestStudentMapper
package net.tp.mybatis.mapper;
import net.tp.mybatis.bean.Student;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;/*** 功能:测试学生映射器接口* 作者:李红芙* 日期:2021年03月10日*/
public class TestStudentMapper {private SqlSession sqlSession; // SQL会话private StudentMapper studentMapper; // 学生映射器@Beforepublic void init() {try {// 读取MyBatis配置文件作为字符输入流Reader reader = Resources.getResourceAsReader("mybatis-config.xml");// 基于MyBatis配置文件构建SQL会话工厂SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);// 利用SQL会话工厂获取SQL会话sqlSession = factory.openSession();// 利用SQL会话获取用户映射器对象studentMapper = sqlSession.getMapper(StudentMapper.class);} catch (IOException e) {e.printStackTrace();}}@Testpublic void testFindByCondition() {HashMap<String,Object> condition =new HashMap<String, Object>();condition.put("gender","女");condition.put("age",20);condition.put("name","唐");List<Student> students=studentMapper.findByCondition(condition);students.forEach(student -> System.out.println(student));if(students.size() >0){students.forEach(student -> System.out.println(student));}else {System.out.println("温馨提示:没有找到符合的学生记录~");}}@Afterpublic void destroy() {// 关闭SQL会话sqlSession.close();}}
五、运行测试方法testFindByCondition(),查看结果