ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MyBatis动态查询

2022-05-10 18:35:22  阅读:181  来源: 互联网

标签:status ndash companyName -- brand 查询 brandName MyBatis 动态


    1. 查询-多条件-动态条件查询:

      SQL语句会随着用户的输入或外部条件的变化而变化,我们称为动态SQL。

      MyBatis对动态SQL有很强大的支撑:

      if

      choose(when,otherwise)

      trim(where,set)

      foreach

      在BrandMapper.xml下面添加

      <!--
          动态条件查询
              * if:条件判断
                  * test:逻辑表达式
              * 问题:
                  * 恒等式
                  * <where> 替换 where 关键字
      -->
      ​
      <select id="selectByCondition" resultMap="brandResultMap">
          select *
          from tb_brand
          /* where 1 = 1 */
          <where>
                <if test="status != null">
                    and status = #{status}
                </if>
                <if test="companyName != null and companyName != '' ">
                    and company_name like #{companyName}
                </if>
                <if test="brandName != null and brandName != '' ">
                    and brand_name like #{brandName};
                </if>
          </where>
      ​
      </select>

       

      总结动态SQL:

      if:用于判断参数是否有值,使用test属性进行条件判断

      *存在问题:第一个条件不需要逻辑运算符

      解决方法:

      (1)使用恒等式让所有条件格式都一样

      (2)<where>标签替换关键字

      查询-单条件-动态条件查询:

      从多个条件中选择一个

      choose(when,where):选择,类似于Java中的switch语句,when相当于case语句,otherwisee相当于default语句

      在BrandMapper类里添加方法

      /**
       * 单条件动态查询
       * @param brand
       * @return
       */
      List<Brand> selectByConditionSingle(Brand brand);

       

      在BrandMapper.xml下面编写

      <!--    <select id="selectByConditionSingle" resultMap="brandResultMap">-->
      ​
      <!--        select *-->
      <!--        from tb_brand-->
      <!--        where-->
      <!--        <choose>&lt;!&ndash; 相当于switch &ndash;&gt;-->
      <!--            <when test="status != null">&lt;!&ndash; 相当于case &ndash;&gt;-->
      <!--                status = #{status}-->
      <!--            </when>-->
      ​
      <!--            <when test="companyName != null and companyName != '' ">&lt;!&ndash; 相当于case &ndash;&gt;-->
      <!--                company_name like #{companyName}-->
      <!--            </when>-->
      ​
      <!--            <when test="brandName != null and brandName != ''">&lt;!&ndash; 相当于case &ndash;&gt;-->
      <!--                brand_name like #{brandName};-->
      <!--            </when>-->
      <!--            <otherwise>-->
      <!--                1 = 1-->
      <!--            </otherwise>-->
      <!--        </choose>-->
      ​
      <!--    </select>-->
      ​
          <select id="selectByConditionSingle" resultMap="brandResultMap">
      ​
              select *
              from tb_brand
              <where>
              <choose><!-- 相当于switch -->
                  <when test="status != null"><!-- 相当于case -->
                      status = #{status}
                  </when>
      ​
                  <when test="companyName != null and companyName != '' "><!-- 相当于case -->
                      company_name like #{companyName}
                  </when>
      ​
                  <when test="brandName != null and brandName != ''"><!-- 相当于case -->
                      brand_name like #{brandName};
                  </when>
      ​
              </choose>
              </where>
          </select>

       

    在test类中添加代码

    @Test
    public void testSelectByConditionSingle() throws IOException {
    ​
        //接收参数
        int status = 1;
        String companyName = "华为";
        String brandName = "华为";
    ​
        //处理参数
        companyName = "%" + companyName + "%";
        brandName = "%" + brandName + "%";
    ​
        //封装对象
        Brand brand = new Brand();
        //brand.setStatus(status);
        brand.setCompanyName(companyName);
        //brand.setBrandName(brandName);
    ​
    ​
    ​
        //1.获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    ​
        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
    ​
        //3.获取Mapper接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    ​
        //4.执行方法
        //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
        //List<Brand> brands = brandMapper.selectByCondition(brand);
        List<Brand> brands = brandMapper.selectByConditionSingle(brand);
        System.out.println(brands);
    ​
        //5.释放资源
        sqlSession.close();
    ​
    }

     

 

标签:status,ndash,companyName,--,brand,查询,brandName,MyBatis,动态
来源: https://www.cnblogs.com/mhy123/p/16254763.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有