BrandMapper
接口:
List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName,@Param("brandName") String brandName);
BrandMapper.xml
映射配置文件:
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
where status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</select>
test/java
下的 com.itheima.mapper
包下的 MybatisTest类中
定义测试方法:
@Test
public void testSelectByCondition() throws IOException {
//1. 获取SqlSessionFactory
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2. 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3. 获取Mapper接口的代理对象
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
//4. 执行方法
List<Brand> brands = brandMapper.selectByCondition();
System.out.println(brands);
//5. 释放资源
sqlSession.close();
}
- 模糊查询格式:
SELECT * FROM tb_brand where company_name like '%华为%';
- 结论
BrandMapper.xml
映射配置文件中:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)