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>

      

  • 相关阅读:
    zz[读书笔记]《Interpretable Machine Learning》
    Xgboost,LightGBM 和 CatBoost
    zz:Xgboost 导读和实战
    bzoj3252: 攻略 优先队列 并查集 贪心
    [BeiJing2009 WinterCamp]取石子游戏 Nim SG 函数
    Playing With Stones UVALive
    Division Game UVA
    [BJWC2011]禁忌 AC 自动机 概率与期望
    [Jsoi2010]连通数 bitset + Floyd
    洛谷P2197 nim游戏模板
  • 原文地址:https://www.cnblogs.com/blogofcookie/p/5573560.html
Copyright © 2011-2022 走看看