Java学习之路

关于int与Integer的一个发现

2019-08-22  本文已影响115人  椰子奶糖

刚刚测试Sql的时候发现了一个小问题,具体测试代码如下:

@Test
    public void testCountByCondition() {
        FollowFans followFans = new FollowFans( );
        System.out.println(followFans);
        followFans.setFansId(1);
        System.out.println(followFansMapper.countByCondition(followFans));
    }
 <select id="countByCondition" resultType="java.lang.Integer">
        SELECT count(*) FROM follow_fans
        <where>
            <if test="followId!=null">
                follow_id=#{followId}
            </if>
            <if test="fansId!=null">
                AND fans_id=#{fansId}
            </if>
        </where>
    </select>

从测试类可以看到 我只加了一个值,另外的followId并没有加,而最终传的sql却是两个条件都有,神奇的是followId居然有个0值

==>  Preparing: SELECT count(*) FROM follow_fans WHERE follow_id=? AND fans_id=? 
==> Parameters: 0(Integer), 1(Integer)
<==    Columns: count(*)
<==        Row: 0
<==      Total: 1

开始分锅

followFans{id=0, followId=0, fansId=0}

int的默认值

没有默认值的Integer

小结

在数据库中int还是对应的Integer,但是Java默认可以用int代替Integer不知是方便编程者还是,再来添点乱哈哈
上一篇 下一篇

猜你喜欢

热点阅读