http://blog.youkuaiyun.com/qiyuexuelang/article/details/17420297
- package com.sxdf.manage.dao.impl;
- import java.util.ArrayList;
- import java.util.List;
- import javax.annotation.Resource;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.stereotype.Component;
- import com.sxdf.manage.dao.SnippetDao;
- @Component("snippetDao")
- public class SnippetDaoImpl implements SnippetDao {
- @Resource(name = "sessionFactory")
- private SessionFactory hu;
- //统计总数(你可以使用count(*),前提是使用另一个HQL语句)
- public int count(final String hql, final Object[] param) {
- int count = 0;
- Session session = hu.getCurrentSession();
- Query query = session.createQuery(hql);
- if (param.length > 0) {
- for (int i = 0; i < param.length; i++) {
- query.setString(i, param[i].toString());
- }
- }
- List list = query.list();
- if (list.size() > 0) {
- count = list.size();
- }
- return count;
- }
- //查找当前页数据
- public List<?> findPublic(final String hql, final Object[] param,
- final int start, final int limit) {
- Session session = hu.getCurrentSession();
- Query query = session.createQuery(hql);
- if (param.length > 0) {
- for (int i = 0; i < param.length; i++) {
- query.setString(i, param[i].toString());
- }
- }
- List<?> list = query.setFirstResult(start).setMaxResults(limit).list();
- return list;
- }
- //得到后半段hql
- public String getHQL(Object[][] ob, Object[][] like
- ,String[][] datetime
- , String[] group,String[] asc, String[] desc) {
- StringBuffer hql = new StringBuffer();
- String ss = null;
- if (null != ob) {
- for (Object[] o : ob) {
- // [key][value] name='value'
- boolean b = ((null != o[1]) && (null != o[0])
- && (!"".equals(o[1].toString()))
- && (!"".equals(o[0].toString())));
- if (b) {
- hql.append("and j." + o[0] + " =? ");
- }
- }
- }
- if (null != like) {
- for (Object[] l : like) {
- // [key][value] name like '%value%'
- boolean bl = ((null != l[1]) && (null != l[0])
- && (!"".equals(l[1].toString()))
- && (!"".equals(l[0].toString())));
- if (bl) {
- hql.append("and j." + l[0] + " LIKE? ");
- }
- }
- }
- //时间(模糊)
- if (null != datetime) {
- for (String[] d : datetime) {
- // [key][value]
- boolean bd = ((null != d[1]) && (null != d[0])
- && (!"".equals(d[1])) && (!"".equals(d[0]
- .toString())));
- if (bd) {
- hql.append("and to_char(j."+ d[0] +",'yyyy-mm-dd')" + " LIKE ? ");
- }
- }
- }
- // 分组 group by [value]
- if (null != group && group.length > 0) {
- StringBuffer groupb = new StringBuffer();
- for (String str1 : group) {
- if (null != str1 && !"".equals(str1)) {
- groupb.append("j." + str1 + " , ");
- }
- }
- if (null != groupb && "".equals("")) {
- hql.append("group by ");
- hql.append(groupb.substring(0, groupb.lastIndexOf(",")));
- }
- }
- // 升序//降序(先升后降) order by [value]
- if (null != asc && asc.length > 0 || null != desc && desc.length > 0) {
- String oy1 = null;
- String oy2 = null;
- // orderb.append("order by ");
- if (null != asc && asc.length > 0) {
- StringBuffer orderb1 = new StringBuffer();
- for (String str2 : asc) {
- if (null != str2 && !"".equals(str2)) {
- orderb1.append("j." + str2 + " , ");
- }
- }
- if (null != orderb1 && "".equals(orderb1)) {
- oy1 = orderb1.substring(0, orderb1.lastIndexOf(","))
- + " asc ";
- }
- }
- if (null != desc && desc.length > 0) {
- StringBuffer orderb2 = new StringBuffer();
- for (String str3 : desc) {
- if (null != str3 && !"".equals(str3)) {
- orderb2.append("j." + str3 + " , ");
- }
- }
- if (null != orderb2 && !"".equals(orderb2)) {
- oy2 = orderb2.substring(0, orderb2.lastIndexOf(","))
- + " desc ";
- }
- }
- if ((null != oy2 && !"".equals(oy2))
- || (null != oy1 && !"".equals(oy1))) {
- hql.append("order by ");
- if (null != oy1 && !"".equals(oy1)) {
- hql.append(oy1);
- }
- if ((null != oy2 && !"".equals(oy2))
- && (null != oy1 && !"".equals(oy1))) {
- hql.append(" , ");
- }
- if (null != oy2 && !"".equals(oy2)) {
- hql.append(oy2);
- }
- }
- }
- if (hql.toString().contains("and")) {
- ss = hql.substring(4, hql.length());
- }
- return ss;
- }
- //选值(=、like)
- public List<Object> getValue(Object[][] ob, Object[][] like,String[][] datetime) {
- List<Object> list = new ArrayList<Object>();
- if (null != ob) {
- for (Object[] o : ob) {
- // [key][value]
- boolean b = ((null != o[1]) && (null != o[0])
- && (!"".equals(o[1].toString())) && (!"".equals(o[0]
- .toString())));
- if (b) {
- list.add(o[1]);
- }
- }
- }
- if (null != like) {
- for (Object[] l : like) {
- // [key][value]
- boolean bl = ((null != l[1]) && (null != l[0])
- && (!"".equals(l[1].toString())) && (!"".equals(l[0]
- .toString())));
- if (bl) {
- list.add("%" + l[1] + "%");
- }
- }
- }
- //时间(模糊)
- if (null != datetime) {
- for (String[] d : datetime) {
- // [key][value]
- boolean bd = ((null != d[1]) && (null != d[0])
- && (!"".equals(d[1])) && (!"".equals(d[0]
- .toString())));
- if (bd) {
- list.add("%" + d[1] + "%");
- }
- }
- }
- return list;
- }
- }
由于删掉一些其他无关的类方法,故可能存在 一些无用的import引入,对代码无影响
总结一下:本文大量使用if-else的运用,大大降低阅读效果。 如果大家有好的方法和建议,洗耳恭听。
对sessionFactory 使用注解,
其中本类所继承的SnippetDao为上一篇中特意介绍的Dao接口方法,详细说明请参考上文-- SSH动态查询具体实现之service
Action 调取示例:
首先引入SnippetService
- @Resource(name = "snippetServiceImpl")
- private SnippetService ss;
接下来,在action方法利用本接口实现你想要的查询
- Object[][] aa1 =null;
- Object[][] aa2 =null;
- String[] group =null;
- String[] asc =null;
- String[] desc = null;;
- ActionWriteUtil.writeStr(JsonUtil.getPurchaseJson((List<Purchase>)ss.search(Purchase.class, aa1,aa2 ,null,group,asc,desc,start, limit),
- ss.count(Purchase.class, aa1,aa2,null,group,asc,desc)));
针对任何bean,无需在其service或dao层再写查询方法(对bean属性*查询, 比较、平均值等特殊查询除外)