mybatis三种方法实现多条件查询
1、散装参数如果方法有多个参数,则需要加@Param("SQL占位符名称")*3、map集合参数map中的键名称与SQL中的参数名称保持一致。*2、对象参数对象的属性名称要和参数占位符名称一致。
·
* 1、散装参数 如果方法有多个参数,则需要加@Param("SQL占位符名称") * 2、对象参数 对象的属性名称要和参数占位符名称一致 * 3、map集合参数 map中的键名称与SQL中的参数名称保持一致
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();
}
更多推荐
已为社区贡献1条内容
所有评论(0)