【MyBatis】学习笔记07:模糊查询
【MyBatis】学习笔记03:配置文件进一步解读(非常重要)
【MyBatis】学习笔记06:各种查询所返回数据的数据类型
写法1:$
/**
* 根据用户名模糊查询
* @param username 用户名的关键字
* @return List<User> 用户列表
*/
List<User> getUserByLike(@Param("username") String username);
<!-- List<User> getUserByLike(@Param("username") String username);-->
<select id="getUserByLike" resultType="User">
select * from t_users where userName like "%${username}%"
</select>
@Test
public void testGetUserByLike(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
List<User> list = mapper.getUserByLike("三");
System.out.println(list);
}
[User{userId='5',userName='张三'userCount='mllt@xrilang.com',userPassword='123'userGrade='0',userRegDate='null'userBlance='0'}, User{userId='6',userName='李三'userCount='xirlang@mllt.c',userPassword='124'userGrade='0',userRegDate='null'userBlance='0'}]
写法2:concat拼接
<!-- List<User> getUserByLike(@Param("username") String username);-->
<select id="getUserByLike" resultType="User">
<!-- select * from t_users where userName like "%${username}%"-->
select * from t_users where userName like concat("%",#{username},"%")
</select>
<!--concat 拼接-->
[User{userId='5',userName='张三'userCount='mllt@xrilang.com',userPassword='123'userGrade='0',userRegDate='null'userBlance='0'}, User{userId='6',userName='李三'userCount='xirlang@mllt.c',userPassword='124'userGrade='0',userRegDate='null'userBlance='0'}]
写法3:#(推荐使用)
<!-- List<User> getUserByLike(@Param("username") String username);-->
<select id="getUserByLike" resultType="User">
<!-- select * from t_users where userName like "%${username}%"-->
<!-- select * from t_users where userName like concat("%",#{username},"%")-->
select * from t_users where userName like "%"#{username}"%"
</select>
[User{userId='5',userName='张三'userCount='mllt@xrilang.com',userPassword='123'userGrade='0',userRegDate='null'userBlance='0'}, User{userId='6',userName='李三'userCount='xirlang@mllt.c',userPassword='124'userGrade='0',userRegDate='null'userBlance='0'}]
版 权 声 明