MyBatis中多条件查询-动态SQL语句查询

MyBatis中多条件查询-动态SQL语句查询_第1张图片
接口:

  /*动态多条件查询*/
    List<Brand> selectByCondition5(Brand brand);

Sql映射

<select id="selectByCondition5" resultType="com.itheima.popj.Brand">
        select *
        from tb_brand
        <where>
            <if test="status != null ">
                and status = #{status}
            if>
            <if test="companyName != null and companyName != '' ">
                and company_name like concat('%', #{companyName}, '%')
            if>
            <if test="brandName != null and brandName !='' ">
                and brand_name like concat('%', #{brandName}, '%')
            if>
        where>
    select>

Tset代码:

@Test
    public void selectByCondition5() {
        Brand brand = new Brand();
        brand.setStatus(0);
       /* brand.setBrandName("华");*/
        brand.setCompanyName("华");

        List<Brand> brands = mapper.selectByCondition5(brand);

        for (Brand brand1 : brands) {
            System.out.println(brand1);
        }
    }

你可能感兴趣的:(#,MyBatis,sql,java)