package cn.edu.hbcf.common.vo;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import cn.edu.hbcf.common.utils.TypeCaseHelper; /**
* 公用条件查询类 也可以用于MVC层之间的参数传递
*
* @author LiPenghui
*
*/
public class Criteria { /**
* 存放条件查询值
*/
private Map<String, Object> condition;
/**
* 是否相异 为什么是protected类型?
*/
private boolean distinct;
/**
* 排序字段
*/
private String orderByClause; private int start;
private int limit; private Integer oracleStart;
private Integer oracleEnd; public Integer getOracleStart() { return oracleStart;
} public void setOracleStart(Integer oracleStart) {
this.oracleStart = oracleStart;
} public Integer getOracleEnd() {
return oracleEnd;
} public void setOracleEnd(Integer oracleEnd) {
this.oracleEnd = oracleEnd;
} public Map<String, Object> getCondition() {
return condition;
} public void setCondition(Map<String, Object> condition) {
this.condition = condition;
} public Criteria() {
condition = new HashMap<String, Object>();
} public void clear() {
this.condition.clear();
this.orderByClause = null;
this.distinct = false;
this.start = 0;
this.limit = 0;
} /**
* 添加查询条件
*
* @param conditionKey
* 查询条件的名称
* @param contidionValue
* 查询条件的值
* @return
*/
public Criteria put(String key, Object value) {
this.condition.put(key, value);
return (Criteria) this;
} /**
* 得到键值,C层和S层的参数传递时取值所用 自行转换对象
*
* @param contionKey
* 条件键值
* @return 返回指定键所映射的值
*/
public Object get(String contionKey) {
return this.condition.get(contionKey);
} public Map<String, Object> getConditionMap() {
return condition;
} public void setConditionMap(Map<String, Object> conditionMap) {
this.condition = conditionMap;
} /**
* 是否相异
*
* @return
*/
public boolean isDistinct() {
return distinct;
} public void setDistinct(boolean distinct) {
this.distinct = distinct;
} /**
* 获取排序字段
*
* @return
*/
public String getOrderByClause() {
return orderByClause;
} /**
* 设置排序字段
*
* @param orderByClause
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
} /**
* 以BigDecimal返回键值
*
* @param key
* 键名
* @return Date 键值
*/
public BigDecimal getAsBigDecimal(String key) {
Object obj = TypeCaseHelper.convert(get(key), "BigDecimal", null);
if (obj != null) {
return (BigDecimal) obj;
} else {
return null;
}
} /**
* 以Date类型返回键值
*
* @param key
* 键名
* @return Date 键值
*/
public Date getAsDate(String key) {
Object obj = TypeCaseHelper.convert(get(key), "Date", "yyyy-MM-dd");
if (obj == null) {
return (Date) obj;
} else {
return null;
}
} /**
* 以Integer类型返回键值
*
* @param key
* 键名
* @return Integer 键值
*/
public Integer getAsInteger(String key) {
Object obj = TypeCaseHelper.convert(get(key), "Integer", null);
if (obj != null) {
return (Integer) obj;
} else {
return null;
}
} /**
* 以Long类型返回键值
*
* @param key
* 键名
* @return Long 键值
*/
public Long getAsLong(String key) {
Object obj = TypeCaseHelper.convert(get(key), "Long", null);
if (obj != null) {
return (Long) obj;
} else {
return null;
}
} /**
* 以字符串类型返回键值
*
* @param key
* 键名
* @return 键值
*/
public String getAsString(String key) {
Object obj = TypeCaseHelper.convert(get(key), "String", null);
if (obj != null) {
return (String) obj;
} else {
return null;
}
} /**
* 以Timestamp类型返回键值
*
* @param key
* 键名
* @return Timestamp键值
*/
public Timestamp getAsTimestamp(String key) {
Object obj = TypeCaseHelper.convert(get(key), "Timestamp", null);
if (obj != null) {
return (Timestamp) obj;
} else {
return null;
}
} /**
* 以Boolean类型返回键值
*
* @param key
* 键名
* @return Boolean 键值
*/
public Boolean getAsBoolean(String key) {
Object obj = TypeCaseHelper.convert(get(key), "Boolean", null);
if (obj != null) {
return (Boolean) obj;
} else {
return null;
}
} public int getStart() {
return start;
} public void setStart(int start) {
this.start = start;
} public int getLimit() {
return limit;
} public void setLimit(int limit) {
this.limit = limit;
}
}

TypeCaseHelper.java

