电商平台学习笔记(六)——都是Boolean惹的祸
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)! 多么痛的领悟,以后多加注意呀!
- 电商平台学习笔记(八)——面向接口编程案例分析
- 电商平台学习笔记(七)——模板方法结合Spring在DAO层的应用
- 电商平台学习笔记(七)——模板方法结合Spring在DAO层的应用
- 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件
- cocos2d-x 3.1.1 step by step 学习笔记3 Win32平台下中文乱码问题
- GreenSock动画平台学习笔记(三)TimelineLite
- PHP学习笔记(一):开发微信平台
- windows平台下人工智能平台TensorFlow安装--(学习笔记)
- javascript学习笔记2-typeof、Number类型、Boolean()
- 浅谈MTK平台下Android开发比较学习笔记
- Java 学习笔记 ------第一章 Java平台概论
- 电商大数据学习笔记:用户画像
- 【VS开发】Windows平台下Makefile学习笔记
- Java学习笔记-《Java程序员面试宝典》-第四章基础知识-4.8 Java平台与内存管理(4.8.1-4.8.3)
- zynq平台开发学习笔记
- tinyos学习笔记3--基于cc2538平台的Blink、串口、timerTest例程测试
- AS3-学习笔记-flashdevelop平台-搭建-配置(转载加补充)
- 深入.NET平台和C#编程 第一章 学习笔记
- wpf学习笔记(3)《都是自己根据网络资源学习记录的仅供参考》
- java学习笔记1--开发环境平台总结