mybatis三种方法实现多条件查询

Mapper设置

    <!-- 可加上判断形成动态SQL -->
    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where status = #{status}
          and company_name like #{companyName}
          and brand_name like #{brandName};
    </select>

散装参数

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

散装参数执行

@Test
    public void testSelectByCondition() throws IOException {
 
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //模糊查询 处理接受的数据
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";
 
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = 
        new SqlSessionFactoryBuilder().build(inputStream);
 
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
 
        //获取mapper接口的代理对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
 
        //mapper执行
        List<Brand> byCondition = mapper.selectByCondition(status, companyName, brandName);
        System.out.println(byCondition);
 
        //释放资源
        sqlSession.close();
}

对象参数

/**
  * @param brand
  * @return
  */
   List<Brand> selectByCondition(Brand brand);

对象参数执行

 @Test
    public void testSelectByCondition() throws IOException {
 
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //模糊查询 处理接受的数据
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";
 
        //封装Brand对象
        Brand brand = new Brand();
        brand.setStatus(1);
        brand.setCompanyName(companyName);
        brand.setBrandName(brandName);
 
         //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = 
        new SqlSessionFactoryBuilder().build(inputStream);
 
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
 
        //获取mapper接口的代理对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
 
        List<Brand> brands = mapper.selectByCondition(brand);
        System.out.println(brands);
 
        //释放资源
        sqlSession.close();
}

 map集合参数

 /**
   * @param map
   * @return
   */
  List<Brand> selectByCondition(Map map);

map集合参数执行

@Test
    public void testSelectByCondition() throws IOException {
 
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
        //模糊查询 处理接受的数据
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";
 
        //创建map对象
        Map map = new HashMap();
        map.put("status",status);
        map.put("companyName",companyName);
        map.put("brandName",brandName);
 
        
        //加载mybatis核心配置文件,获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = 
        new SqlSessionFactoryBuilder().build(inputStream);
 
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
 
        //获取mapper接口的代理对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
 
        List<Brand> list = mapper.selectByCondition(map);
        System.out.println(list);
 
        //释放资源
        sqlSession.close();
 
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值