HibernateCRUD基础框架包括3篇文章,主要讲述整个CRUD基础框架的思路。
第1篇:讲述最基本的实体类,这些实体类是对SQL语言中的一些概念的封装。
第2篇:在这些实体类的基础上,开发一个“HQL语句构造器-HqlQueryBuilder”。
第3篇:构建一套简单的和较为复杂的标准的CRUD API。
优点:提高代码复用,提升编码效率。
缺点:没有过多考虑性能,具体性能没有去检验。
功能有限,只是针对常用的和较为简单的CRUD功能。
友情提示:注释已经写得比较清楚了,不再过多解释。
有问题,可以留言,抽空答复。
第1篇:实体类
1.常量和基础定义
package cn.fansunion.hibernate.sql;
/**
* SQL关键字常量“and,like,where”,比较符常量">,<,="等常量。
*
* @author LeiWen@FansUnion.cn
*/
public class ConstantBase {
/**
* SQL关键字or
*/
public static final String OR = "or";
/**
* SQL关键字and
*/
public static final String AND = "and";
/**
* SQL关键字from
*/
public static final String FROM = "from";
/**
* SQL关键字as
*/
public static final String AS = "as";
/**
* SQL关键字where
*/
public static final String WHERE = "where";
/**
* SQL关键字asc
*/
public static final String ASC = "asc";
/**
* SQL关键字desc
*/
public static final String DESC = "desc";
/**
* SQL关键字in
*/
public static final String IN = "in";
/**
* not in
*/
public static final String NOT_IN = "not in";
/**
* SQL关键字like
*/
public static final String LIKE = "like";
/**
* not like
*/
public static final String NOT_LIKE = "not like";
/**
* order by
*/
public static final String ORDER_BY = "order by";
/**
* group by
*/
public static final String GROUP_BY = "group by";
/**
* SQL关键字limit
*/
public static final String LIMIT = "limit";
/**
* 冒号
*/
public static final String COLON = ":";
/**
* 逗号
*/
public static final String COMMA = ",";
/**
* 一个空格
*/
public static final String BLANK = " ";
/**
* 一个空字符串
*/
public static final String EMPTY = "";
/**
*
*/
public static final boolean AUTO_ADD = true;
/**
* 右括号
*/
public static final String RIGHT_BRACKET = ")";
/**
* 左括号
*/
public static final String LEFT_BRACKET = "(";
/**
* 百分号
*/
public static final String PERCENT = "%";
/**
* 单引号
*/
public static final String SINGLE_QUOTATION_MARK = "'";
/**
* 等号
*/
public static final String EQUALS = "=";
/**
* 不等
*/
public static final String NOT_EQUALS = "!=";
/**
* 大于号
*/
public static final String GREAT_THAN = ">";
/**
* 小于号
*/
public static final String LESS_THAN = "<";
/**
* 大于等于
*/
public static final String GREAT_THAN_EQUALS = ">=";
/**
* 小于等于
*/
public static final String LESS_THAN_EQUALS = "<=";
// *************************************
// **********左右分别加1个空格的常量*****************
// *************************************
public static final String EQUALS_WITH_BLANK = buildBlank(EQUALS);
public static final String NOT_EQUALS_WITH_BLANK = buildBlank(NOT_EQUALS);
public static final String GREAT_THAN_WITH_BLANK = buildBlank(GREAT_THAN);
public static final String LESS_THAN_WITH_BLANK = buildBlank(LESS_THAN);
public static final String GREAT_THAN_EQUALS_WITH_BLANK = buildBlank(GREAT_THAN_EQUALS);
public static final String LESS_THAN_EQUALS_WITH_BLANK = buildBlank(LESS_THAN_EQUALS);
public static final String IN_WITH_BLANK = buildBlank(IN);
public static final String NOT_IN_WITH_BLANK = buildBlank(NOT_IN);
public static final String LIKE_WITH_BLANK = buildBlank(LIKE);
public static final String NOT_LIKE_WITH_BLANK = buildBlank(NOT_LIKE);
public static final String ORDER_BY_WITH_BLANK = buildBlank(ORDER_BY);
public static final String GROUP_BY_WITH_BLANK = buildBlank(GROUP_BY);
public static final String LIMIT_WITH_BLANK = buildBlank(LIMIT);
public static final String WHERE_WITH_BLANK = buildBlank(WHERE);
public static final String AS_WITH_BLANK = buildBlank(AS);
/**
* 返回变量对应的字符串值
*
* @param or
* @return true返回“or”,false返回"and"
*/
public static String isOr(Boolean or) {
String str = AND;
if (or) {
str = OR;
}
return str;
}
/**
* 在字符串的左边加上百分号"%",在字符串的右边加上百分号"%"
*
* @param str
* 字符串
* @return 被"%%"包围起来的新的字符串
*/
public static String buildLike(Object str) {
String newStr = PERCENT + str + PERCENT;
return newStr;
}
/**
* 在一个字符串的左边和右边都加上一个空格
*
* @param str
* @return 新的字符串
*/
public static String buildBlank(Object str) {
String newStr = BLANK + str + BLANK;
return newStr;
}
/**
* 在字符串的左边和右边加上单引号"'"
*
* @param str
* 字符串
* @return 被"''"包围起来的新的字符串
*/
public static String buildQuota(Object str) {
String newStr = SINGLE_QUOTATION_MARK + str + SINGLE_QUOTATION_MARK;
return newStr;
}
/**
* 在字符串的左边加上左括号"(",在字符串的右边加上右括号")"
*
* @param str
* 字符串
* @return 被"()"包围起来的新的字符串
*/
public static String buildBracket(Object str) {
String newStr = LEFT_BRACKET + str + RIGHT_BRACKET;
return newStr;
}
public static void println(Object object) {
System.out.println(object);
}
public static void print(Object object) {
System.out.print(object);
}
}
package cn.fansunion.hibernate.sql;
/**
* 操作符的类型,如"= != > >= < <="。
*
* @author LeiWen@FansUnion.cn
*/
public enum Operator {
EQUALS, NOT_EQUALS, GREAT_THAN, GREAT_THAN_EQUALS, LESS_THAN, LESS_THAN_EQUALS, LIKE, NOT_LIKE, IN, NOT_IN;
/**
* 转化为字符串(TODO 放在一个单独的工具类里比较合适)
*/
public static String toString(Operator operator) {
String str = "";
switch (operator) {
case EQUALS:
str = ConstantBase.EQUALS;
break;
case NOT_EQUALS:
str = ConstantBase.NOT_EQUALS;
break;
case GREAT_THAN:
str = ConstantBase.GREAT_THAN;
break;
case GREAT_THAN_EQUALS:
str = ConstantBase.GREAT_THAN_EQUALS;
break;
case LESS_THAN:
str = ConstantBase.LESS_THAN;
break;
case LESS_THAN_EQUALS:
str = ConstantBase.LESS_THAN_EQUALS;
break;
case LIKE:
str = ConstantBase.LIKE;
break;
case NOT_LIKE:
str = ConstantBase.NOT_LIKE;
break;
case IN:
str = ConstantBase.IN;
break;
case NOT_IN:
str = ConstantBase.NOT_IN;
break;
default:
break;
}
return str;
}
}
public enum AndOr {
AND, OR
}
2.实体类
package cn.fansunion.hibernate.sql.entity;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;
import cn.fansunion.hibernate.sql.ConstantBase;
/**
* From语句,如"from User user"。
*
* @author LeiWen@FansUnion.cn
*/
public class From extends ConstantBase {
/**
* 实体类的class,如User.class
*/
private Class
modelClazz;
/**
* 实体类Class的字符串表示,如User
*/
private String model;
/**
* 别名,如user
*/
private String alias;
public From() {
super();
}
public From(Class
modelClazz) {
super();
this.modelClazz = modelClazz;
}
public From(Class
modelClazz, String alias) {
super();
this.modelClazz = modelClazz;
this.alias = alias;
}
public From(String model) {
super();
this.model = model;
}
public From(String model, String alias) {
super();
this.model = model;
this.alias = alias;
}
//
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Class
getModelClazz() {
return modelClazz;
}
public void setModelClazz(Class
modelClazz) {
this.modelClazz = modelClazz;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
/**
* 转化为字符串
*/
public String toString() {
if (modelClazz != null) {
this.model = modelClazz.getSimpleName();
}
StrBuilder builder = new StrBuilder();
if (StringUtils.isNotEmpty(model)) {
builder.append(FROM).append(BLANK)
.append(modelClazz.getSimpleName());
if (StringUtils.isNotEmpty(alias)) {
builder.append(AS_WITH_BLANK).append(alias);
}
}
return builder.toString();
}
}
package cn.fansunion.hibernate.sql.entity;
import java.util.Date;
import cn.fansunion.hibernate.sql.ConstantBase;
import cn.fansunion.hibernate.sql.Operator;
/**
* 1个查询条件。由3部分构成,键-操作-值,如 "age > 12 ","email ='LeiWen@FansUnion.cn'"等。
*
* @author LeiWen@FansUnion.cn
*/
public class Condition extends ConstantBase {
/**
* 条件的键
*/
private String key;
/**
* 条件的操作符
*/
private Operator operator;
/**
* 条件的值
*/
private Object value;
public Condition() {
}
public Condition(String key, Operator operator) {
super();
this.key = key;
this.operator = operator;
}
public Condition(String key, Object value) {
this.key = key;
this.value = value;
}
public Condition(String key, Operator operator, Object value) {
this.key = key;
this.operator = operator;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
/**
* 转化为字符串
*/
public String toString() {
String str = "";
String newValue = "";
// 构造完整的语句
if (value != null) {
// 是否需要自动增加"%%","()"
if (AUTO_ADD) {
switch (operator) {
case LIKE:
case NOT_LIKE:
newValue = buildLike(value);
break;
case IN:
case NOT_IN:
newValue = buildBracket(value);
break;
default:
break;
}
// 需要添加引号的类型
boolean isString = value instanceof String;
boolean isDate = value instanceof Date;
boolean isSqlDate = value instanceof java.sql.Date;
if (isString || isDate || isSqlDate) {
newValue = buildQuota(value);
} else {
newValue = value.toString();
}
}
}
// 构造带有占位符的语句
else {
newValue = COLON + key;
}
// "name=a","name like a",统一都加个空格
str += key + BLANK + Operator.toString(operator) + BLANK + newValue;
return str;
}
}
package cn.fansunion.hibernate.sql.entity;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.text.StrBuilder;
import cn.fansunion.hibernate.AndOr;
import cn.fansunion.hibernate.sql.ConstantBase;
import cn.fansunion.hibernate.util.EmptyUtils;
/**
* 逻辑上属于1个组的条件。
*
* @author LeiWen@FansUnion.cn
*/
public class GroupCondition extends ConstantBase {
/**
* 多个条件构成的集合
*/
private List
conditionList;
/**
* 组内各个条件之间的关系:且|或,默认为且
*/
private List
relationList; public static final Boolean AND = true; public static final Boolean OR = false; public GroupCondition() { conditionList = new ArrayList
(); relationList = new ArrayList
(); } public void addCondition(Condition condition) { addCondition(condition, true); } public void addCondition(Condition condition, boolean or) { conditionList.add(condition); if (EmptyUtils.notEmpty(conditionList)) { relationList.add(or); } } public void addCondition(Condition condition, AndOr ao) { conditionList.add(condition); if (EmptyUtils.notEmpty(conditionList)) { if (ao == AndOr.AND) { relationList.add(AND); } else { relationList.add(OR); } } } public void addO