2015-01-16 15 views
0

我有一张表格,其中包含一些字段以及该表格的一些搜索表单。 我想添加使用此表格筛选表格中项目的功能。 为了实现这一点,我想从表中选择从参数中获取的表格。使用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> 

回答

0

嗯...我没能找到的东西比

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

虽然它比以前更好。

+0

可能只是一个小的改变,但是,您可以用“”替换“Where TRUE”部分,然后在之后关闭标签。 – yalpertem

相关问题