Python 官方文档:入门教程 => 点击学习
目录通过association对两表进行联表查询按照查询嵌套处理按照结果嵌套处理总结通过association对两表进行联表查询 student表属性如下 teacher表属性如下
student表属性如下
teacher表属性如下
当使用以下时,查询出来存在问题
<select id="getStudentTeacher" resultType="Student" >
select s.id,s.name,t.id, t.name from student s, teacher t where s.tid = t.id
</select>
思路:
利用结果集映射,联系起来
编写接口
// 查询所有的学生信息以及对应的老师信息
List<Student> getStudent();
编写mapper配置文件
<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 = #{nid}
</select>
测试得到结果
结果中嵌套了结果
一些注意问题:
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2" >
select s.id sid,s.name sname,t.id tid, t.name tname
from student s, 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="id" column="tid" />
<result property="name" column="tname" />
</association>
</resultMap>
查询出来的需要展现的结果都应该在resultMap中进行定义,否则会以默认值进行展示如下,在resultMap中注释掉tid和sname后,执行的结果
到此这篇关于mybatis中association基本使用的文章就介绍到这了,更多相关MyBatis association使用内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: MyBatis中association的基本使用方法
本文链接: https://www.lsjlt.com/news/168081.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0