码迷,mamicode.com
首页 > 数据库 > 详细

用 Form-Sql-Builder-mysql 将用户提交的表单自动转化成sql

时间:2014-10-13 10:05:29      阅读:245      评论:0      收藏:0      [点我收藏+]

标签: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,20
jack
ny
1
36
更具体的关于

参见github官网



用 Form-Sql-Builder-mysql 将用户提交的表单自动转化成sql

标签:style   http   color   io   ar   java   for   文件   sp   

原文地址:http://blog.csdn.net/nsrainbow/article/details/40039215

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!