MyBatis 模糊查询 like

方法一

  • 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();
    }

注意:

  • 使用方法一更安全
posted @ 2023-08-16 16:15  MaoShine  阅读(58)  评论(0编辑  收藏  举报