学习内容:
创建数据库:
- student表:
- teacher表:
1. 多对一
- 多个学生,对应一个老师
- 对于学生这边而言, 关联 … 多个学生,关联一个老师 【多对一】
1.1、测试环境搭建
- 导入lombok
- 新建实体类 Teacher,Student
- 建立Mapper接口
- 建立Mapper.XML文件
- 在核心配置文件中绑定注册我们的Mapper接口或者文件!【方式很多,随心选】
- 测试查询是否能够成功!
- Teacher:
@Data
public class Teacher {
private int id;
private String name;
}
- Student:
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
- mybatis-config.xml:
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<package name="pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="dao.TeacherMapper"/>
<mapper class="dao.StudentMapper"/>
</mappers>
</configuration>
1.2、按照查询嵌套处理(子查询)
- 在StudentMapper接口中编写抽象方法:
//查询所有的学生信息,以及对应的老师的信息
public List<Student> getStudent();
- 在 StudentMapper.xml中编写对应的sql语句:
<!-- 按照查询嵌套处理 -->
<!--
思路:
1.查询所有的学生信息
2.根据查询出来的学生的tid,寻找对应的老师(子查询)
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性,我们需要单独处理 对象:association 集合:collection-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{tid};
</select>
- 编写测试程序:
@Test
public void getStudent(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent();
for(Student student:studentList){
System.out.println(student);
}
sqlSession.close();
}
结果:
1.3、按照结果嵌套处理
- 在StudentMapper接口中编写抽象方法:
//查询所有的学生信息,以及对应的老师的信息
public List<Student> getStudent2();
- 在 StudentMapper.xml中编写对应的sql语句:
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname from mybatis.student s,mybatis.teacher t where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
- 编写测试程序:
@Test
public void getStudent2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudent2();
for(Student student:studentList){
System.out.println(student);
}
sqlSession.close();
}
结果:
(这里的Teacher中的id=0是因为上面是sql语句并没有查询老师的id)
2. 一对多
- 多个学生,对应一个老师
- 对于老师而言, 集合 , 一个老师,有很多学生 【一对多】
2.1、环境搭建,和刚才一样
两个实体类的改变:
- Teacher:
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
- Student:
@Data
public class Student {
private int id;
private String name;
private int tid;
}
2.2、按照结果嵌套处理
- 在TeacherMapper接口中编写抽象方法:
//获取指定老师下的所有学生信息
Teacher getTeacher1(@Param("tid") int tid);
- 在 TeacherMapper.xml中编写对应的sql语句:
<!--按结果嵌套查询-->
<!--获取指定老师下的所有学生信息-->
<select id="getTeacher1" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid from student s ,teacher t where s.tid=t.id and t.id=#{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--
复杂的属性,我们需要单独处理 对象:association 集合:collection
javaType="" 指定属性的类型
集合中的泛型信息我们使用ofType获取
-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
- 编写测试程序:
//获取指定老师下的所有学生信息
@Test
public void getTeacher1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher=mapper.getTeacher1(1);
System.out.println(teacher);
sqlSession.close();
}
结果:
2.3、按照查询嵌套处理(子查询)
- 在TeacherMapper接口中编写抽象方法:
//获取指定老师下的所有学生信息
Teacher getTeacher2(@Param("tid") int tid);
- 在 TeacherMapper.xml中编写对应的sql语句:
<!--子查询-->
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from student where tid=#{tid}
</select>
- 编写测试程序:
//获取指定老师下的所有学生信息
@Test
public void getTeacher2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher=mapper.getTeacher2(1);
System.out.println(teacher);
sqlSession.close();
}
结果:
3. 小结
- 关联 - association 【多对一】
- 集合 - collection 【一对多】
- javaType & ofType
- JavaType 用来指定实体类中属性的类型
- ofType 用来指定映射到List或者集合中的 pojo类型,泛型中的约束类型!
- javatype和ofType 都是用来指明对象类型, 区别在于使用的场合不一样, javatype是在pojo属性类型, ofType指定是当对象需要Collection进行list集合映射对象的时候使用 ofType,也就是一对多映射的时候使用
注意点:
- 保证SQL的可读性,尽量保证通俗易懂
- 注意一对多和多对一中,属性名和字段的问题!
- 如果问题不好排查错误,可以使用日志 , 建议使用 Log4j