public class Student {
private Integer id;
private String name;
private Boolean age;
private String sex;
private StudentStatus studentStatus;
// set and get
}
public class StudentStatus {
private Integer id;
private String num;
private String major;
// set and get
}
1.1.3 创建 DAO 接口
public class StudentStatus {
private Integer id;
private String num;
private String major;
// set and get
}
public class Course {
private Integer cId;
private String cName;
private List students;
// set and get
}
public class Student {
private Integer sId;
private String sName;
private Long sAge;
private String sSex;
private List courses;
// set and get
}
1.3.3 创建 DAO 接口
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/9/3
* @description course DAO 接口
*/
public interface CourseDao {
public List findAll();
}
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/9/3
* @description student DAO 接口
*/
public interface StudentDao {
public List findAll();
}
1.3.4 配置 mapper
☞ student-mapper.xml
<&#63;xml version="1.0" encoding="UTF-8" &#63;>
&#9758; course-mapper.xml
<&#63;xml version="1.0" encoding="UTF-8" &#63;>
1.3.5 测试
/**
* Created with IntelliJ IDEA.
*
* @author Demo_Null
* @date 2020/9/3
* @description 测试类
*/
public class MybatisDemo {
@Test
public void TestA() throws IOException {
// 加载核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
// 获得 sqlSession 工厂对象
SqlSessionFactory sqlSessiOnFactory= new SqlSessionFactoryBuilder().build(resourceAsStream);
// 获得 sqlSession 对象
SqlSession sqlSession = sqlSessionFactory.openSession();
List courseList = sqlSession.selectList("com.software.mybatis.dao.CourseDao.findAll");
List studentList = sqlSession.selectList("com.software.mybatis.dao.StudentDao.findAll");
System.out.println("### 课程 ###");
for (Course course : courseList) {
System.out.println(course);
}
System.out.println("### 学生 ###");
for (Student student : studentList) {
System.out.println(student);
}
}
}