StringBuilder的一些意识

本文详细介绍了在使用StringBuilder时应注意的关键点,并通过SQL语句实例展示了如何正确处理StringBuilder对象,避免常见的语法错误。

在使用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语句就是这样不支持的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值