方法一:使用“trim”標簽。
<select id="selTest" parameterType="mocha.framework.entiey.SnDocCountDef" resultMap="BaseResultMap"> select <trim suffixOverrides="," > <if test="bodefId != null" > bodef_id, </if> <if test="bodefName != null" > bodef_name, </if> <if test="bodefType != null" > bodef_type, </if> <if test="addTime != null" > add_time, </if> <if test="extend1 != null" > extend1, </if> </trim> from sn_doc_count_def </select>
此時,如果給“if”后面對應的參數傳入值,在查詢時 就會添加上對應的字段。
方法二:使用“${}”傳入參數。
<select id="selTest" parameterType="mocha.framework.entiey.SnDocCountDef" resultMap="BaseResultMap">
select ${sql} from sn_doc_count_def
</select>
在${sql}處傳入需要查詢的字段,即可實現動態查詢字段的sql。