HibernateCRUD基础框架(2)-HQL语句构造器(HqlQueryBuilder,HqlUpdateBuilder)
上篇讲述了最基本的实体类,本篇接着讲述HQL语句构造器,包括查询和更新等。
优点:通过面向对象的方式构造HQL语句,更快捷,不需要手动拼接HQL。
缺点:封装可能降低性能,只能支持常用的和较为简单的HQL构造。
部分功能不完善,待开发。
1.HQL语句构造器
package cn.fansunion.hibernate.sql; import org.apache.commons.lang.text.StrBuilder; import cn.fansunion.hibernate.sql.entity.From;
import cn.fansunion.hibernate.sql.entity.GroupBy;
import cn.fansunion.hibernate.sql.entity.OrderGroup;
import cn.fansunion.hibernate.sql.entity.SearchCondition; /**
* HQL语句构造器。
* <p/>
* 目前只适用于1张表的情况,只有查询条件支持占位符,建议使用完全构造的sql语句(是否会产生sql注入,待研究)。
*
* @author LeiWen@FansUnion.cn
*/
public class HqlQueryBuilder extends ConstantBase { // 查询条件组合策略
// -----------------------------------
// *************From******************
// -----------------------------------
private String select; private From from; // -----------------------------------
// *************Where******************
// -----------------------------------
private SearchCondition searchCondition; // -----------------------------------
// *************Group by**************
// ----------------------------------- private GroupBy groupBy; // -----------------------------------
// *************Order by******************
// -----------------------------------
private OrderGroup orderGroup; // -----------------------------------
// 通过构造方法,创建查询构造器。各种字段组合形式的构造方法太多,只给出3种比较常见的。
// 建议使用链式构造或setter方法设置属性。
// -----------------------------------
public HqlQueryBuilder() { } public HqlQueryBuilder(From from) {
this.from = from;
} public HqlQueryBuilder(From from, SearchCondition searchCondition,
GroupBy groupBy, OrderGroup orderGroup) {
this.from = from; this.searchCondition = searchCondition;
this.groupBy = groupBy;
this.orderGroup = orderGroup;
} public HqlQueryBuilder select(String select) {
this.select = select;
return this;
} // -----------------------------------
// 支持链式用法,暂时不能很好地支持**************
// -----------------------------------
public HqlQueryBuilder from(From from) {
this.from = from;
return this;
} public HqlQueryBuilder from(String model) {
doFrom(model, "");
return this;
} public HqlQueryBuilder from(String model, String alias) {
doFrom(model, alias);
return this;
} public HqlQueryBuilder from(Class<?> clazz) {
doFrom(clazz.getSimpleName());
return this;
} public HqlQueryBuilder from(Class<?> clazz, String alias) {
doFrom(clazz.getSimpleName(), alias);
return this;
} private void doFrom(String model) {
doFrom(model, null);
} private void doFrom(String model, String alias) {
this.from = new From(model, alias);
} public HqlQueryBuilder searchCodition(SearchCondition searchCondition) {
this.searchCondition = searchCondition;
return this;
} public HqlQueryBuilder groupBy(GroupBy groupBy) {
this.groupBy = groupBy;
return this;
} public HqlQueryBuilder orderBy(OrderGroup orderGroup) {
this.orderGroup = orderGroup;
return this;
} /**
* 转换成HQL语句
*/
public String toHql() { StrBuilder builder = new StrBuilder();
if (select != null) {
builder.append(select).append(EMPTY);
}
if (from != null) {
builder.append(from);
} if (searchCondition != null) {
builder.append(searchCondition);
}
if (groupBy != null) {
builder.append(groupBy);
}
if (orderGroup != null) {
builder.append(orderGroup);
} return builder.toString();
} }
2.Hql更新语句构造器
下面这个是构造更新HQL语句的构造器,待完善。
package cn.fansunion.hibernate.sql.update; import java.util.HashMap;
import java.util.Map; import cn.fansunion.hibernate.util.Pair; /**
* Hql更新语句构造器。(TODO 待完善)
*
* @author LeiWen@FansUnion.cn
*/
public class HqlUpdateBuilder { private Map<String, Object> params; private String model; public HqlUpdateBuilder() {
params = new HashMap<String, Object>();
} public HqlUpdateBuilder(String model) { } public HqlUpdateBuilder(Class<?> model) {
this.model = model.getSimpleName();
} public HqlUpdateBuilder model(String model) {
this.model = model;
return this;
} public HqlUpdateBuilder model(Class<?> model) {
this.model = model.getSimpleName();
return this;
} public HqlUpdateBuilder param(String key, Object value) {
params.put(key, value);
return this;
} public HqlUpdateBuilder param(Pair... pair) {
for (Pair p : pair) {
params.put(p.getKey(), p.getValue());
}
return this;
} public HqlUpdateBuilder param(Map<String, Object> params) {
this.params.putAll(params);
return this;
} public String toHql() {
String hql = "update " + model + " set ";
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
hql += key + "=:" + key + " ";
}
return hql;
} }
3.原生SQL语句构造器
类似的,有时候,可能不用Hibernate的HQL,而是用原生的SQL。
这个时候,可以编写与HqlQueryBuilder和HqlUpdateBuilder相应的SQL版本。
package cn.fansunion.hibernate.sql; import cn.fansunion.hibernate.sql.entity.Limit; /**
* 原生SQL语句构造器。(TODO 待完善)
*
* @author LeiWen@FansUnion.cn
*/
public class SqlQueryBuilder {
// limit只适用于nativeSQL
// -----------------------------------
// *************limit**************
// -----------------------------------
private Limit limit; /**
* 转换成SQL语句
*/ public String toSql(){
//TODO
return null;
}
}
4.HQL查询构造器使用例子
/**
* HQL查询构造器使用例子。
*
* @author LeiWen@FansUnion.cn
*/
public class HqlQueryBuilderTest extends ConstantBase { @Test
public void test() {
From from = new From(HqlQueryBuilder.class);
SearchCondition searchCondition = createSearchCondtion();
OrderGroup orderGroup = createOrderGroup();
GroupBy groupBy = createGroupBy();
HqlQueryBuilder builder = new HqlQueryBuilder(from, searchCondition,
groupBy, orderGroup);
// 直接打印,不使用“断言”
println(builder.toHql());
} private GroupBy createGroupBy() {
GroupBy groupBy = new GroupBy();
groupBy.addGroup("name");
groupBy.addGroup("id");
return groupBy;
} private OrderGroup createOrderGroup() {
Order order1 = new Order("id", false);
Order order2 = new Order("name", "asc"); OrderGroup orderGroup = new OrderGroup(order1, order2);
return orderGroup;
} private SearchCondition createSearchCondtion() {
GroupCondition groupCondition1 = oneGroupCondition();
GroupCondition groupCondition2 = oneGroupCondition();
//String groupStr1 = groupCondition1.toString();
//String groupStr2 = groupCondition2.toString();
// System.out.println(groupStr1);
// System.out.println(groupStr2); SearchCondition searchCondition = new SearchCondition();
searchCondition.addGroupCondition(groupCondition1);
searchCondition.addGroupCondition(groupCondition2, true);
//String searchStr = searchCondition.toString();
// System.out.println(searchStr);
return searchCondition;
} private GroupCondition oneGroupCondition() {
// =,Integer
String age = "age";
Integer ageValue = 24;
Condition condition2 = new Condition(age, Operator.EQUALS, ageValue);
String str2 = condition2.toString(); Assert.assertEquals(str2, age + EQUALS_WITH_BLANK + ageValue); // =,String
String name = "name";
String nameValue = "LeiWen@FansUnion.cn";
Condition condition = new Condition(name, Operator.EQUALS, nameValue);
String str = condition.toString();
Assert.assertEquals(str, name + EQUALS_WITH_BLANK
+ buildQuota(nameValue)); // =,Date
String date = "date";
Date dateValue = new Date();
Condition condition3 = new Condition(date, Operator.EQUALS, dateValue);
String str3 = condition3.toString();
Assert.assertEquals(str3, date + EQUALS_WITH_BLANK
+ buildQuota(dateValue)); GroupCondition groupCondition1 = new GroupCondition();
groupCondition1.addCondition(condition);
groupCondition1.addCondition(condition2, true);
groupCondition1.addCondition(condition3, false);
return groupCondition1;
} }
5.输出结果
from HqlQueryBuilder where (name = 'LeiWen@FansUnion.cn' or age = 24 and date = 'Mon Dec 30 16:57:21 CST 2013')
or (name = 'LeiWen@FansUnion.cn' or age = 24 and date = 'Mon Dec 30 16:57:21 CST 2013')
group by name,id order by id desc,name asc
原文链接:http://blog.fansunion.cn/articles/3622(小雷博客-blog.fansunion.cn)
HibernateCRUD基础框架(2)-HQL语句构造器(HqlQueryBuilder,HqlUpdateBuilder)的更多相关文章
- HibernateCRUD基础框架(1)-实体类
HibernateCRUD基础框架包括3篇文章,主要讲述整个CRUD基础框架的思路. 第1篇:讲述最基本的实体类,这些实体类是对SQL语言中的一些概念的封装. 第2篇:在这些实体类的基础上,开发一个& ...
- HibernateCRUD基础框架(3)-简单的和较为复杂的标准的CRUD API
优点:简单的和基础的CRUD功能可以很快实现,可以说是比较的"标准化".维护起来也很容易. 缺点:性能没有保障.不支持特别复杂的CRUD. 可以适用的场景:小型Web项目 1.Cr ...
- Hibernate框架HQL语句
这篇随笔将会记录hql的常用的查询语句,为日后查看提供便利. 在这里通过定义了三个类,Special.Classroom.Student来做测试,Special与Classroom是一对多,Class ...
- 用Hibernate框架把hql生成可执行的sql语句-Oracle方言
引言:为什么这样的需求,源自公司项目需要,公司的项目使用java的struts2+spring2.5+oracle中间件tuxedo,数据库用的是Oracle,但由于不直接连接数据库,用中间件处理的方 ...
- java:Hibernate框架3(使用Myeclipse逆向工程生成实体和配置信息,hql语句各种查询(使用hibernate执行原生SQL语句,占位符和命名参数,封装Vo查询多个属性,聚合函数,链接查询,命名查询),Criteria)
1.使用Myeclipse逆向工程生成实体和配置信息: 步骤1:配置MyEclipse Database Explorer: 步骤2:为项目添加hibernate的依赖: 此处打开后,点击next进入 ...
- Hibernate框架之HQL查询与Criteria 查询的区别
Hibernate框架提供了HQL查询和Criteria 查询.下面对这两种查询分别做个例子.也好对这两种查询方法有个大概的了解.就用房屋信息表做例子,查询所有房屋信息. HQL语句查询所有房屋信息: ...
- 一篇文章带你掌握主流基础框架——Spring
一篇文章带你掌握主流基础框架--Spring 这篇文章中我们将会介绍Spring的框架以及本体内容,包括核心容器,注解开发,AOP以及事务等内容 那么简单说明一下Spring的必要性: Spring技 ...
- 详解Java的MyBatis框架中SQL语句映射部分的编写
这篇文章主要介绍了Java的MyBatis框架中SQL语句映射部分的编写,文中分为resultMap和增删查改实现两个部分来讲解,需要的朋友可以参考下 1.resultMap SQL 映射XML 文件 ...
- PHP 设计模式 笔记与总结(2)开发 PSR-0 的基础框架
[PSR-0 规范的三项约定]: ① 命名空间必须与绝对路径一致 ② 类名的首字母必须大写 ③ 除入口文件外,其他".php"必须只有一个类(不能有可执行的代码) [开发符合 PS ...
随机推荐
- 洛谷 P2690 接苹果
P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...
- C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序
C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 ...
- eclispe中如何创建web项目
xian 1.从file中点击---->new----->other---->javaEE----->web---->Dynamic Web project----&g ...
- jquery15 on() trigger() : 事件操作的相关方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- IBM磁盘阵列及文件系统的管理
一.几个基本概念 物理卷(PV):一个物理卷指一块硬盘 卷组(VG):卷组是可用物理硬盘的集合,可以逻辑地看成一块大硬盘 物理分区(PP):卷组中物理卷划分成固定大小的块(缺省为4MB) 逻辑卷(LV ...
- 2017国家集训队作业[agc006f]Blackout
2017国家集训队作业[agc006f]Blackout 题意: 有一个\(N*N\)的网格,一开始有\(M\)个格子被涂黑,给出这\(M\)个格子,和染色操作:如果有坐标为\((x,y),(y,z) ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- Android模拟、实现、触发系统按键事件的方法
Android模拟.实现.触发系统按键事件的方法 /** * 模拟系统按键. * * @param keyCode */ public static void onKeyEvent(final ...
- 汉化 Hirens.BootCD 中的 XP 系统
汉化 Hirens.BootCD 中的 XP 系统 1. 在中文版 XPSP3 镜像中提取所需的文件 原系统为没有作 server pack 的 Windows XP Professional ,在W ...
- 类名引用static变量好处
不仅强调了变量static的结构,而且在有些情况下他还为编译器进行优化提供了更好的机会.