程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 用 Form-Sql-Builder-mysql 將用戶提交的表單自動轉化成sql

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

編輯:C++入門知識

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


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

maven依賴


  org.crazycake
  form-sql-builder-mysql
  1.0.0-RELEASE

快速開始

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,20
jack
ny
1
36
更具體的關於

規則的寫法和可選值通配符的用法范圍查詢分組查詢添加limit添加排序IN查詢 參見github官網



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