A simple paging implementation (3)

xiaoxiao2021-03-06  58

I started to design this paging implementation in hibernate, because when using hibernate, the results after the query are automatically encapsulated into a list, so it is particularly convenient to use, here I have made a more vulgar implementation, It is only the string type, but most queries are really just a string operation.

Package treeroot.util;

Import net.sf.hibelnate.hibernateException; import treueot.common.dao.abstractBasedao;

/ *** @author treerot * @version 1.0 * @since 2004-9-30 * / public class HibernatePage extends AbstractPage implements Pageable {private String querySql; private String countSql; private String [] parameters;

public HibernatePage (int currentPage, String querySql, String countSql, String [] parameters) throws PageException {this (currentPage, Pageable.DEFAULT_PAGESIZE, querySql, countSql, parameters);} public HibernatePage (int currentPage, int pageSize, String querySql, String countSql , String [] parameters) throws PageException {super (currentPage, pageSize); this.querySql = querySql; this.countSql = countSql; this.parameters = parameters; init ();} protected void init () throws PageException {try {this .count = AbstractBasedao.Querycount (Countsql); Int fromNDEX = (this.getcurrentpage () - 1) * this.getpagesize (); int toindex = math.min (this.count, fromNDEX THITPAGESIZE ()); this .result = AbstractBasedao.find (this.querysql, this.parameters, fromNDEX, TOINDEX);} catch (hibernateException E) {throw new pageexception (E.GetMessage ());}}} This class is not very reasonable. Since the query can only accept string parameters, it is enough if only string parameters are required. In addition, the query statement must be a JDBC style parameter (?) Instead of Hibernate style (: =), you have seen the code inside Dao, know why. Look at how to use it (a method of Dao): public Pageable findByName (String name, int currentPage) throws PageException {String countSql = "select count (*) from MyClass as c where c.name like?"; String querySql = "from myclass as c where c.name like?"; string [] parameter = new string [] {name}; return new hibernatePage (currentpage, countsql, querysql, parameter);} This method should be relatively simple, here Given the implementation of QueryCount and Find, I know more superficial to Hibernate, so if there is any improper place, I hope to point out, thank you!

public static int queryCount (String hql, String [] args) throws HibernateException {if (hql == null) throw new NullPointerException (); Object obj = null; Transaction trans = null; Session s = null; try {s = HibernateSessionFactory. CurrentSession (); trans = s.begintractions (); query q = s.createQuery (HQL); if (args! = null) {for (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-111181.html

New Post(0)