写法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'}]