Mybatis学习之一对多的处理
在阅读本文前请阅读:添加链接描述
一对多的理解
一个老师拥有多个学生,如果对于老师这边,就是一个一对多的现象,即从一个老师下面拥有一群学生(集合)
工具类的编写
package com.peihj.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);
System.out.println("inputStream"+inputStream);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
实体类编写
实体类编写之Students
package com.peihj.pojo;
import lombok.Data;
@Data
public class Student1 {
private String name;
private int id;
private int tid;
}
实体类编写之Teachers
package com.peihj.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher1 {
private int id;
private String name;
private List<Student1> students;
}
编写接口
通过老师的id去查询对应的学生集合
package com.peihj.dao;
import com.peihj.pojo.Teacher1;
public interface TeacherMapper1 {
public Teacher1 getTeacher(int id);
public Teacher1 getTeacher2(int id);
}
按结果嵌套处理
编写接口对应的Mapper配置文件
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.id tid,t.name tname from student s ,teacher t where t.id = s.tid and t.id = #{id}
</select>
<resultMap id="TeacherStudent" type="Teacher1">
<result property="name" column="tname"/>
<result property="id" column="tid"/>
<!--因为他是一个集合所有我们需要使用collection-->
<collection property="students" ofType="com.peihj.pojo.Student1">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
</collection>
</resultMap>
</mapper>
思路: 1. 从学生表和老师表中查出学生id,学生姓名,老师姓名 2. 对查询出来的操作做结果集映射 3. 集合的话,使用collection,JavaType和ofType都是用来指定对象类型的,JavaType是用来指定pojo中属性的类型,ofType指定的是映射到list集合属性中pojo的类型。
通过SQL语句进行查询,并通过resultMap进行联系,将SQL语句用到的参数(sid,sname,tid,tname)在参数。
将Mapper文件注册到MyBatis-config文件中
<mappers>
<!--加载sql文件的映射文件-->
<!--如果使用注解开发,这里就别设置了,不然冲突还报错-->
<!-- <mapper resource="com/peihj/dao/UserMapper.xml"/>-->
<!--使用class绑定接口-->
<mapper resource="com/peihj/dao/TeacherMapper1.xml"/>
</mappers>
编写测试类
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper1 teacherMapper1 = sqlSession.getMapper(TeacherMapper1.class);
Teacher1 studentList = teacherMapper1.getTeacher(1);
System.out.println(studentList);
}
按查询嵌套处理
TeacherMapper接口编写方法
package com.peihj.dao;
import com.peihj.pojo.Teacher1;
public interface TeacherMapper1 {
public Teacher1 getTeacher(int id);
public Teacher1 getTeacher2(int id);
}
编写接口对应的Mapper配置文件
注意我们利用方法1,通过结果嵌套处理,是不需要配置javatype的,但是通过查询嵌套处理是需要配置javatype(该复杂参数是啥类型),column是该外键与另外一个表的连接纽带,这里是id。
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from teacher where id = #{id}
</select>
<resultMap id="TeacherStudent2" type="Teacher1">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--column是一对多的外键 , 写的是一的主键的列名,再本项目就是指代id-->
<collection property="students" ofType="Student1" javaType="ArrayList" column="id" select="searchstudent"/>
</resultMap>
<select id="searchstudent" resultType="Student1">
select * from student where student.tid = #{id}
</select>
将Mapper文件注册到MyBatis-config文件中
测试类
@Test
public void test1(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper1 teacherMapper1 = sqlSession.getMapper(TeacherMapper1.class);
Teacher1 teacher2 = teacherMapper1.getTeacher2(1);
System.out.println(teacher2);
}
参考
https://www.bilibili.com/video/BV1NE411Q7Nx?p=21&t=1575.0
|