模糊查询学习了三种:
DAO层
// 可以使用
List<User> wherelike01(String user_name);
// 忘记
List<User> wherelike02(Map<String, Object> map);
// 推荐使用
List<User> wherelike03(String user_name)
测试类
@Test public void where语句测试01() { SqlSession sqlSession = null; try { sqlSession = MyBatisUtils.getSqlSession(); sqlSession.getMapper(UserDao.class).wherelike01("宋%"); } catch (Exception e) { logger.debug(e.getMessage()); throw new RuntimeException(e.getMessage()); } finally { MyBatisUtils.closeSqlSession(sqlSession); } } @Test public void where语句测试02$() { SqlSession sqlSession = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("account", "宋"); sqlSession = MyBatisUtils.getSqlSession(); sqlSession.getMapper(UserDao.class).wherelike02(map); } catch (Exception e) { logger.debug(e.getMessage()); throw new RuntimeException(e.getMessage()); } finally { MyBatisUtils.closeSqlSession(sqlSession); } } @Test public void where语句测试03推荐() { SqlSession sqlSession = null; try { sqlSession = MyBatisUtils.getSqlSession(); sqlSession.getMapper(UserDao.class).wherelike03("宋"); } catch (Exception e) { logger.debug(e.getMessage()); throw new RuntimeException(e.getMessage()); } finally { MyBatisUtils.closeSqlSession(sqlSession); } }
Mapper中
<mapper namespace="com.shxt.dao.UserDao">
<select id="wherelike01" parameterType="string" resultType="com.shxt.model.User">
SELECT
*
FROM
USER
WHERE
user_name LIKE #{user_name}
</select>
<select id="wherelike02" parameterType="map" resultType="com.shxt.model.User">
SELECT
*
FROM
USER
WHERE
user_name LIKE '${user_name}%'
</select>
<select id="wherelike03" parameterType="string" resultType="com.shxt.model.User">
SELECT
*
FROM
USER
WHERE
user_name LIKE CONCAT(#{user_name}, '%')
</select>
</mapper>