设为首页 加入收藏

TOP

用 Form-Sql-Builder-mysql 将用户提交的表单自动转化成sql
2015-07-20 17:30:01 来源: 作者: 【 】 浏览:2
Tags: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官网



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYoj-一种排序 下一篇NYoj-5个数求最值

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·About - Redis (2025-12-26 08:20:56)
·Redis: A Comprehens (2025-12-26 08:20:53)
·Redis - The Real-ti (2025-12-26 08:20:50)
·Bash 脚本教程——Li (2025-12-26 07:53:35)
·实战篇!Linux shell (2025-12-26 07:53:32)