多条件查询

散装参数用注解的方式(@param(”参数名称”))

使用 @Param(“参数名称”) 标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位

在接口类中定义这样的方法
要求参数的名称和对应sql语句中参数的占位符名称一样

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

然后映射文件这样

  1. <!-- 条件查询-->
  2. <select id="selectByCondition" resultMap="brandResultMap">
  3. select *
  4. from tb_brand
  5. where status = #{status}
  6. and company_name like #{companyName}
  7. and brand_name like #{brandName};
  8. </select>

在测试代码中

  1. String companyName = "华为";
  2. String brandName = "华为";
  3. // 处理参数
  4. companyName = "%"+companyName+"%";
  5. brandName= "%"+brandName+"%";
  6. String resource = "mybatis-config.xml";
  7. InputStream inputStream = Resources.getResourceAsStream(resource);
  8. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  9. //获取SqlSession
  10. SqlSession sqlSession = sqlSessionFactory.openSession();
  11. //获取Mapper接口代理对象
  12. BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);
  13. List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
  14. System.out.println(brands);

多条件查询 - 图1

实体类封装参数

将多个参数封装成一个 实体对象 ,将该实体对象作为接口的方法参数。
该方式要求在映射配置文件的SQL中使用 #{内
容} 时,里面的内容必须和实体类属性名保持一致。

先写这个方法

  1. List<Brand> selectByCondition(Brand brand)

然后映射文件中的sql语句
这里 的sql语句不用变

  1. <select id="selectByCondition" resultMap="brandResultMap">
  2. select *
  3. from tb_brand
  4. where status = #{status}
  5. and company_name like #{companyName}
  6. and brand_name like #{brandName};
  7. </select>

然后测试类

  1. 模糊查询
  2. String companyName = "华为";
  3. String brandName = "华为";
  4. // 处理参数
  5. companyName = "%"+companyName+"%";
  6. brandName= "%"+brandName+"%";
  7. //将参数封装对象
  8. Brand brand = new Brand();
  9. brand.setStatus(status);
  10. brand.setCompanyName(companyName);
  11. brand.setBrandName(brandName);
  12. String resource = "mybatis-config.xml";
  13. InputStream inputStream = Resources.getResourceAsStream(resource);
  14. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  15. //获取SqlSession
  16. SqlSession sqlSession = sqlSessionFactory.openSession();
  17. //获取Mapper接口代理对象
  18. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  19. List<Brand> brands = brandMapper.selectByCondition(brand);//传入brand对象
  20. System.out.println(brands);

很明显我们这种方式采用的是对象也就是实体类封装的方式
多条件查询 - 图2

map集合

将多个参数封装到map集合中,将map集合作为接口的方法参数。
该方式要求在映射配置文件的SQL中使用 #{内容}
时,里面的内容必须和map集合中键的名称一致。

首先接口中的方法

  1. List<Brand> selectByCondition(Map map)

然后映射文件中的sql语句还是不需要更改
测试类中需要的

  1. int status = 1;
  2. // 模糊查询
  3. String companyName = "华为";
  4. String brandName = "华为";
  5. // 处理参数
  6. companyName = "%"+companyName+"%";
  7. brandName= "%"+brandName+"%";
  8. Map map = new HashMap<>();
  9. map.put("status",status);
  10. map.put("companyName",companyName);
  11. map.put("brandName",brandName);
  12. String resource = "mybatis-config.xml";
  13. InputStream inputStream = Resources.getResourceAsStream(resource);
  14. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  15. //获取SqlSession
  16. SqlSession sqlSession = sqlSessionFactory.openSession();
  17. //获取Mapper接口代理对象
  18. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
  19. List<Brand> brands = brandMapper.selectByCondition(map);
  20. System.out.println(brands);
  21. sqlSession.close();

多条件查询 - 图3
这就是实现多条件查询的几种方式。