- BaseDAO
sql和hql通用分页代码的区别:
/**
- 分页: jdbc: --> executeQuery(String sql,PageBean pagebean,Class clz)
- sql:
- select * from t_hibernate_book where book_name like ‘%?%’
- select * from t_hibernate_book where book_name like ‘%圣墟%’
- countSql = select count(*) from (sql) t;
- 分页: 1、sql–>countSql–>total–>pageBean
-
2、sql-->pageSql-->result
-
3、处理结果集
- hql:
- 0、select * from Book where bookName like ‘%:bookName%’ select count(*)
- sql from From frOm
- 1、hql–>countHql–>total–>pageBean
- 2、sql–>pageSql–>result(hibernate掉用内置接口自动生成分页语句,查询结果)
- 3、不需要
*/
通过书籍名字模糊查询数据,并且具备分页的功能
hql通用分页代码方法分享:
public class BaseDao {
/**
* 判断Objiect是什么类型,
* 给query赋值
* @param query
* @param map
*/
private void setParameter(Query query,Map<String, Object> map) {
if(map == null || map.size() == 0) {
return;
}
Object value = null;
// 给query赋值
for (Map.Entry<String, Object> entry : map.entrySet()) {
value = entry.getValue();//赋值给value
//集合类型
if(value instanceof Collection) {
query.setParameterList(entry.getKey(),(Collection) value);
}
//数组类型
else if(value instanceof Object[]) {
query.setParameterList(entry.getKey(),(Object[]) value);
}
//其他
else{
query.setParameter(entry.getKey(), value);
}
}
}
/**
* 获取最大条目数
* @param hql
* @return
*/
private String getCountHql(String hql) {
//把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);//获取hql语句
Query countquery = session.createQuery(countHql);//执行hql语句
this.setParameter(countquery, map);//给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();
}
}
}
-
原生sql
hql实现不了的功能,可以考虑使用原生sql1、配置关系麻烦
2、复杂sql查询需求:统计订单表中的订单对应的所有订单项中,某一书籍的销量。
select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oi
where o.orderid = oi.oid and b.bid = oi.product_idsql->hql
from Book
-
视图映射
select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oifunca(){ select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oi where bname like '%?%' } funca(){ select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oi where order_no > 343 } funca(){ select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oi where product_id = 1213 } funca(){ slect * from DDD,(select oi.selfNo,t.bname from t_hibernate_book b,t_hibernate_Oerder o,t_hibernate_Oerder_item oi) }
两张表 hql
三张以上 原生sql、视图、存储过程
SELECT @rowno:=@rowno + 1 AS rowno,a.* FROM tableName a,(SELECT @rowno:=0) b