zoukankan      html  css  js  c++  java
  • QueryHelper插件类(hql)

    package cn.itcast.core.util;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class QueryHelper {
        
        //from子句
        private String fromClause = "";
        //where子句
        private String whereClause = "";
        //order by子句
        private String orderByClause = "";
        
        private List<Object> parameters;
        //排序顺序
        public static String ORDER_BY_DESC = "DESC";//降序
        public static String ORDER_BY_ASC = "ASC";//升序
        
        /**
         * 构造from 子句
         * @param clazz 实体类
         * @param alias 实体类对应的别名
         */
        public QueryHelper(Class clazz, String alias){
            fromClause = "FROM " + clazz.getSimpleName() + " " + alias;
        }
        
        /**
         * 构造where子句
         * @param condition 查询条件语句;例如:i.title like ?
         * @param params 查询条件语句中?对应的查询条件值;例如: %标题%
         */
        public void addCondition(String condition, Object... params){
            if (whereClause.length() > 1) {//非第一个查询条件
                whereClause += " AND " + condition;
            } else {//第一个查询条件
                whereClause += " WHERE " + condition;
            }
            
            //设置查询条件值到查询条件值集合中
            if(parameters == null){
                parameters = new ArrayList<Object>();
            }
            if(params != null){
                for(Object param: params){
                    parameters.add(param);
                }
            }
        }
        
        /**
         * 构造order by子句
         * @param property 排序属性,如:i.createTime
         * @param order 排序顺序,如:DESC 或者 ASC
         */
        public void addOrderByProperty(String property, String order){
            if (orderByClause.length() > 1) {//非第一个排序属性
                orderByClause += "," + property + " " + order;
            } else {//第一个排序属性
                orderByClause = " ORDER BY " + property + " " + order;
            }
        }
    
        //查询hql语句
        public String getQueryListHql(){
            return fromClause + whereClause + orderByClause;
        }
        
        //查询统计数的hql语句
        public String getQueryCountHql(){
            return "SELECT COUNT(*) " + fromClause + whereClause;
        }
        
        //查询hql语句中?对应的查询条件值集合
        public List<Object> getParameters(){
            return parameters;
        }
    }
  • 相关阅读:
    PHP删除文件
    PHP定时执行任务
    PHP设置30秒内对页面的访问次数
    PHP抓取网页内容的几种方法
    QQ,新浪,SNS等公众平台的登录及api操作
    php,javascript设置和读取cookie
    php验证邮箱,手机号是否正确
    php自定义加密和解密
    Linux下安装启动多个Mysql
    linux-gcc 编译时头文件和库文件搜索路径
  • 原文地址:https://www.cnblogs.com/lm970585581/p/7359252.html
Copyright © 2011-2022 走看看