前言
mybatis 中 association 和 collection 的 column 传入多个参数值,项目中在使用 association 和 collection 实现一对一和一对多关系时需要对关系中结果集进行筛选,如果使用懒加载模式,即联合使用 select 标签时,主 sql 和关系映射里的 sql 是分开的,查询参数传递成为问题。
mybatis文档描述
column 数据库的列名或者列标签别名。与传递给 resultSet.getString(columnName) 的参数名称相同。注意: 在处理组合键时,您可以使用 column="{id=id, lngId=lngId}" 这样的语法,设置多个列名传入到嵌套查询语句。这就会把 prop1 和 prop2 设置到目标嵌套选择语句的参数对象中。
<resultMap type="com.zres.product.resmaster.portal.homePage.entity.ResTreeDetailEntity" id="leftTreeFirstMaps"><id column="id" jdbcType="INTEGER" property="id"/><id column="lngId" jdbcType="INTEGER" property="lngId"/><collection property="children"ofType="com.zres.product.resmaster.portal.homePage.entity.ResTreeDetailEntity"select="queryLeftTreeById"column="{id=id, lngId=lngId}"></collection></resultMap><select id="queryLeftTreeById" parameterType="map" resultMap="leftTreeFirstMaps">SELECT prt.id,prt.tree_id as treeId,prt.parent_id as parentId,prt.res_type_id as resTypeId,#{lngId} as lngId,<choose><when test="lngId == -1">prt.res_type as resName,</when><otherwise>FUN_GET_LNG_DESC(#{lngId},prt.id,prt.res_type,'PUB_RES_TREE_DETAIL',prt.env_domain_id) as resName,</otherwise></choose>prt.icon as icon,prt.query_id as queryId,prt.role_id as roleIdFROM pub_res_tree_detail prtWHERE prt.is_display = 1AND prt.parent_id = #{id}AND prt.is_resource = 2</select>
来源:https://blog.csdn.net/yangbo787827967/article/details/86648878
