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();
}