方法一
- Dao.java
// 模糊查询
List<User> getUserLike(String str);
- mapper
<select id="getUserLike" resultType="com.mao.pojo.User" parameterType="String">
select * from test.user where name like "%"#{str}"%"
</select>
- test
@Test
public void testGetUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> us = mapper.getUserLike("自定义");
for (User u : us) {
System.out.println(u.toString());
}
sqlSession.close();
}
方法二
- mapper
<select id="getUserLike" resultType="com.mao.pojo.User" parameterType="String">
select * from test.user where name like #{str}
</select>
- test
@Test
public void testGetUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> us = mapper.getUserLike("%自定义%");
for (User u : us) {
System.out.println(u.toString());
}
sqlSession.close();
}
注意:
- 使用方法一更安全