在使用StringBuilder时,应注意以下几点:
new出来的对象,当不追加任何内容时,是“”,不是null。这点在sql语句中尤其要注意,因为一些sql语句支持null,却不支持“”。例如
StringBuilder userIds = new StringBuilder();
if(usercIds != null && usercIds.size() > 0)
{
for(int t = 0; t < usercIds.size(); t++)
{
if(userIds == null || userIds.length() == 0)
{
userIds.append(usercIds.get(t));
}
else
{
userIds.append(",").append(usercIds.get(t));
}
}
}
else
{
userIds = null;
}
<select id="selectMemberCsShowInCenter" parameterType="map" resultType="com.lietou.ltc.dao.entity.User_c" >
select SQL_CALC_FOUND_ROWS a.* from ${db_user}.user_c as a
<where>
<if test="c_kind != null">
and a.c_kind=#{c_kind}
</if>
<if test="h_industry != null">
and a.c_industry in (${h_industry})
</if>
<if test="h_jobtitle != null">
and a.c_jobtitle in (${h_jobtitle})
</if>
<if test="userc_ids != null">
and a.user_id in (${userc_ids})
</if>
</where>
order by a.c_modifytime desc limit #{limit_cnt}
<!-- limit #{start}, #{pagesize} -->
</select>
注意上面 sql语句中的条件:in时
sql语句支持。。
where user_id in (1, "", null);
但是不支持where user_id in(1, , "");而StringBuilder不追加内容,直接传进来解析成sql语句就是这样不支持的。
本文详细介绍了在使用StringBuilder时应注意的关键点,并通过SQL语句实例展示了如何正确处理StringBuilder对象,避免常见的语法错误。
396

被折叠的 条评论
为什么被折叠?



