mybatis resultMap collection column 子查询用到父查询中的不存在的条件的传递参数问题
描述
标题描述的可能不够清晰,场景就是,在mybatis中,dao层传递了多个参数,有很多参数并不是父查询要是用的而是子查询要使用的,而且参数的类型可能为List或者String等,这个时候的解决方案。
思路和解决例子展示
我们可以在父查询时,将需要传递到子任务的查询参数,在父任务的查询结果中制造“虚拟列”,在父任务传递参数信息给子任务时,一并传递过去,如下代码:
父查询
<select id="getTeXingData" resultMap="texing">
SELECT jira_texing."id",jira_texing.texingname,
case when ('${versionname}' IS NULL) then NULL else '${versionname}' end as versionname,
case when ('${modulename}' IS NULL) then NULL else '${modulename}' end as modulename,
case when (${begindate} IS NULL) then NULL else ${begindate} end as begindate,
case when (${enddate} IS NULL) then NULL else ${enddate} end as enddate,
case when ('${projectname}' IS NULL) then NULL else '${projectname}' end as projectname
FROM jira_texing
WHERE 1= 1
<if test="texingname != null and texingname != ''">
AND jira_texing.texingname IN
<foreach collection="texingname.split(',')" item="item" index="index" separator="," open = "(" close = ")">
'${item}'
</foreach>
</if>
ORDER BY jira_texing.texingname ASC
</select>
其中versionname 、modulename、begindate、enddate、projectname 是子查询需要用到的参数,以上的case when 是以pgsql数据库为例,如果使用的是mysql 或者其他数据库类型,可以使用其他函数代替,如:IFNULL 等。
父查询对应的resultMap
<resultMap type="com.chanjet.tim.TplusTeXing.bean.TeXing" id="texing">
<id property="id" column="id"/>
<result property="texingname" column="texingname"/>
<collection property="funcionList" javaType="java.util.ArrayList"
column="{texingname=texingname,projectname=projectname,versionname=versionname, modulename=modulename}"
ofType="com.chanjet.tim.TplusTeXing.bean.FunctionPoint" select="getFunctionPointByTeXing">
</collection>
</resultMap>
使用column 进行虚拟列的数据传递,在子查询中进行接收即可
子查询
<select id="getFunctionPointByTeXing" resultMap="functionPoint">
SELECT jira_functionpoint."id",
jira_functionpoint.issueid ,
jira_functionpoint.issukey,
jira_functionpoint.summary,
jira_functionpoint.texingname,
jira_functionpoint.creatdate,
jira_functionpoint.closedate,
jira_functionpoint.statusname,
jira_functionpoint.satusid,
jira_functionpoint.projectname,
jira_functionpoint.versionname,
jira_functionpoint.modulename
FROM jira_functionpoint
WHERE 1 =1
AND jira_functionpoint.texingname = #{texingname}
<if test="projectname != null and projectname != ''">
AND jira_functionpoint.projectname IN
<foreach collection="projectname.split(',')" item="item" index="index" separator="," open = "(" close = ")">
'${item}'
</foreach>
</if>
<if test="versionname != null and versionname != ''">
AND jira_functionpoint.versionname IN
<foreach collection="versionname.split(',')" item="item" index="index" separator="," open = "(" close = ")">
'${item}'
</foreach>
</if>
<if test="modulename != null and modulename != ''">
AND jira_functionpoint.modulename IN
<foreach collection="modulename.split(',')" item="item" index="index" separator="," open = "(" close = ")">
'${item}'
</foreach>
</if>
ORDER BY jira_functionpoint.issueid ASC
</select>
注意
因为传递的参数,可能为List,String 类型等等,在传递过程中,直接将List这种数据传递会报错,因为在数据库中,没有和List想对应的数据类型,所以如果想传递这类数据的话,不妨在传递前,就先将List转为String类型,去除掉String两侧的[ ],然后在参数使用时进行切割遍历,这样就解决了 column 不能直接传递List的问题。
去除String两侧的[ ],可使用工具类StringUtils
import org.apache.commons.lang3.StringUtils;
如:StringUtils.strip(texingnames.toString(),"[]")
|