使用mybatis实现过滤的最佳方法

时间:2015-01-16 07:35:34

标签: mybatis

我有一张桌子,上面有一些字段和一些搜索表单。 我想添加使用此表单过滤表中项目的功能。 为了实现这一点,我想从表格中选择从搜索表单中获取的参数。

但问题是使用MyBatis及其XML映射器实现它的最佳方式是什么?

我不喜欢我的解决方案,因为如果有多达10个参数 - 查询将是巨大的......

<select id="getFilteredDevelopers" parameterType="map" resultMap="DeveloperResult">
    select
        developer_id,
        private_information
    from pmc.developer
    <choose>
        <when test="filterId != null and filterPrivateInformation == null">
            where developer_id like #{filterId}
        </when>
        <when test="filterId != null and filterPrivateInformation != null">
            where developer_id like #{filterId} and private_information like #{filterPrivateInformation}
        </when>
        <when test="filterId == null and filterPrivateInformation != null">
            where private_information like #{filterPrivateInformation}
        </when>
    </choose>
</select>

1 个答案:

答案 0 :(得分:1)

嗯......我找不到比

更好的东西
<select id="getFilteredProjects" resultMap="ProjectResult" parameterType="map">
    select
        project_id,
        project_name,
        project_owner
    from pmc.project
    where TRUE
    <choose>
        <when test="projectId != null">
            and project_id like #{projectId}
        </when>
        <when test="projectName != null">
            and project_name like #{projectName}
        </when>
        <when test="projectOwner != null">
            and project_owner like #{projectOwner}
        </when>
    </choose>
</select>

虽然它比以前更好。