2015-01-16 14 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

相關問題