MyBatis多条件查询


  1. 查询-多条件 查询:

    1.编写接口方法:Mapper接口

    参数:所有查询条件

    结论:List

    1.创建BrandMapper类

    public interface BrandMapper {
    ?
    ?
    ?
        /**
         * 查询所有
         */
        List selectAll();
    ?
        /**
         * 查看详情:根据Id查询
         */
        Brand selectById(int id);
    ?
        /**
         * 条件查询
         *  *参数接收
         *      1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符")
         *      2.对象参数
         *      3.map集合参数
         *
    //     * @param status
    //     * @param companyName
    //     * @param brandName
         * @return
         */
        //List selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName") String brandName);
    ?
        //List selectByCondition(Brand brand);
    //
        List selectByCondition(Map map);
    }

    2.编写SQL语句:SQL映射文件

    创建BrandMapper.xml文件

    <?xml version="1.0" encoding="UTF-8" ?>
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    ?
    
    <mapper namespace="com.uestc.mapper.BrandMapper">
    ?
        
    ?
        
    ?
        <resultMap id="brandResultMap" type="brand">
            
            <result column="brand_name" property="brandName" />
            <result column="company_name" property="companyName" />
    ?
        resultMap>
    ?
            <select id="selectAll" resultMap="brandResultMap">
    ?
                select
                       *
                from tb_brand;
    ?
            select>
    ?
    ?
        
    
    
    
    ?
    
    ?
    
    
    
    ?
    
    ?
    
    ?
    
    
    ?
    
    ?
        
    
    
    
    
    ?
        <select id="selectById" resultMap="brandResultMap">
            select *
            from tb_brand where id
            
            <
            ]]>
            #{id};
        select>
    ?
        
        <select id="selectByCondition" resultMap="brandResultMap">
            select *
            from tb_brand
            where status = #{status}
                and company_name like #{companyName}
                and brand_name like #{brandName};
        select>
    ?
    mapper>

    3.执行方法,测试

    创建MyBatisTest类

     @Test
        public void testSelectByCondition() throws IOException {
    ?
            //接收参数
            int status = 1;
            String companyName = "华为";
            String brandName = "华为";
    ?
            //处理参数
            companyName = "%" + companyName + "%";
            brandName = "%" + brandName + "%";
    ?
            //封装对象
    //        Brand brand = new Brand();
    //        brand.setStatus(status);
    //        brand.setCompanyName(companyName);
    //        brand.setBrandName(brandName);
    ?
            //Map处理
            Map map = new HashMap();
            map.put("status",status);
            map.put("companyName",companyName);
            map.put("brandName",brandName);
    ?
            //1.获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    ?
            //2.获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
    ?
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    ?
            //4.执行方法
            //List brands = brandMapper.selectByCondition(status, companyName, brandName);
            //List brands = brandMapper.selectByCondition(brand);
            List brands = brandMapper.selectByCondition(map);
            System.out.println(brands);
    ?
            //5.释放资源
            sqlSession.close();
    ?
        }

    查询-多条件-动态条件查询:

    SQL语句会随着用户的输入或外部条件的变化而变化,我们称为动态SQL。

    MyBatis对动态SQL有很强大的支撑:

    if

    choose(when,otherwise)

    trim(where,set)

    foreach

    查询-单条件-动态条件查询:

    从多个条件中选择一个

    choose(when,where):选择,类似于Java中的switch语句

     

     

总结:SQL语句设置多个参数有几种方式?

(1)散装参数:需要使用@Param("SQL中的参数占位符名称")

(2)实体类封装参数:只需要保证SQL中的参数名和实体类属性名对应上,即可设置成功。

(3)map集合:只需要保证SQL中的参数名和map集合的键的名称对应上,即可设置成功。

MyBatis完成操作需要几步?三步:编写接口方法->编写SQL->执行方法