亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java基于用戶搜索的動態where子句

java基于用戶搜索的動態where子句

慕少森 2022-01-12 16:43:33
您好我需要根據用戶選擇動態構建查詢。我不能用 if 語句寫 where 條件,因為我在 table 中有 50 列。有沒有 if 語句的方法?
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

你的意思是如何避免sql注入,比如在運行時連接sql語句?


    static final String INVOICE_QUERY = "SELECT inv from Invoice inv WHERE (:createDate IS NULL OR inv.createDate = :createDate) AND (:quantity IS NULL OR inv.quantity = :quantity) AND (:custName IS NULL OR inv.custName = :custName)";

    Session session = getHibernateTemplate().getSessionFactory().openSession();


    Query query = session.createQuery(INVOICE_QUERY);

    query.setDate("createDate", createDate);

    if(quantity != null)

        query.setLong("quantity", quantity);

    else

        query.setBigInteger("quantity", null);

    if(StringUtils.isNotBlank(custName))

        query.setString("custName", custName);

    else

        query.setString("custName", null);

    return query.list();


查看完整回答
反對 回復 2022-01-12
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

您可以查看 CrtitriaAPI 或者您可以通過示例使用休眠功能查詢

實際上,如果您使用帶有彈簧數據模塊的彈簧,您可以查看規格

請查看以下鏈接:

https://dzone.com/articles/hibernate-query-example-qbe https://vladmihalcea.com/query-entity-type-jpa-criteria-api/ https://spring.io/blog/2011/04 /26/advanced-spring-data-jpa-specifications-and-querydsl/


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號