hql(通常分页)、视图、原生态sql解析

本文介绍了在BaseDao中使用HQL进行分页查询的方法,包括如何获取最大条目数和设置分页参数。同时探讨了原生态SQL的局限性,如配置困难和业务逻辑复杂,并提出视图作为对原生SQL的一种增强解决方案,适用于业务逻辑复杂且需重用的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建BaseDao的作用

  1. 传参
  2. 通常分页

思路:

  1. 获取最大条目数 select count(*)hql.toupcase.indexof(“from”)
  2. 获取需要的查询的分页数据
    query.setfirstResult
    query.setfirstResult

代码分享:

package com.zking.eight.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.struts2.components.Else;
import org.hibernate.Session;
import org.hibernate.query.Query;

import util.PageBean;


public class BaseDao {
	private void setParameter(Query query,Map<String,Object> map) {
		if(map == null || map.size() == 0) {
			return;
		}
		Object value = null;
		for(Map.Entry<String, Object> entry:map.entrySet()) {
			value = entry.getValue();
			if(value instanceof Collection) {
				query.setParameterList(entry.getKey(), (Collection) value);
			}
			else if(value instanceof Object[]) {
				query.setParameterList(entry.getKey(), (Object[]) value);
			}
			else {
				query.setParameterList(entry.getKey(), (Collection) value);
			}
		}
	}
	//from 关键字转换成小写
	public String getCountHql(String hql) {
		int index = hql.toUpperCase().indexOf("FROM");
		return "select count(*)"+ hql.substring(index);
		
	}

	public List executeQuery(String hql,PageBean pageBean,Map<String , Object>map,Session session) {
		//判断需不需要分页
		if(pageBean !=null && pageBean.isPagination()) {
			String countHql = getCountHql(hql);
			Query countQuery = session.createQuery(countHql);
			this.setParameter(countQuery, map);
			String total = countQuery.getSingleResult().toString();
			pageBean.setTotal(total);
		//	
			Query pageQuery = session.createQuery(hql);
			this.setParameter(pageQuery, map);
			pageQuery.setFirstResult(pageBean.getStartIndex());
			pageQuery.setMaxResults(pageBean.getRows());
			return pageQuery.list();
		}
		else {
		//不需要分页,查询所有	
			Query query = session.createQuery(hql);
			this.setParameter(query, map);
			return query.list();
		}
	}

	public List eQy(String hql,PageBean pageBean,Map<String , Object> map,Session session) {
		if(pageBean !=null && pageBean.isPagination()) {
			String countHql = getCountHql(hql);
			Query countQuery = session.createQuery(countHql);
			this.setParameter(countQuery, map);
			String total = countQuery.getSingleResult().toString();
			pageBean.setTotal(total);
			
			Query pageQuery = session.createQuery(hql);
			this.setParameter(pageQuery, map);
			pageQuery.setFirstResult(pageBean.getStartIndex());
			pageQuery.setMaxResults(pageBean.getRows());
			return pageQuery.list();

		}
		else {
			Query query = session.createQuery(hql);
			this.setParameter(query, map);
		    return query.list();
		}
	}
}

2、原生态sql
1、关系难配置
2、业务逻辑复杂

3、视图(对原生态sql增强)
业务逻辑复杂并重用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值