mybatis全动态sql处理方式

对于项目中需要用到全动态拼接sql的场景时,可以用到mybatis的statementType属性

Demo(如果是非预编译的话,最好使用${}而不是#{}):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<select id="query" resultType="Map" statementType="STATEMENT">  
select * from ${tableName} t where
<foreach item="item" index="index" collection="field" open=" "
separator="and" close=" ">
<choose>
<when test="item.fieldType == 'DATE' and item.dateQueryFlag == 0">
${item.fieldCode} between
to_date('${item.fieldValue}','yyyy-mm-dd
hh24:mi:ss')
</when>
<when test="item.fieldType == 'DATE' and item.dateQueryFlag == 1">
to_date('${item.fieldValue}','yyyy-mm-dd
hh24:mi:ss')
</when>
<when test="item.fieldItemCode != null and item.fieldItemCode != ''">
${item.fieldCode} =
'${item.fieldItemCode}'
</when>
<otherwise>
${item.fieldCode} =
'${item.fieldValue}'
</otherwise>
</choose>
</foreach>

</select>