zoukankan      html  css  js  c++  java
  • 动态SQL之模糊查询

    模糊查询学习了三种:

    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>

      

  • 相关阅读:
    内存分配略谈
    变量声明顺序和指针偏移问题示例
    解决You have to be inside an Angular CLI project in order to use the serve command
    Linux中将命令放到全局
    Linux设置vim显示行号
    根据指定的excel模板导出数据
    解决 Maven工程运行报错Failed to clean project: Failed to delete
    springcloud工作用常用到的一些注解
    cmd窗口备份mysql数据库
    调用存储过程msql
  • 原文地址:https://www.cnblogs.com/blogofcookie/p/5573560.html
Copyright © 2011-2022 走看看