标签:一对 处理 查询 嵌套 teacher tid Mybatis05 id select
Mybatis05-一对多和多对一处理
多对一的处理
- 将配置文件等完成
查询嵌套处理
- 思路
1. 查询所有的学生信息
2. 根据所有的学生tid,查询对应的老师 - StudentMapper.xml
<select id="getStudent" resultMap="Student_Teacher">
select * from student;
</select>
<resultMap id="Student_Teacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性,需要单独处理 对象:association 集合:collection -->
<!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{tid}
</select>
- 理解:如同数据库中的嵌套查询。首先查出所有的student,然后根据学生的teacher属性,传入tid来查询对应的teacher。类似于Sql中的子查询
按照结果嵌套处理
- 思路:直接查询出结果,进行结果集的映射
- StudentMapper.xml
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="Student_Teacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid = t.id;
</select>
<!--在sql中起了别名,列应该就变为别名-->
<resultMap id="Student_Teacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<!--复杂类型-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
- 理解:从两个表中查出结果,然后在resultMap中对结果的每一个字段进行相应处理。类似于Sql中的联表查询
一对多的处理
根据嵌套查询方式
- TeacherMapper.xml
<!--按照嵌套查询方式-->
<select id="getTeacher2" resultMap="Teacher_student_2">
select * from teacher where id = #{tid}
</select>
<resultMap id="Teacher_student_2" type="teacher">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--因为student类型为List,因此javaType="ArrayList",其中的具体类型为"Student"-->
<collection property="students" column="id" javaType="ArrayList" ofType="Student" select="getStudent"/>
</resultMap>
<select id="getStudent" resultType="Student">
select * from student where tid = #{id}
</select>
根据结果嵌套查询
- TeacherMapper.xml
<!--按照结果嵌套查询-->
<select id="getTeacher" resultMap="Teacher_Student">
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="Teacher_Student" type="teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--javaType ="" 执行属性的类型 集合中的泛型信息,使用ofType获取-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
总结
- 知识点:
- 关联-association
- 集合-collection
- 所以association是用于一对一和多对一,而collection是用于一对多的关系
- JavaType和ofType都是用来指定对象类型的
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
- 注意说明:
- 保证SQL的可读性,尽量通俗易懂
- 根据实际要求,尽量编写性能更高的SQL语句
- 注意属性名和字段不一致的问题
- 注意一对多和多对一 中:字段和属性对应的问题
- 尽量使用Log4j,通过日志来查看自己的错误
标签:一对,处理,查询,嵌套,teacher,tid,Mybatis05,id,select 来源: https://blog.csdn.net/qq_39356280/article/details/111461383
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。