mybatis-一对多查询
准备阶段(一个老师对应多个学生)
1.建表
2.编写实体类
package com.lv.pojo; import lombok.Data; @Data public class Student { private int id; private String name; private int tid; }
package com.lv.pojo; import lombok.Data; import java.util.List; @Data public class Teacher { private int id; private String name; //一个老师拥有多个学生 private List<Student> students; }
3.编写Mapper接口
package com.lv.dao; public interface StudentMapper { }
package com.lv.dao; import com.lv.pojo.Teacher; import java.util.List; public interface TeacherMapper { //获取老师 List<Teacher> getTeacher(); }
4.编写Mapper.xml配置文件
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lv.dao.StudentMapper"> </mapper>
<?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lv.dao.TeacherMapper"> <select id="getTeacher" resultType="Teacher"> select * from teacher </select> </mapper>
5.在mybatis-config.xml中添加映射
<mappers> <mapper class="com.lv.dao.TeacherMapper"/> <mapper class="com.lv.dao.StudentMapper"/> </mappers>
6.编写测试文件进行测试
import com.lv.dao.TeacherMapper; import com.lv.pojo.Teacher; import com.lv.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class MyTest { @Test public void getTeacher(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); List<Teacher> teacherList = mapper.getTeacher(); for (Teacher teacher : teacherList) { System.out.println(teacher); } sqlSession.close(); } }
7.测试结果
方式一:按查询嵌套处理(相当于SQL中的子查询)
1.在TeacherMapper接口中编写方法
Teacher getTeacher3(@Param("tid") int id);
2.编写对应的Mapper.xml文件
<resultMap id="TeacherStudent2" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/> </resultMap> <select id="getTeacher3" resultMap="TeacherStudent2"> select * from teacher where id = #{tid} </select> <select id="getStudentByTeacherId" resultType="Student"> select * from student where tid = #{tid} </select>
3.测试
@Test public void getTeacher3(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher3 = mapper.getTeacher3(1); System.out.println(teacher3); sqlSession.close(); }
方式二:按结果嵌套处理(相当于SQL中的连表查询)
1.在TeacherMapper接口中编写方法
//获取指定老师下的所有学生及老师的信息 Teacher getTeacher2(@Param("tid") int id);
2.编写对应的Mapper.xml文件
<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> <!--按照结果嵌套查询--> <select id="getTeacher2" 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>
3.测试
@Test public void getTeacher2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher2(1); System.out.println(teacher2); sqlSession.close(); }
注:
- association 关联 用于一对一和多对一
- collection 集合 用于一对多
- javaType 和 ofType 都用来指定对象类型,区别是:javaType 指定的是实里类中的属性,ofType 指定的是映射到list集合属性中实体类的类型
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术