单条件查询

(1).编写接口方法Mapper接口   List<Brand> selectAllById();
        参数:三个
        返回值结果为:List<Brand>
(2).编写sql语句:sql映射文件
(3).执行方法,测试

(4).结果

1.编写接口方法Mapper接口   Brand selectAllById();

//2.查看详情,根据id来查找对象  where id = ?;
    public Brand selectAllById(int id);

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

参数占位符
    1.#{} 会将其转换成?,防止sql注入问题 和prepareStatement类似
    2.${} 拼sql,会存在sql注入问题
    3.使用时机:
        *参数传递的时候:#{}
        *表明或者列明不固定的情况下:${},但是也存在sql注入问题

特殊字符的处理   例如:select * from  tb_brand where id<${id};是不可以的
    1.转义字符  小于号的转义字符  &lt;   select * from  tb_brand where id &lt; ${id};
    2.CDATA区  在中间加入大写的CD确定 就可以正常写入小于号了

parameterType="integer"是参数类型,可以省略

<select id="selectAllById" parameterType="integer" resultMap="brandResultMap">
        select * from  tb_brand where id = ${id};
    </select>

3.执行方法,测试

/**
     *条件查询select * from tb_Brand where id = #{};
     * @throws IOException
     */
    @Test
    public void selectById()throws IOException {

//1.加载mybatis的核心配置文件,获取SQL session factory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
        SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
        BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
        //定义一个局部变量用来接受参数
        int id = 1;
        Brand brand = brandMapper.selectAllById(id);
        System.out.println(brand);
//5.释放资源
        SqlSession.close();
    }

多条件查询

(1).编写接口方法Mapper接口   List<Brand> selectAllById();
        参数:三个
        返回值结果为:List<Brand>
(2).编写sql语句:sql映射文件
(3).执行方法,测试

(4).结果

 1.编写接口方法Mapper接口

多条件查询
      1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
      2.对象作为参数 :对象属性名称要和参数占位符名称一致
      3.集合作为参数

(1).散装参数 :如果方法中有多个参数

List<Brand>selectByCondition
(@Param("status")int status, 
@Param("companyName")String companyName,
 @Param("brandName")String brandName);

(2).对象作为参数

List<Brand> selectByCondition(Brand brand);

(3).集合作为参数

List<Brand> selectByCondition(Map map);

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

多条件查询
      1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
      2.对象参数
      3.集合参数

(1).散装参数 :如果方法中有多个参数

<resultMap id="brandResultMap" type="com.itheima.pojo.Brand">
        <result column="brand_name" property="brandName"></result>
        <result column="company_name" property="companyName"></result>
    </resultMap>
 <!--条件查询-->
    <select id="selectByCondition" resultType="com.itheima.pojo.Brand" resultMap="brandResultMap">
        select * from tb_brand where
            status = #{status} and company_name like #{companyName} and brand_name like #{brandName};
    </select>

(2).对象作为参数

<resultMap id="brandResultMap" type="com.itheima.pojo.Brand">
        <result column="brand_name" property="brandName"></result>
        <result column="company_name" property="companyName"></result>
    </resultMap>
 <!--条件查询-->
    <select id="selectByCondition" resultType="com.itheima.pojo.Brand" resultMap="brandResultMap">
        select * from tb_brand where
            status = #{status} and company_name like #{companyName} and brand_name like #{brandName};
    </select>

(3).集合作为参数

<resultMap id="brandResultMap" type="com.itheima.pojo.Brand">
        <result column="brand_name" property="brandName"></result>
        <result column="company_name" property="companyName"></result>
    </resultMap>
 <!--条件查询-->
    <select id="selectByCondition" resultType="com.itheima.pojo.Brand" resultMap="brandResultMap">
        select * from tb_brand where
            status = #{status} and company_name like #{companyName} and brand_name like #{brandName};
    </select>

3.执行方法,测试

多条件查询
      1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
      2.对象参数
      3.集合参数

(1).散装参数 :如果方法中有多个参数

  //定义三个局部变量用来接受参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
        companyName = "%" + companyName+ "%";
        brandName = "%" + brandName+ "%";

/**
     *条件查询select * from tb_Brand where id = #{};
     * @throws IOException
     */
    @Test
    public void selectByCondition()throws IOException {

//1.加载mybatis的核心配置文件,获取SQL session factory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
        SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
        BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
        //定义三个局部变量用来接受参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
        companyName = "%" + companyName+ "%";
        brandName = "%" + brandName+ "%";

        List<Brand> brand = brandMapper.selectByCondition(status,companyName,brandName);
        System.out.println(brand);
//5.释放资源
        SqlSession.close();
    }

(2).对象作为参数

//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";

//封装成对象
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);

List<Brand> brands = brandMapper.selectByCondition(brand);
System.out.println(brands);

/**
     *条件查询select * from tb_Brand where id = #{};
     * @throws IOException
     */
    @Test
    public void selectByCondition()throws IOException {

//1.加载mybatis的核心配置文件,获取SQL session factory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
        SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
        BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
        //定义三个局部变量用来接受参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
        companyName = "%" + companyName+ "%";
        brandName = "%" + brandName+ "%";

        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);

        //List<Brand> brands = brandMapper.selectByCondition(status,companyName,brandName);
        List<Brand> brands = brandMapper.selectByCondition(brand);
        System.out.println(brands);
//5.释放资源
        SqlSession.close();
    }

(3).集合作为参数

//定义三个局部变量用来接受参数
int status = 1;
String companyName = "华为";
String brandName = "华为";
//处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
companyName = "%" + companyName+ "%";
brandName = "%" + brandName+ "%";
//封装集合
Map map = new HashMap();
map.put("status",status);
map.put("companyName",companyName);
map.put("brandName",brandName);

/**
     *条件查询select * from tb_Brand where id = #{};
     * @throws IOException
     */
    @Test
    public void selectByCondition()throws IOException {

//1.加载mybatis的核心配置文件,获取SQL session factory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);//把字符串传进来,转换成一个字节输入流
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SQL session的对象用它来执行sql
        SqlSession SqlSession = sqlSessionFactory.openSession();
//3.获取BrandMapper接口的代理对象
        BrandMapper brandMapper = SqlSession.getMapper(BrandMapper.class);
//4.执行方法
        //定义三个局部变量用来接受参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //处理参数>>>模糊表达式要用百分号或者下划线的占位符对参数进行处理
        companyName = "%" + companyName+ "%";
        brandName = "%" + brandName+ "%";
        //封装集合
        Map map = new HashMap();
        map.put("status",status);
        map.put("companyName",companyName);
        map.put("brandName",brandName);
      /*封装对象
        Brand brand = new Brand();
        brand.setStatus(status);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);*/

        //List<Brand> brands = brandMapper.selectByCondition(status,companyName,brandName);
        //List<Brand> brands = brandMapper.selectByCondition(brand);
        List<Brand> brands = brandMapper.selectByCondition(map);
        System.out.println(brands);
//5.释放资源
        SqlSession.close();
    }

4.执行结果

3.多条件查询
      1.散装参数 :如果方法中有多个参数,需要使用@param(“sql占位符的名称”)
      2.对象参数
      3.集合参数

(1).散装参数 :如果方法中有多个参数

java mybites 查询条件or java多条件查询sql_mybatis

(2).对象作为参数

java mybites 查询条件or java多条件查询sql_sql_02

(3).集合作为参数

 

java mybites 查询条件or java多条件查询sql_mybatis_03

总结:

此方式有一个bug,就是用户不会根据你有多少变量而去填写多少变量,如果用户只想填写一个变量呢?只填写了两个呢?,所查结果只能是空。