条件查询:用户数据某几项信息,通过这些信息模糊查询用户需要的数据
编写条件查询的sql
< select id = " selectByCondition" resultMap = " brandResultMap" >
select *
from tb_brand
where status = #{status}
and company_name like #{companyName}
and brand_name like #{brandName}
</ select>
编写接口
List < Brand > selectByCondition ( Map map) ;
测试类
@Test
public void testSelectCondition ( ) throws IOException {
String resource = "mybatis-config" ;
InputStream inputStream = Resources . getResourceAsStream ( resource) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( ) ;
BrandMapper mapper = sqlSession. getMapper ( BrandMapper . class ) ;
int status = 1 ;
String companyName = "华为" ;
String brandName = "华为" ;
companyName = "%" + companyName + "%" ;
brandName = "%" + brandName + "%" ;
HashMap hashMap = new HashMap ( ) ;
hashMap. put ( "status" , status) ;
hashMap. put ( "companyName" , companyName) ;
hashMap. put ( "brandName" , brandName) ;
List < Brand > brands = mapper. selectByCondition ( hashMap) ;
System . out. println ( brands) ;
sqlSession. close ( ) ;
}
运行失败
解决数据库和idea之间的编码不一致导致无法输出或报错的问题