程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Mybatis 條件查詢 批量增刪改查功能

Mybatis 條件查詢 批量增刪改查功能

編輯:關於JAVA

模糊查詢:

@Select({
    "SELECT * FROM account where account like CONCAT('%',#{query},'%') or email like CONCAT('%',#{query},'%')"
})
Account findAccountByAccountOrMail(@Param("query") String query);

批量添加:

@Insert({
    "<script>" +
        "INSERT INTO company_label(company_id,label_id) values " +
        " <foreach collection=\"item\" item=\"item\" index=\"index\" separator=\",\" > " +
        "    (#{companyId},#{item}) " +
        "  </foreach>" +
        "</script>"
})
void insertLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);

批量刪除:

@Delete({
    "<script>delete from company_label where company_id = #{companyId} and label_id in " +
        "<foreach collection = \"item\" item = \"item\" open=\"(\" separator=\",\" close=\")\">" +
        "#{item}" +
        "</foreach>" +
        "</script>"
})
void removeLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);

批量修改:

@Update(value = "<script>" + "update banner b set b.display = #{status} where b.id in "+
    "<foreach item = 'item' index = 'index' collection = 'ids' open = '(' separator = ',' close = ')'>#{item}</foreach>" +
    "" +
    "</script>")
int updateStatus(@Param("status") Long status, @Param("ids") Long[] ids);

批量查詢:

@Select({
    "<script>" +
        "select * from product where id in" +
        "<foreach item = 'item' index = 'index' collection = 'idList' open = '(' separator = ',' close = ')'>#{item}</foreach>" +
        "</script>"
})
List<Product> findByIdList(@Param("idList")List<Long> idList);

條件查詢,if裡面不僅可以判空,還可以判斷是否滿足某個條件

@Select({
      "<script>SELECT * FROM company where 1=1 and parent_id = #{companyId} " +
          //平級
          "<if test = \"isScanSameLevelValue == 1\">and type = #{type}</if>" +
           "<if test = \"isScanSameLevelValue == 0\">and type != #{type}</if>" +

          "</script> "
  })
  List<Company> findCompanyConditional(@Param("isScanSameLevelValue") String isScanSameLevelValue, @Param("isScanParentLevelValue") String isScanParentLevelValue, @Param("companyId") Long companyId, @Param("type") Integer type);

條件查詢:

 */
@Lang(XMLLanguageDriver.class)
@Select({"<script>select DISTINCT p.* FROM `us_product`.`hot_category_surgery` hcs "+
    "LEFT JOIN `us_product`.`product` p ON hcs.`product_id` =p.`id`"+
    "LEFT JOIN `us_product`.`category_surgery` cs on cs.`product_id` =p.`id`"+
    "LEFT JOIN `us_product`.`merchant_product` mp on mp.`product_id` = p.`id`"+
    "LEFT JOIN `us_product`.`org_product` op on op.`product_id` =p.`id`"+
    "where p.`type` =1 and p.`is_for_sale` =1 "+
    "        <if test=\"hId != null\"> and hcs.hot_category_id = #{hId} and p.id = hcs.product_id</if>" + //熱門類目id
    "        <if test=\"categoryId != null\"> and cs.category_id = #{categoryId} and p.id = cs.product_id</if>" + //類目id
    "        <if test=\"input != null\">    and (p.name like CONCAT('%',#{input},'%') or p.company like CONCAT('%',#{input},'%')) </if> "+  //用戶輸入,包括商品名和店鋪名,模糊
    "        <if test = \" location != null\"> and p.location like CONCAT('%',#{location},'%') </if> "+    //位置..
    "        <if test=\"method != null\">   and mp.filter_id = #{method} and p.id = mp.product_id</if> "+  //篩選條件  手術方式
    "        <if test=\"org != null\">     and op.filter_id = #{org} and p.id = op.product_id</if> "+   //篩選條件  所屬機構
    "         ORDER BY sale_volume DESC"+
    "        </script>"
})
List<Product> findProductFromLocal(@Param("hId")Long hId,@Param("categoryId")Long categoryId,@Param("input")String input,@Param("method")Long method,@Param("org")Long org,@Param("location")String location);

以上所述是小編給大家介紹的Mybatis 條件查詢 批量增刪改查功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved