hibernateのhql文の生成をカプセル化するためにクラスを自分で書いた.

4905 ワード

毎日hqlを書くのは少し面倒で、自分でhqlを生成するためのツールクラスを書きました:HqlHelperその具体的なコードは以下の通りです:
/**
 * HQL   
 * @author cjd
 */
public class HqlHelper {
	
	private String name;
	private String hql = "";
	private String where = "";
	private String order = "";
	private List params = new ArrayList();
	

	/**
	 *     
	 * @param c	     
	 * @param name   
	 */
	public HqlHelper(Class> c,String name){
		hql = "From " +c.getSimpleName()+" "+name ;
		this.name = name;
	}
	
	/**
	 *  hql     where  ,  .addWhere("author=?",user);
	 * @param condition
	 * @param value
	 * @return
	 */
	public HqlHelper addWhere(String condition,Object obj){
		if(where.length()==0){
			where = " WHERE "+ condition;
		}else{
			where += " AND " + condition;
		}
		params.add(obj);
		return this;
	}
	/**
	 *      null.  :addConditionIsNull("d.teacher");
	 * @param condition
	 * @return
	 */
	public HqlHelper addConditionIsNull(String condition){
		if(where.length()==0){
			where = " WHERE "+ condition+ " IS NULL ";
		}else{
			where += " AND "+ condition + " IS NULL ";
		}
		return this;
	}
	/**
	 *       null.  :addConditionIsNull("d.teacher");
	 * @param condition
	 * @return
	 */
	public HqlHelper addConditionNotNull(String condition){
		if(where.length()==0){
			where = " WHERE "+ condition+ " IS NOT NULL ";
		}else{
			where += " AND "+ condition + " IS NOT NULL ";
		}
		return this;
	}
	/**
	 *   Where  
	 * 
	 * @param append
	 * @param condition
	 * @param params
	 */
	public HqlHelper addCondition(String condition, Object param) {
		addWhere(condition, param);
		return this;
	}
	
	/**
	 *    1    true,   Where  
	 * 
	 * @param append
	 * @param condition
	 * @param params
	 */
	public HqlHelper addCondition(boolean append, String condition, Object param) {
		if (append) {
			addWhere(condition, param);
		}
		return this;
	}
	
	/**
	 *   OrderBy  
	 * 
	 * @param propertyName
	 *               
	 * @param isAsc
	 *            true    ,false    
	 */
	public HqlHelper addOrder(String propertyName, boolean isAsc) {
		if (order.length() == 0) {
			order = " ORDER BY " + propertyName + (isAsc ? " ASC" : " DESC");
		} else {
			order += ", " + propertyName + (isAsc ? " ASC" : " DESC");
		}
		return this;
	}
	
	/**
	 *   OrderBy  
	 * 
	 * @param propertyName
	 *               
	 * @param isAsc
	 *            true    ,false    
	 */
	public HqlHelper addOrder(String propertyName, String order) {
		if (this.order.length() == 0) {
			this.order = " ORDER BY " + propertyName +" "+ order;
		} else {
			this.order += ", " + propertyName + " " + order;
		}
		return this;
	}

	/**
	 *    1    true,   OrderBy  
	 * 
	 * @param append
	 * @param propertyName
	 *               
	 * @param isAsc
	 *            true    ,false    
	 */
	public HqlHelper addOrder(boolean append, String propertyName, boolean isAsc) {
		if (append) {
			addOrder(propertyName, isAsc);
		}
		return this;
	}
	/**
	 *   sql    
	 * @return
	 */
	public String getHQL(){
		return hql+where+order;
	}
	
	/**
	 *   hql            
	 * @return
	 */
	public List getPatams(){
		return params;
	}
	/**
	 *        count(*) hql  
	 * @return
	 */
	public String getCountHQL(){
		return "SELECT COUNT(*) "+getHQL();
	}
	public String getDeleteHQL(){
		return "DELETE  "+getHQL();
	}

}

使用方法(完全な非同期取得データの例であり、PageUtilはページパッケージのクラスである):
		@RequestMapping(value = "/contentList.do")
		public Object jkHomeContentList(HttpServletRequest request,HttpServletResponse response,Integer rows,Integer page,String sort,String order)throws Exception {
			HqlHelper hqlHelper = new HqlHelper(TContent.class, "c");
			PageUtil pageUtil = new PageUtil(rows,page);
			String en_name = request.getParameter("en_name");
			String zh_name = request.getParameter("zh_name");
			if(en_name==null || en_name.length()==0){
				hqlHelper.addCondition("c.en_name like ?", "jk_index_%");
			}else{
				hqlHelper.addCondition("c.en_name = ?", en_name);
			}
			if(zh_name!=null && zh_name.trim().length()>0){
				hqlHelper.addCondition("c.zh_name like ?", "%"+zh_name.trim()+"%");
			}
			if(sort!=null){
				hqlHelper.addOrder(sort, order);
			}
			hqlHelper.addOrder("c.en_name",true);
			hqlHelper.addOrder("c.position_id", true);
			commonService.listByHqlHelper(hqlHelper,pageUtil);
			JSONObject result = JSONObject.fromObject(pageUtil);
			response.getWriter().write(result.toString());
			return null;
		}

実はhibernate自体はhqlを使わないクエリークラスCriteriaを提供していますが、興味があれば自分で勉強することができます.私は先入観があり、自分で書いたものが好きです.状況を見てみましょう.