本文介绍了使用mybatis实现过滤的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中包含一些字段和该表的某些搜索表单.我想添加使用此表单过滤表中项目的功能.为了实现这一点,我想从表中选择带有从搜索表单中获取的参数的

I've got a table with some fields and some search form for that table.I want to add ability to filter items in the table using this form.To implement this I want to do select from the table with parameters, acquired from the search form.

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

我不喜欢我的解决方案,因为如果最多有10个参数-查询将非常庞大...

I don't like my solution because if there up to 10 parameters - query will be huge...

<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>

推荐答案

好吧...我找不到比这更好的东西

Well...I was not able to find something better than

<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>

尽管如此,它还是比以前更好.

Though it's better than before anyway.

这篇关于使用mybatis实现过滤的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:28