电商平台学习笔记(六)——都是Boolean惹的祸
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>
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()); } } }
数据库表中数据:is_display=#{isDisplay}
</if>