多条件查询1

176 阅读1分钟
public interface UserMapper {
	/**
     * 多条件模糊查询
     * @param status        状态
     * @param BrandName     企业名称
     * @param companyName   公司名称
     * @return              存储多个Brand对象的List集合
     */
    /**
     * 条件查询
     *  * 参数接收
     *      1. 散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
     *      2. 对象参数:对象的属性名称要和参数占位符名称一致
     *      3. map集合参数: SQL参数占位符名称必须和键名一致
     *
     */
    //参数形式: 参数列表,散装参数
    List<Brand> selectByCondition(@Param("status") int status, @Param("brandName") String BrandName, @Param("companyName") String companyName);

    //参数形式: Map集合对象
    List<Brand> selectByCondition(Map map);

    //参数形式: Brand对象
    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();
    }

image.png

image.png

<select id="selectBrandByBrandName" resultMap="brandMap">
    select id, brand_name, company_name, ordered, description, status
    from tb_brand
    where brand_name = #{brandName}
</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 = #{status}
        </if>
        <if test="brandName !=null and brandName != ''">
            and brand_name like #{brandName}
        </if>
        <if test="companyName !=null and companyName != ''">
            and company_name like #{companyName}
        </if>
    </where>
</select>
''">