用 Form-Sql-Builder-mysql 將使用者提交的表單自動轉化成sql

來源:互聯網
上載者:User

標籤:style   http   color   io   ar   java   for   檔案   sp   

用 Form-Sql-Builder-mysql 將使用者提交的表單自動轉化成sql

maven依賴

<dependency>  <groupId>org.crazycake</groupId>  <artifactId>form-sql-builder-mysql</artifactId>  <version>1.0.0-RELEASE</version></dependency>

快速開始

STEP 1. 建立一個規則檔案

在classpath下建立一個檔案夾叫 formSqlRules ,在這個檔案夾下建立global.json

{"global":[{"field":"String:*","op":"like","rel":"and"},{"field":"*:*","op":"=","rel":"and"}]}
STEP 2. 建立一個測試用PO類

public class Person {private Integer activeStatus;private String name;private Integer age;private String city;public Person(String name, Integer age, String city, Integer activeStatus){this.name = name;this.age = age;this.city = city;this.activeStatus = activeStatus;}public Integer getActiveStatus() {return activeStatus;}public void setActiveStatus(Integer activeStatus) {this.activeStatus = activeStatus;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}}

STEP 3. 調用 FormSqlBuilder

Person form = new Person("jack", 36, "ny", 1);FormSqlBuilder b = new FormSqlBuilder(form, "global");b.addLimit(1, 20);SqlAndParams s = b.build();System.out.println(s.getSql());for(Object v:s.getParams()){System.out.println(v);}
控制台輸出

SELECT * FROM person WHERE name like ? AND city like ? AND active_status = ? AND age = ?  LIMIT 0,20jackny136
更具體的關於

  • 規則的寫法和可選值
  • 萬用字元的用法
  • 範圍查詢
  • 分組查詢
  • 添加limit
  • 添加排序
  • IN查詢
參見github官網



用 Form-Sql-Builder-mysql 將使用者提交的表單自動轉化成sql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.