有时候我们在查询数据库时,需要以查询结果为查询条件进行关联查询。
在mybatis 中通过 association 标签(一对一查询,collection 一对多 查询) 实现延迟加载子查询
column="{sourcetarget=id,sourcetype=cmdb_type}"/>
解释:
定义 xxxmap 继承 zzzzmap 类型为 xxxx.bean.xxx 实体。
然后通过 association 标签(一对一查询,collection 一对多 查询)。
select 子查询标签,值为查询的dao 层的方法。
column 为 传递到select 查询里的参数,sourcetarget=id (sourcetarget 为子查询方法的参数,id 为父查询的记录id 用于传递到子查询中作为sourcetarget 的值)
property=”destname” 将查询的结果映射到父查询的 resultmap 的 destname 属性
javatype=”java.lang.string” 查询结果的类型,通常是一个bean实体
子查询对应的dao层
string getbysource(@param("sourcetarget") int sourcetarget, @param("sourcetype") string sourcetype);
select name from xxx_relation
where source_target = #{sourcetarget} and source_type = #{sourcetype}
参考:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对萬仟网的支持。如果你想了解更多相关内容请查看下面相关链接
希望与广大网友互动??
点此进行留言吧!