package cn.edu.hbcf.common.utils;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale; /**
* 类型转换辅助工具类<br>
*
*/
public class TypeCaseHelper { /**
* 转换核心实现方法
*
* @param obj
* @param type
* @param format
* @return Object
* @throws TypeCastException
*/
public static Object convert(Object obj, String type, String format) throws TypeCastException {
Locale locale = new Locale("zh", "CN", "");
if (obj == null)
return null;
if (obj.getClass().getName().equals(type))
return obj;
if ("Object".equals(type) || "java.lang.Object".equals(type))
return obj;
String fromType = null;
if (obj instanceof String) {
fromType = "String";
String str = (String) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return obj;
if (str.length() == 0)
return null;
if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) {
Boolean value = null;
if (str.equalsIgnoreCase("TRUE"))
value = new Boolean(true);
else
value = new Boolean(false);
return value;
}
if ("Double".equals(type) || "java.lang.Double".equals(type))
try {
Number tempNum = getNf(locale).parse(str);
return new Double(tempNum.doubleValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
try {
BigDecimal retBig = new BigDecimal(str);
int iscale = str.indexOf(".");
int keylen = str.length();
if (iscale > -1) {
iscale = keylen - (iscale + 1);
return retBig.setScale(iscale, 5);
} else {
return retBig.setScale(0, 5);
}
} catch (Exception e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Float".equals(type) || "java.lang.Float".equals(type))
try {
Number tempNum = getNf(locale).parse(str);
return new Float(tempNum.floatValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Long".equals(type) || "java.lang.Long".equals(type))
try {
NumberFormat nf = getNf(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Long(tempNum.longValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
try {
NumberFormat nf = getNf(locale);
nf.setMaximumFractionDigits(0);
Number tempNum = nf.parse(str);
return new Integer(tempNum.intValue());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
if ("Date".equals(type) || "java.sql.Date".equals(type)) {
if (format == null || format.length() == 0)
try {
return Date.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null)
df = DateFormat.getDateInstance(3, locale);
else
df = DateFormat.getDateInstance(3);
java.util.Date fieldDate = df.parse(str);
return new Date(fieldDate.getTime());
} catch (ParseException e1) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new Date(fieldDate.getTime());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) {
if (str.length() == 10)
str = str + " 00:00:00";
if (format == null || format.length() == 0)
try {
return Timestamp.valueOf(str);
} catch (Exception e) {
try {
DateFormat df = null;
if (locale != null)
df = DateFormat.getDateTimeInstance(3, 3, locale);
else
df = DateFormat.getDateTimeInstance(3, 3);
java.util.Date fieldDate = df.parse(str);
return new Timestamp(fieldDate.getTime());
} catch (ParseException e1) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
java.util.Date fieldDate = sdf.parse(str);
return new Timestamp(fieldDate.getTime());
} catch (ParseException e) {
throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e);
}
} else {
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
}
if (obj instanceof BigDecimal) {
fromType = "BigDecimal";
BigDecimal bigD = (BigDecimal) obj;
if ("String".equals(type))
return getNf(locale).format(bigD.doubleValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return obj;
if ("Double".equals(type))
return new Double(bigD.doubleValue());
if ("Float".equals(type))
return new Float(bigD.floatValue());
if ("Long".equals(type))
return new Long(Math.round(bigD.doubleValue()));
if ("Integer".equals(type))
return new Integer((int) Math.round(bigD.doubleValue()));
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Double) {
fromType = "Double";
Double dbl = (Double) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return getNf(locale).format(dbl.doubleValue());
if ("Double".equals(type) || "java.lang.Double".equals(type))
return obj;
if ("Float".equals(type) || "java.lang.Float".equals(type))
return new Float(dbl.floatValue());
if ("Long".equals(type) || "java.lang.Long".equals(type))
return new Long(Math.round(dbl.doubleValue()));
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
return new Integer((int) Math.round(dbl.doubleValue()));
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(dbl.toString());
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Float) {
fromType = "Float";
Float flt = (Float) obj;
if ("String".equals(type))
return getNf(locale).format(flt.doubleValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(flt.doubleValue());
if ("Double".equals(type))
return new Double(flt.doubleValue());
if ("Float".equals(type))
return obj;
if ("Long".equals(type))
return new Long(Math.round(flt.doubleValue()));
if ("Integer".equals(type))
return new Integer((int) Math.round(flt.doubleValue()));
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Long) {
fromType = "Long";
Long lng = (Long) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return getNf(locale).format(lng.longValue());
if ("Double".equals(type) || "java.lang.Double".equals(type))
return new Double(lng.doubleValue());
if ("Float".equals(type) || "java.lang.Float".equals(type))
return new Float(lng.floatValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type))
return new BigDecimal(lng.toString());
if ("Long".equals(type) || "java.lang.Long".equals(type))
return obj;
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
return new Integer(lng.intValue());
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Integer) {
fromType = "Integer";
Integer intgr = (Integer) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
return getNf(locale).format(intgr.longValue());
if ("Double".equals(type) || "java.lang.Double".equals(type))
return new Double(intgr.doubleValue());
if ("Float".equals(type) || "java.lang.Float".equals(type))
return new Float(intgr.floatValue());
if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) {
String str = intgr.toString();
BigDecimal retBig = new BigDecimal(intgr.doubleValue());
int iscale = str.indexOf(".");
int keylen = str.length();
if (iscale > -1) {
iscale = keylen - (iscale + 1);
return retBig.setScale(iscale, 5);
} else {
return retBig.setScale(0, 5);
}
}
if ("Long".equals(type) || "java.lang.Long".equals(type))
return new Long(intgr.longValue());
if ("Integer".equals(type) || "java.lang.Integer".equals(type))
return obj;
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Date) {
fromType = "Date";
Date dte = (Date) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
if (format == null || format.length() == 0) {
return dte.toString();
} else {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new java.util.Date(dte.getTime()));
}
if ("Date".equals(type) || "java.sql.Date".equals(type))
return obj;
if ("Time".equals(type) || "java.sql.Time".equals(type))
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type))
return new Timestamp(dte.getTime());
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Timestamp) {
fromType = "Timestamp";
Timestamp tme = (Timestamp) obj;
if ("String".equals(type) || "java.lang.String".equals(type))
if (format == null || format.length() == 0) {
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(tme).toString();
} else {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new java.util.Date(tme.getTime()));
}
if ("Date".equals(type) || "java.sql.Date".equals(type))
return new Date(tme.getTime());
if ("Time".equals(type) || "java.sql.Time".equals(type))
return new Time(tme.getTime());
if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type))
return obj;
else
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
if (obj instanceof Boolean) {
fromType = "Boolean";
Boolean bol = (Boolean) obj;
if ("Boolean".equals(type) || "java.lang.Boolean".equals(type))
return bol;
if ("String".equals(type) || "java.lang.String".equals(type))
return bol.toString();
if ("Integer".equals(type) || "java.lang.Integer".equals(type)) {
if (bol.booleanValue())
return new Integer(1);
else
return new Integer(0);
} else {
throw new TypeCastException("Conversion from " + fromType + " to " + type + " not currently supported");
}
}
if ("String".equals(type) || "java.lang.String".equals(type))
return obj.toString();
else
throw new TypeCastException("Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported");
} private static NumberFormat getNf(Locale locale) {
NumberFormat nf = null;
if (locale == null)
nf = NumberFormat.getNumberInstance();
else
nf = NumberFormat.getNumberInstance(locale);
nf.setGroupingUsed(false);
return nf;
} public static Boolean convert2SBoolean(Object obj) throws TypeCastException {
return (Boolean) convert(obj, "Boolean", null);
} public static Integer convert2Integer(Object obj) throws TypeCastException {
return (Integer) convert(obj, "Integer", null);
} public static String convert2String(Object obj) throws TypeCastException {
return (String) convert(obj, "String", null);
} public static String convert2String(Object obj, String defaultValue) throws TypeCastException {
Object s = convert(obj, "String", null);
if (s != null)
return (String) s;
else
return "";
} public static Long convert2Long(Object obj) throws TypeCastException {
return (Long) convert(obj, "Long", null);
} public static Double convert2Double(Object obj) throws TypeCastException {
return (Double) convert(obj, "Double", null);
} public static BigDecimal convert2BigDecimal(Object obj, int scale) throws TypeCastException {
return ((BigDecimal) convert(obj, "BigDecimal", null)).setScale(scale, 5);
} public static Date convert2SqlDate(Object obj, String format) throws TypeCastException {
return (Date) convert(obj, "Date", format);
} public static Timestamp convert2Timestamp(Object obj, String format) throws TypeCastException {
return (Timestamp) convert(obj, "Timestamp", format);
}
}

TypeCastException.java

package cn.edu.hbcf.common.utils;

import java.io.PrintStream;
import java.io.PrintWriter; /**
* 类型转换工具类<br>
*
* @author
* @since 2009-07-06
* @see java.lang.RuntimeException
*/
public class TypeCastException extends RuntimeException { private static final long serialVersionUID = 1L; Throwable nested; public TypeCastException() {
nested = null;
} public TypeCastException(String msg) {
super(msg);
nested = null;
} public TypeCastException(String msg, Throwable nested) {
super(msg);
this.nested = null;
this.nested = nested;
} public TypeCastException(Throwable nested) { this.nested = null;
this.nested = nested;
} public String getMessage() {
if (nested != null)
return super.getMessage() + " (" + nested.getMessage() + ")";
else
return super.getMessage();
} public String getNonNestedMessage() {
return super.getMessage();
} public Throwable getNested() {
if (nested == null)
return this;
else
return nested;
} public void printStackTrace() {
super.printStackTrace();
if (nested != null)
nested.printStackTrace();
} public void printStackTrace(PrintStream ps) {
super.printStackTrace(ps);
if (nested != null)
nested.printStackTrace(ps);
} public void printStackTrace(PrintWriter pw) {
super.printStackTrace(pw);
if (nested != null)
nested.printStackTrace(pw);
}
}

公共查询类criteria的更多相关文章

  1. hibernate----(Hql)另一种查询---利用Criteria类

    package com.etc.test; import java.util.List; import org.hibernate.Criteria;import org.hibernate.Sess ...

  2. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

  3. Util应用程序框架公共操作类(八):Lambda表达式公共操作类(二)

    前面介绍了查询的基础扩展,下面准备给大家介绍一些有用的查询封装手法,比如对日期范围查询,数值范围查询的封装等,为了支持这些功能,需要增强公共操作类. Lambda表达式公共操作类,我在前面已经简单介绍 ...

  4. Util应用程序框架公共操作类(七):Lambda表达式公共操作类

    前一篇扩展了两个常用验证方法,本文将封装两个Lambda表达式操作,用来为下一篇的查询扩展服务. Lambda表达式是一种简洁的匿名函数语法,可以用它将方法作为委托参数传递.在Linq中,大量使用La ...

  5. Util应用程序框架公共操作类(六):验证扩展

    前面介绍了仓储的基本操作,下面准备开始扩展查询,在扩展查询之前,首先要增加两个公共操作类,一个是经常要用到的验证方法,另一个是Lambda表达式的操作类. 很多时候,我们会判断一个对象是否为null, ...

  6. 【C#公共帮助类】JsonHelper 操作帮助类, 以后再也不用满地找Json了,拿来直接用

     四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换j ...

  7. hibernate框架学习笔记7:HQL查询、Criteria查询简介

    HQL查询:hibernate独有的查询语言 适用于不复杂的多表查询 示例: 实体类: package domain; public class Customer { private Long cus ...

  8. Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加

    Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...

  9. 【C#公共帮助类】JsonHelper 操作帮助类

    四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换js ...

随机推荐

  1. C++迭代器之'反向迭代器'

    反向迭代器(Reverse Iterator)是普通迭代器的适配器,通过重新定义自增和自减操作,以达到按反序遍历元素的目的.如果在标准算法库中用反向迭代器来代替普通的迭代器,那么运行结果与正常情况下相 ...

  2. Linux pci驱动源码

    #include <linux/kernel.h>#include <linux/errno.h>#include <linux/module.h>#include ...

  3. 体积雾 global fog unity 及改进

    https://github.com/keijiro/KinoFog lighting box 2.7.2 halfspace fog 相机位于一个位置 这个位置可以在fog volumn里面或者外面 ...

  4. 配置thinkphp3.2 404页面

    ThinkPHP自身提供了 404 页面的处理机制,我们只需要在控制器 中添加一个 EmptyController.class.php,并且实现以下方法即可,方法如下: <? class  Em ...

  5. SQL Server datetime数据类型设计、优化误区

    一.场景 在SQL Server 2005中,有一个表TestDatetime,其中Dates这个字段的数据类型是datetime,如果你看到表的记录如下图所示,你最先想到的是什么呢? (图1:数据列 ...

  6. 解决Oracle在Linux下Listener起不来,error 111错误

    近来发生一个问题有点头疼,在linux上的Oracle数据库突然无法访问 主要报错如下: 基于本人的走歪路经验,分享一下我的解决思路: 首先,最直观的一点,监听器起不来,是不是数据库本身就没起来 se ...

  7. Android6.0指纹识别开发

    近期在做android指纹相关的功能,谷歌在android6.0及以上版本号对指纹识别进行了官方支持.当时在FingerprintManager和FingerprintManagerCompat这两个 ...

  8. Node.js mm131图片批量下载爬虫1.00 iconv协助转码

    //====================================================== // mm131图片批量下载爬虫1.00 // 2017年11月15日 //===== ...

  9. Android微信支付V3版

    由于公司需求做微信APP支付,在集成过程中也遇到各种问题,比如说签名错误,body编码必须为UTF-8.APP端无法调用支付页面直接到支付结果页面.结果为null,code=-1等等: 1.签名错误问 ...

  10. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何使用断点

    首先写好简单的程序,比如A=10,然后A每次都会递减,C是SQRT(A),这样当A时负数的时候就会异常了,点击PLC-Windows-断点   点击新建,然后可以设置断点的位置(注意程序写好之后先运行 ...