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<Condition> conditionList; /**
* 组内各个条件之间的关系:且|或,默认为且
*/
private List<Boolean> relationList; public static final Boolean AND = true;
public static final Boolean OR = false; public GroupCondition() {
conditionList = new ArrayList<Condition>();
relationList = new ArrayList<Boolean>();
} 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 addOr(int index, boolean or) {
relationList.set(index, or);
} /**
* 转化为字符串
*/
public String toString() {
if (EmptyUtils.isEmpty(conditionList)) {
return EMPTY;
} StrBuilder builder = new StrBuilder();
int size = conditionList.size();
for (int index = 0; index < size; index++) {
if (index == 0) {
builder.append(conditionList.get(index));
} else {
builder.append(BLANK).append(isOr(relationList.get(index)))
.append(BLANK).append(conditionList.get(index));
}
}
return builder.toString();
} }

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.sql.ConstantBase;
import cn.fansunion.hibernate.util.EmptyUtils; /**
* 搜索条件(2个组之间,且|或。)
*
* @author LeiWen@FansUnion.cn
*/
public class SearchCondition extends ConstantBase { /**
* 多个组条件构成的集合
*/
private List<GroupCondition> groupConditionList; /**
* 多个组条件之间的关系:且|或,默认为且
*/
private List<Boolean> and; public SearchCondition() {
groupConditionList = new ArrayList<GroupCondition>();
and = new ArrayList<Boolean>(); } public void addGroupCondition(GroupCondition groupCondition) {
addGroupCondition(groupCondition, false);
} public void addGroupCondition(GroupCondition groupCondition, boolean or) {
groupConditionList.add(groupCondition);
if (EmptyUtils.notEmpty(groupConditionList)) {
and.add(or);
}
} public void addOr(int index, boolean or) {
and.set(index, or);
} public List<GroupCondition> getGroupConditionList() {
return groupConditionList;
} public void setGroupConditionList(List<GroupCondition> groupConditionList) {
this.groupConditionList = groupConditionList;
} public List<Boolean> getAnd() {
return and;
} public void setAnd(List<Boolean> and) {
this.and = and;
} /**
* 转化为字符串
*/
public String toString() {
if (EmptyUtils.isEmpty(groupConditionList)) {
return EMPTY;
} StrBuilder builder = new StrBuilder(); int size = groupConditionList.size();
for (int index = 0; index < size; index++) {
// 1个组的条件用()包起来
if (index == 0) {
builder.append(WHERE_WITH_BLANK).append(
buildBracket(groupConditionList.get(index))); } else {
builder.append(BLANK).append(isOr(and.get(index)))
.append(BLANK)
.append(buildBracket(groupConditionList.get(index)));
}
} return builder.toString();
}
}

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; /**
*
* 排序语句,如“order by id desc”。
*
* @author LeiWen@FansUnion.cn
*/
public class Order extends ConstantBase {
/**
* 排序语句的键
*/
private String key;
/**
* 排序语句的升序或降序(字符串形式)
*/
private String asc;
/**
* 排序语句的升序或降序(布尔形式)
*/
private Boolean isAsc; public Order() { } public Order(String key, String asc) {
this.key = key;
this.asc = asc;
} public Order(String key, Boolean isAsc) {
super();
this.key = key;
this.isAsc = isAsc;
} /**
* 转化为字符串
*/
public String toString() {
String finalSort = DESC; if (StringUtils.isNotEmpty(asc)) {
finalSort = asc;
} else {
if (isAsc) {
finalSort = ASC;
}
} StrBuilder builder = new StrBuilder();
builder.append(key).append(BLANK).append(finalSort);
return builder.toString();
} }

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.sql.ConstantBase;
import cn.fansunion.hibernate.util.ListUtils; /**
* 排序组,如"order by id desc,name asc"。
*
* @author LeiWen@FansUnion.cn
*/
public class OrderGroup extends ConstantBase {
/**
* 多个排序构成的集合
*/
private List<Order> orderList; public OrderGroup() {
orderList = new ArrayList<Order>();
} public OrderGroup(Order order) {
this();
orderList.add(order);
} public OrderGroup(Order order1, Order order2) {
this();
orderList.add(order1);
orderList.add(order2);
} public void addOrder(Order order) {
orderList.add(order);
}
/**
* 转化为字符串
*/
public String toString() {
StrBuilder builder = new StrBuilder();
if (ListUtils.notEmpty(orderList)) {
String orders = ListUtils.list2String(orderList);
builder.append(ORDER_BY_WITH_BLANK).append(orders);
}
return builder.toString();
} }

package cn.fansunion.hibernate.sql.entity;

import java.util.ArrayList;
import java.util.List; import cn.fansunion.hibernate.sql.ConstantBase;
import cn.fansunion.hibernate.util.EmptyUtils;
import cn.fansunion.hibernate.util.ListUtils; /**
* 分组语句,如"group by id,name"。
*
* @author LeiWen@FansUnion.cn
*/
public class GroupBy extends ConstantBase { /**
* 分组的字段名称构成的集合,暂时不支持having子句
*/
private List<String> group; public GroupBy() {
group = new ArrayList<String>();
} public List<String> getGroup() {
return group;
} /**
* 转化为字符串
*/
public String toString() {
if (EmptyUtils.notEmpty(group)) {
String groupStr = ListUtils.list2String(group);
return GROUP_BY_WITH_BLANK + groupStr;
}
return EMPTY;
} public void addGroup(String g) {
this.group.add(g);
}
}

