1. 程式人生 > 其它 >MyBatis動態查詢

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