设为首页 加入收藏

TOP

实现按条件查询(一)
2015-07-24 11:35:33 来源: 作者: 【 】 浏览:5
Tags:实现 条件 查询

第一步:

定义接口:

public interface ICommonDao {

List findCollectionByConditionNoPage(String codition,Object[] params, Map orderby);

}

第二步:
实现接口的类:

Class entityClass = TUtils.getTClass(this.getClass());

public class TUtils {

	/**泛型转换,目的获取子类传递父类的真实类型,也就是T所对应的类型*/
	public static Class getTClass(Class entity) {
		ParameterizedType type = (ParameterizedType)entity.getGenericSuperclass();
		Class entityClass = (Class) type.getActualTypeArguments()[0];
		return entityClass;
	}
}?
/**指定查询条件查询对应的结果,返回List(不分页)*/
	/**
	 * FROM ElecText o WHERE 1=1        #Dao层
		AND o.textName LIKE '%张%'	#Service层
		AND o.textRemark LIKE '%张%'    #Service层
		ORDER BY o.textDate ASC,o.textName DESC  #Service层
	 */
	public List findCollectionByConditionNoPage(String condition,
			final Object[] params, Map orderby) {
		//定义hql语句
		String hql = "FROM "+entityClass.getSimpleName()+" o WHERE 1=1";
		//定义排序语句
		String orderbyHql = this.orderbyHql(orderby);
		//定义最终的语句
		final String finalHql = hql + condition + orderbyHql;
		//执行语句一
		//List list = this.getHibernateTemplate().find(finalHql, params);
		//执行语句二
//		SessionFactory sf = this.getHibernateTemplate().getSessionFactory();
//		Session s = sf.getCurrentSession();
//		Query query = s.createQuery(finalHql);
//		List list = query.list();
		//执行语句三
		List list = this.getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(finalHql);
				if(params!=null && params.length>0){
					for(int i=0;i orderby) {
		StringBuffer buffer = new StringBuffer("");
		if(orderby!=null && orderby.size()>0){
			buffer.append(" order by ");
			for(Map.Entry map:orderby.entrySet()){
				buffer.append(map.getKey()+" "+map.getValue()+",");
			}
			//删除最后一个逗号
			buffer.deleteCharAt(buffer.length()-1);
		}
		return buffer.toString();
	}

下面是根据不同的业务来编写不同的代码。

第三步:

service接口:

public interface IElecTextService {
	public static final String SERVICE_NAME = "com.itheima.elec.service.impl.ElecTextServiceImpl";
	
	List findCollectionByConditionNoPage(ElecText elecText);
}

第四步:

service实现

/**指定查询条件查询对应的结果,返回List*/
	/**
	 * FROM ElecText o WHERE 1=1        #Dao层
		AND o.textName LIKE '%张%'	#Service层
		AND o.textRemark LIKE '%张%'    #Service层
		ORDER BY o.textDate ASC,o.textName DESC  #Service层
	 */
	public List findCollectionByConditionNoPage(ElecText elecText) {
		//查询条件
		String condition = "";
		List paramsList = new ArrayList();
		//判断是否添加查询条件
		if(StringUtils.isNotBlank(elecText.getTextName())){
			condition += " and o.textName like ?";
			paramsList.add("%"+elecText.getTextName()+"%");
		}
		if(StringUtils.isNotBlank(elecText.getTextRemark())){
			condition += " and o.textRemark like ?";
			paramsList.add("%"+elecText.getTextRemark()+"%");
		}
		Object [] params = paramsList.toArray();
		//排序语句(hql语句和sql语句的排序是有顺序的
		Map orderby = new LinkedHashMap();
		orderby.put("o.textDate", "asc");
		orderby.put("o.textName", "desc");
		//查询
		List list = elecTextDao.findCollectionByConditionNoPage(condition,params,orderby);
		return list;
	} 
 


第五步:

/**模拟Action
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇【MongoDB】Mongodb数据库之Cappe.. 下一篇学生表sid,sname,成绩表cid,cname..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)