您的位置:首页 > 其它

电商平台学习笔记(六)——都是Boolean惹的祸

2016-04-03 08:31 405 查看
昨天遇到了一个Bug,得到的数据和我预期的数据刚好相反!后来才发现是因为没有注意boolean和Boolean两种数据类型的细微区别而引起的!下面我就直接上代码(用的是Mybatis框架),举例说明:
DAO层代码:

import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Component; import cn.ilxy.bean.product.Brand; import cn.ilxy.dao.SqlsessionFactoryDefinition; @Component("brandDaoImpl") /** * 商品DAO * @author: 张荣 * @date: 2016年3月22日 */ public class BrandDaoImpl extends SqlsessionFactoryDefinition implements BrandDao{ public List<Brand> getBrandListWithPage(Brand brand) { List<Brand> brandList = this.getSqlSession().selectList("cn.ilxy.dao.product.BrandDao.getBrandListWithPage", brand); return brandList == null ? new ArrayList<Brand>():brandList; } }

Mybatis映射文件:

<resultMap type="Brand" id="brand">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="description" jdbcType="VARCHAR" property="description" />
<result column="imgUrl" jdbcType="VARCHAR" property="img_url" />
<result column="sort" jdbcType="INTEGER" property="sort" />
<result column="isDisplay" jdbcType="BOOLEAN" property="is_display" />
</resultMap>

<!-- 查询品牌 -->
<select id="getBrandListWithPage" parameterType="Brand" resultMap="brand">
SELECT id, name , description, img_url, sort , is_display
FROM bbs_brand
<where>
<if test="isDisplay != null">
is_display=#{isDisplay}
</if>
<if test="name != null">
<!-- and 只能放在这个位置,where标签可以屏蔽掉 -->
AND name=#{name}
</if>
</where>
ORDER BY id DESC LIMIT 0,5
</select>


Junit测试代码:

import java.util.List; import javax.annotation.Resource; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.ilxy.bean.product.Brand; import cn.ilxy.dao.product.BrandDao; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application-context.xml") public class TestBrand { @Resource(name="brandDaoImpl") private BrandDao brandDao; public void testGetBrandListWithPage(){ Brand brand = new Brand(); brand.setPageNo(1); List<Brand> brandList = brandDao.getBrandListWithPage(brand); for (Brand tmp:brandList) { System.out.println(tmp.toString()); } } }

数据库表中数据:

数据库,Mabitis配置文件,DAO层,Junit测试代码如上!而最终的结果却令人大跌眼镜!原以为会得到所有的数据,但是最终结果却是除了取到了中间is_display属性为0的那一条数据外,没有任何其他数据! 后来经过查询百度,经过视频中主讲人的分析,终于明白了! 原因也是很简单。我在Mybatis配置文件中的这一句: <if test="isDisplay != null">
is_display=#{isDisplay}
</if> 是无效的!写和不写没有区别!因为我在定义实体Brand时,isDisplay时boolean类型的变量!而非Boolean类型!而在Test中定义了Brand变量brand,却没有给该字段赋值!所以该字段的值是boolean是其默认值:false,所以在数据库中查询的时候,当然只会取到is_display为0的呀! 解决方法:将boolean换成Boolean类型的!那么定义却没赋值时的值就会是null!那么也就会得到预期的结果啦! 总结:int与Integer、double和Double等也是这种类似情况,他们之间还是有细微差别的: 前者没有null,如果定义了(比如定义了int i,double j)等变量,那么i和j都不为空,都有默认值(i默认为0),这虽然是一个细节,但是当程序比较复杂的话,因此等事而引发空指针异常,你去排查的话,还是很麻烦的,凡事从细节处着手注意,在程序中,尽量还是使用基本数据类型(效率高,避免自动拆箱装箱)。而去避免此类问题。比如刚刚就遇到了这种错误,定义了boolean变量却没有赋值,故有默认值false,而在Mybatis中配置的是判断是否为null,如果使用了boolean,这种判断是无用的,因为boolean变量永远不可能为null(只有Boolean变量才有null)! 多么痛的领悟,以后多加注意呀!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: