public interface UserMapper {
List<Brand> selectByCondition(@Param("status") int status, @Param("brandName") String BrandName, @Param("companyName") String companyName);
List<Brand> selectByCondition(Map map);
List<Brand> selectByCondition(Brand brand);
}
` <!--配置品牌查询结果映射关系-->
<resultMap id="brandMap" type="brand">
<id column="id" property="id"/>
<result column="brand_name" property="brandName"/>
<result column="company_name" property="companyName"/>
</resultMap>
<!--
多条件模糊查询
-->
<select id="selectByCondition" resultMap="brandMap">
select id, brand_name, company_name, ordered, description, status
from tb_brand
where
status = #{status}
and brand_name like #{brandName}
and company_name like #{companyName}
</select>``js
```js
```js
//测试多条件查询
@Test
public void testSelectByCondition() throws IOException {
//准备条件
int status = 1
String brandName = "%华为%"
String companyName = "%华为%"
//创建Brand对象
Brand brand = new Brand()
brand.setStatus(status)
brand.setBrandName(brandName)
brand.setCompanyName(companyName)
//创建Map集合对象
Map map = new HashMap()
map.put("status",status)
map.put("brandName",brandName)
map.put("companyName",companyName)
//1.使用Mybatis工具类获取SqlSession对象
SqlSession sqlSession = MybatisUtil.getSqlSession()
//2.使用SqlSession对象获取接口代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class)
//3.使用接口代理对象调用方法完成增删改查的操作,获取结果
//调用散装参数的方法
List<Brand> brandList = brandMapper.selectByCondition(status, brandName, companyName)
//调用Brand对象参数的方法
List<Brand> brandList = brandMapper.selectByCondition(brand)
//调用Map对象参数的方法
List<Brand> brandList = brandMapper.selectByCondition(map)
//4.处理结果数据
System.out.println(brandList)
//5.释放资源
sqlSession.close()
}


<select id="selectBrandByBrandName" resultMap="brandMap">
select id, brand_name, company_name, ordered, description, status
from tb_brand
where brand_name =
</select>
<!--
多条件模糊查询
动态sql拼接多条件查询的条件
if标签: 条件判断
test属性: 布尔表达式
问题:
where后面直接拼接了and导致sql报错
解决方案1: 不好 where后面拼接恒等式 1 = 1
解决方案2:
使用where标签
-->
<select id="selectByCondition" resultMap="brandMap">
select id, brand_name, company_name, ordered, description, status
from tb_brand
<where>
<if test="status != null">
status =
</if>
<if test="brandName !=null and brandName != ''">
and brand_name like
</if>
<if test="companyName !=null and companyName != ''">
and company_name like
</if>
</where>
</select>
''">