MyBatis中模糊查询

时间:2022-08-31 22:45:28

接口

//	模糊查询
List<User> getUserLike(String value);

Mapper.xml文件

  <!-- 模糊查询 -->
<select id="getUserLike" parameterType="String" resultType="com.perwrj.pojo.User">
select * from mybatis.user where name like #{value}
</select>

测试

/**
* 模糊查询
*/
@Test
public void getUserLike() {
SqlSession sqlSession = MyBatisUtil.getSqlSession(); try {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
String value = "%汪%";
List<User> list = userMapper.getUserLike(value);
for (User user : list) {
System.out.println(user);
} }catch(Throwable e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}

在模糊查询中的通配符

方式一:

			String value = "%汪%";

方式二:

    select * from mybatis.user where name like "%"#{value}"%"

注意:一定要避免出现sql注入问题