mybatis中association和collection的column传入多个参数值
项目中在使用association和collection实现一对一和一对多关系时需要对关系中结果集进行筛选,如果使用懒加载模式,即联合使用select标签时,主sql和关系映射里的sql是分开的,查询参数传递成为问题。
mybatis文档:
property description
column 数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的参数名称相同。注意: 在处理组合键时,您可以使用column=“{prop1=col1,prop2=col2}”这样的语法,设置多个列名传入到嵌套查询语句。这就会把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 roleId
FROM pub_res_tree_detail prt
WHERE prt.is_display = 1
AND prt.parent_id = #{id}
AND prt.is_resource = 2
</select>
以前使用的都是一个参数注入, 后来增加了国际化之后需要使用多个类型, 可参考此法!