Java Mybatis

在Mapper中判断字符串变量是否是预期值时,直接用 "==" 无法判断。

例如在下边例子中 enFlag == '0'.toString() 

<!-- 根据上级分类查询子项 -->
<select id="queryByParentId" parameterType="com.miselehe.article.bean.ArticleClassify" resultMap="baseMap">
	select <include refid="baseColumn" /> 
	from <include refid="table" /> 
	<where>
		parent_id = #{parentId,jdbcType=BIGINT} 
		<choose>
			<when test="enFlag != null and enFlag == '0'.toString()"> <!-- 查询包含禁用分类(全部) -->
			</when>
			<otherwise><!-- 默认查询启用状态下 -->
				and en_flag = '1' 
			</otherwise>
		</choose>
		and del_flag = '0' 
	</where>
	order by sort ASC
</select>
一般在select时,需要判断是否为 null 和 "", 但是日期类型不能增加 == '' 判断,否则无法得到预期值:

<if test="obj.statusFlag != null and obj.statusFlag != ''">
    and status_flag = #{obj.statusFlag,jdbcType=VARCHAR} 
</if>
<if test="startTime != null">
    and create_time >= #{startTime,jdbcType=TIMESTAMP} 
</if>
<if test="endTime != null">
    <![CDATA[ and create_time <= #{endTime,jdbcType=TIMESTAMP} ]]> 
</if>

上述例子中, startTime 不能增加  != '' 判断。


转载请指明出处!http://www.miselehe.com/article/view/33