原文链接http://blog.fansunion.cn/articles/3616(小雷博客-blog.fansunion.cn)

HibernateCRUD基础框架(1)-实体类的更多相关文章

  1. [LINQ2Dapper]最完整Dapper To Linq框架(三)---实体类关系映射

    此特性需要安装Kogel.Dapper.Mssql或者Oracle 3.06及以上版本,实体类层需要安装Kogel.Dapper.Extension 3.06及以上版本 目录 [LINQ2Dapper ...

  2. spring框架对于实体类复杂属性注入xml文件的配置

    spring框架是javaWeb项目中至关重要的一个框架,大多web 项目在工作层次上分为持久层.服务层.控制层.持久层(dao.mapper)用于连接数据库,完成项目与数据库中数据的传递:服务层(s ...

  3. DAO层使用mybatis框架有关实体类的有趣细节

    1.根据个人习惯,将储存那些数据库查询结果集有映射关系的实体类的Package包名有如下格式: cn.bjut.domain cn.bjut.pojo cn.bjut.model cn.bjut.en ...

  4. HibernateCRUD基础框架(2)-HQL语句构造器(HqlQueryBuilder,HqlUpdateBuilder)

    上篇讲述了最基本的实体类,本篇接着讲述HQL语句构造器,包括查询和更新等. 优点:通过面向对象的方式构造HQL语句,更快捷,不需要手动拼接HQL. 缺点:封装可能降低性能,只能支持常用的和较为简单的H ...

  5. HibernateCRUD基础框架(3)-简单的和较为复杂的标准的CRUD API

    优点:简单的和基础的CRUD功能可以很快实现,可以说是比较的"标准化".维护起来也很容易. 缺点:性能没有保障.不支持特别复杂的CRUD. 可以适用的场景:小型Web项目 1.Cr ...

  6. DataSet的灵活,实体类的方便,DTO的效率:SOD框架的数据容器,打造最适合DDD的ORM框架

    引言:DDD的困惑 最近,我看到园子里面有位朋友的一篇博客 <领域驱动设计系列(一):为何要领域驱动设计? >文章中有下面一段话,对DDD使用产生的疑问: •没有正确的使用ORM, 导致数 ...

  7. 准备.Net转前端开发-WPF界面框架那些事,搭建基础框架

    题外话 最近都没怎么写博客,主要是最近在看WPF方面的书<wpf-4-unleashed.pdf>,挑了比较重要的几个章节学习了下WPF基础技术.另外,也把这本书推荐给目前正在从事WPF开 ...

  8. eclipse从数据库逆向生成Hibernate实体类

    做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...

  9. eclipse逆向生成hibernate的实体类(注解和配置文件)

    eclipse从数据库逆向生成Hibernate实体类(注解和配置文件) 分类: hibernate 数据库 java 2011-10-22 21:28 2915人阅读 评论(8) 收藏 举报 做项目 ...

随机推荐

  1. You have ettempted to queue to many files.You may select one files.

    <script type="text/javascript" src="/script/swfupload/swfupload.js"></s ...

  2. CF 439C(251C题)Devu and Partitioning of the Array

    Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabytes ...

  3. jquery13 attr() prop() val() addClass()等 : 对元素属性的操作

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. js---12数据类型,数据类型转换,NaN,

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Vue绑定事件

    <!-- 方法处理器 --> <button v-on:click="doThis"></button> <!-- 内联语句 --> ...

  6. LuoguP3356 火星探险问题(费用流)

    题目描述 火星探险队的登陆舱将在火星表面着陆,登陆舱内有多部障碍物探测车.登陆舱着陆后,探测车将离开登陆舱向先期到达的传送器方向移动.探测车在移动中还必须采集岩石标本.每一块岩石标本由最先遇到它的探测 ...

  7. Linux 时区的修改

    Linux 时区的修改 1. CentOS和Ubuntu的时区文件是/etc/localtime,但是在CentOS7以后localtime以及变成了一个链接文件 ``` [root@centos7 ...

  8. userdel---删除用户及相关文件

    userdel命令   userdel命令用于删除给定的用户,以及与用户相关的文件.若不加选项,则仅删除用户帐号,而不删除相关文件. 语法 userdel(选项)(参数) 选项 -f:强制删除用户,即 ...

  9. SDNU 1206.蚂蚁感冒 【代码如此简单,思维练习】【7月29】

    蚂蚁感冒 Description 长100厘米的细长直杆子上有n仅仅蚂蚁. 它们的头有的朝左,有的朝右. 每仅仅蚂蚁都仅仅能沿着杆子向前爬,速度是1厘米/秒. 当两仅仅蚂蚁碰面时.它们会同一时候掉头往 ...

  10. 数字签名算法--3.ECDSA

    package Imooc; import java.security.KeyFactory; import java.security.KeyPair; import java.security.K ...