基于分页的通用类
StringUtils 类
package com.eight.dao;
public class StringUtils {
// 私有的构造方法,保护此类不能在外部实例化
private StringUtils() {
}
/**
* 如果字符串等于null或去空格后等于"",则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isBlank(String s) {
boolean b = false;
if (null == s || s.trim().equals("")) {
b = true;
}
return b;
}
/**
* 如果字符串不等于null或去空格后不等于"",则返回true,否则返回false
*
* @param s
* @return
*/
public static boolean isNotBlank(String s) {
return !isBlank(s);
}
}
pageBean
分页工具类
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 3;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
// 获取前台向后台提交的所有参数
private Map parameterMap;
// 获取上一次访问后台的url
private String url;
/**
* 初始化pagebean
*
* @param req
*/
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));
this.setRows(req.getParameter("rows"));
// 只有jsp页面上填写pagination=false才是不分页
this.setPagination(!"fasle".equals(req.getParameter("pagination")));
this.setParameterMap(req.getParameterMap());
this.setUrl(req.getRequestURL().toString());
}
public int getMaxPage() {
return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
}
public int nextPage() {
return this.page < this.getMaxPage() ? this.page + 1 : this.getMaxPage();
}
public int previousPage() {
return this.page > 1 ? this.page - 1 : 1;
}
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
this.page = StringUtils.isBlank(page) ? this.page : Integer.valueOf(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
this.rows = StringUtils.isBlank(rows) ? this.rows : Integer.valueOf(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
public Map getParameterMap() {
return parameterMap;
}
public void setParameterMap(Map parameterMap) {
this.parameterMap = parameterMap;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination
+ ", parameterMap=" + parameterMap + ", url=" + url + "]";
}
}
BaseDao
package com.eight.dao;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.omg.CORBA.OBJ_ADAPTER;
/**
* 作用: 1,将赋值的操作交给basedao处理 2,分页 a,查询出条件的总记录数 b,查询符合条件的某一页记录
*
* @author YGUUU
*
*/
public class BaseDao {
/**
* 赋值的操作
*
* @param query
* 预定义对象
* @param map
* 前台传递过来的参数
*/
public void setParameter(Query query, Map map) {
// query.setParameter("bookName", "%"+book.getBookName()+"%");
if (map == null || map.size() == 0) {
return;
}
Object value = null;
for (Map.Entry 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.setParameter(entry.getKey(), value);
}
}
}
public void ser(Query query, Map map) {
if (map == null && map.size() == 0) {
return;
}
Object value = null;
for (Map.Entry 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.setParameter(entry.getKey(), value);
}
}
}
/**
* select * from t_hibernate_book where book_name like '%圣墟%' countSql = select
* count(*) from (select * from t_hibernate_book where book_name like '%圣墟%') t
* pageSql = sql+" limit ?,?" hibernate中这一步省略
*
* hql = from Book where 1=1 and bookName like '%圣墟%' hql = select
* bookName,price from Book where 1=1 and bookName like '%圣墟%'
*
* 思路: 截取from后面的sql语句,前面拼接select count(*) from转成大写
*/
/**
* 查询出条件的总记录数
*
* @param hql
* @return
*/
public String getCountHql(String hql) {
int indexOf = hql.toUpperCase().indexOf("FROM");
return "select count(*) " + hql.substring(indexOf);
}
public List executeQuery(Session session, String hql, PageBean pageBean, Map map) {
if (pageBean != null && pageBean.isPagination()) {
// select count(*) from t_hibernate_book where book_name like '%圣墟%'
String countHql = getCountHql(hql);
Query Countquery = session.createQuery(countHql);
this.setParameter(Countquery, map);
// pageBean设置总记录数,用于分页
pageBean.setTotal(Countquery.getSingleResult().toString());
// 查询展示的数据
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();
}
}
}
继承basedao
package com.five.dao;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import com.zking.eight.dao.BaseDao;
import com.zking.eight.dao.PageBean;
import com.zking.eight.dao.StringUtils;
import com.zking.five.entity.Book;
import com.zking.two.util.SessionFactoryUtils;
public class BookDao extends BaseDao{
public List list1(Book book,PageBean pageBean){
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
String hql="from Book where 1=1";
if(StringUtils.isNotBlank(book.getBookName())) {
hql+=" and bookName like :bookName";
}
Query query = session.createQuery(hql);
if(StringUtils.isNotBlank(book.getBookName())) {
query.setParameter("bookName", "%"+book.getBookName()+"%");
}
if(pageBean!=null&&pageBean.isPagination()) {
query.setFirstResult(pageBean.getStartIndex());
query.setMaxResults(pageBean.getRows());
}
List list = query.list();
transaction.commit();
session.close();
return list;
}
public List list2(Book book,PageBean pageBean){
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
Map map=new HashMap<>();
String hql="from Book where 1=1";
if(StringUtils.isNotBlank(book.getBookName())) {
hql+=" and bookName like :bookName";
map.put("bookName", book.getBookName());
}
//没写BaseDao前,你上面写了多少个判断,下面就要写多少个判断加分页的一个判断,写 了BaseDao后,直接调用这个executeQuery 就行了
List list = super.executeQuery(session, hql, pageBean, map);
transaction.commit();
session.close();
return list;
}
public List
测试方法
private BookDao bookDao=new BookDao();
@Test
public void testList1() {
PageBean pageBean=new PageBean();
// pageBean.setPage(2);
Book book=new Book();
book.setBookName("斗罗大陆");
List list1 = this.bookDao.list1(book, pageBean);
for (Book b : list1) {
System.out.println(b);
}
}
@Test
public void testList2() {
PageBean pageBean=new PageBean();
// pageBean.setPage(2);
Book book=new Book();
book.setBookName("斗罗大陆");
List list1 = this.bookDao.list2(book, pageBean);
for (Book b : list1) {
System.out.println(b);
}
}
@Test
public void testList3() {
PageBean pageBean=new PageBean();
pageBean.setPage(2);
Book book=new Book();
// book.setBookName("斗罗大陆");
List
原生sql
hql实现不了的功能,可以考虑使用原生sql
1、多表(5+)联查
2、未配置映射文件中关系
public List
视图映射
场景
select * from 3表联查
创建视图
创建视图实体类(注:实体类要实现 Serializable接口)
package com.zking.eight.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import com.zking.five.entity.Category;
public class BookOrder implements Serializable{
private Integer book_id;
private Integer order_id;
private String bookName;
private String OrderNo;
public Integer getBook_id() {
return book_id;
}
public void setBook_id(Integer book_id) {
this.book_id = book_id;
}
public Integer getOrder_id() {
return order_id;
}
public void setOrder_id(Integer order_id) {
this.order_id = order_id;
}
public String getOrderNo() {
return OrderNo;
}
public void setOrderNo(String orderNo) {
OrderNo = orderNo;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public BookOrder(Integer book_id, Integer order_id, String bookName, String orderNo) {
super();
this.book_id = book_id;
this.order_id = order_id;
this.bookName = bookName;
OrderNo = orderNo;
}
public BookOrder() {
super();
}
@Override
public String toString() {
return "BookOrder [book_id=" + book_id + ", order_id=" + order_id + ", bookName=" + bookName + ", OrderNo="
+ OrderNo + "]";
}
}
*.hbm.xml
?xml version="1.0" encoding="UTF-8"?>
配置hibernate.cfg.xml文件
public List list4(){
Session session = SessionFactoryUtils.getSession();
Transaction transaction = session.beginTransaction();
// Map map=new HashMap();
String hql="from BookOrder";
List list = session.createQuery(hql).list();
transaction.commit();
session.close();
return list;
}