您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
10-查询-动态条件查询(if、where、choose、when、otherwise标签)
发布时间:2024-11-21 16:50:12编辑:雪饮阅读()
-
if标签
上篇中根据status、brand_name,company_name来查询,但如果用户没有选择brand_name或没有选择company_name后则是查询不出结果的。
那么所以我们需要有条件的来判断某个字段是否有值才加入where条件里面。
所以select标签里面的statement的sql可以使用if标签去嵌套,如:
<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand
where
<if test="status != null">
status=#{status}
</if>
<if test=" companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test=" brandName != null and brandName != '' ">
and brand_name like #{brandName}
</if>
</select>
那么即便是这样,还有一种情况就是,如果用户连status都没有选择呢?
为了解决这种情况,我们有一种方案就是恒等式,至少有一个1=1这样的条件,不至于and之前是没有任何where条件的导致sql出错。所以其实无论是1=1,也可以2-2,只要恒等式即可,那么status前面也就需要and了。
所以此时sql将变如
<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand
where 1 = 1
<if test="status != null">
and status=#{status}
</if>
<if test=" companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test=" brandName!= null and brandName != '' ">
and brand_name like #{brandName}
</if>
</select>
where标签
除了1=1这种方式进行兜底,也可以使用where进行嵌套if标签,会自动处理兜底问题。
像是这样了
<select id="selectByCondition" resultMap="brandResultMap">
select * from tb_brand
<where>
<if test="status != null">
and status=#{status}
</if>
<if test=" companyName != null and companyName != '' ">
and company_name like #{companyName}
</if>
<if test=" brandName != null and brandName != '' ">
and brand_name like #{brandName}
</if>
</where>
</select>
单条件查询(choose、when、otherwise标签)
有时候就是比如那个字段有值就查那个字段,而并不是and多条件。
也可以用choose标签于when标签进行嵌套,类似switch与case一样,同样也有兜底解决方案otherwise标签。则实现如:
<select id="selectByConditionBySingle" resultMap="brandResultMap">
select * from tb_brand
where
<choose>
<when test="status != null">
status=#{status}
</when>
<when test="companyName != null and companyName != '' ">
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != '' ">
brand_name like #{brandName}
</when>
<otherwise>
1 = 1
</otherwise>
</choose>
</select>
接口的抽象方法实现如:
List<Brand> selectByConditionBySingle(Brand brand);
那么其测试方法如
@Test
public void test5() throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
BrandMapper brandMapper=sqlSession.getMapper(BrandMapper.class);
String brandName="松鼠";
Brand brand=new Brand();
brand.setBrandName("%"+brandName+"%");
List<Brand> brands=brandMapper.selectByConditionBySingle(brand);
System.out.println(brands);
sqlSession.close();
}
同样也支持where嵌套形式的兜底方案
<select id="selectByConditionBySingle" resultMap="brandResultMap">
select * from tb_brand
<where>
<choose>
<when test="status != null">
status=#{status}
</when>
<when test="companyName != null and companyName != '' ">
company_name like #{companyName}
</when>
<when test="brandName != null and brandName != '' ">
brand_name like #{brandName}
</when>
</choose>
</where>
</select>
关键字词:if,where,choose,when,otherwise
上一篇:09-查询-条件查询(散装参数,POJO对象参数,Map参数)
下一篇:11-添加&修改功能(insert、update标签、useGeneratedKeys、keyProperty属性、commit方法、openSession参数)