In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the Hibernate DAO class". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Hibernate DAO class".
There are two structures in Struts pagination:
1. In Action, all the records are queried by DAO, then added to the session or request object, passed to the client, and paged by JSP. This method is very convenient when the amount of data is small, and does not affect the speed.
two。 In Action, only one page of records are queried through DAO at a time, and then passed to the JSP page. This structure is good for programs with a large amount of data, but for a small amount of data, it will increase the request to the server and increase the load on the server.
1). Hibernate DAO class
Package com.jpcf.db.dao; import com.jpcf.db.model.*; import com.jpcf.db.helper.HibernateUtil; import net.sf.hibernate.*; import java.util.*; import com.jpcf.db.controller.*; public class VehiclePropertyDAO {public Collection findWithPage (int pageSize, int startRow) throws HibernateException {Collection vehicleList = null; Transaction tx = null; try {Session session = HibernateUtil.currentSession (); tx = session.beginTransaction (); Query Q = session.createQuery ("from VehicleProperty vp") Q.setFirstResult (startRow); q.setMaxResults (pageSize); vehicleList = q.list (); tx.commit ();} catch (HibernateException he) {if (tx! = null) {tx.rollback ();} throw he;} finally {HibernateUtil.closeSession ();} return vehicleList;} public int getRows (String query) throws HibernateException {int totalRows = 0; Transaction tx = null; try {Session session = HibernateUtil.currentSession (); tx = session.beginTransaction () TotalRows = (Integer) session.iterate (query). Next (). IntValue (); tx.commit ();} catch (HibernateException he) {if (tx! = null) {tx.rollback ();} throw he;} finally {HibernateUtil.closeSession ();} return totalRows;}}
I will post the code needed for paging in the Hibernate DAO class. "from VehicleProperty vp" can also be passed in with a parameter. If you are interested, change it yourself.
2). Action
Here is the code used in Action:
Public ActionForward queryWithPage (ActionMapping actionMapping, ActionForm actionForm HttpServletRequest httpServletRequest, HttpServletResponse httpServletresponse) {Collection clInfos = null;// used to output to the page the total rows of int totalRows;// records VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO (); / / get the total number of rows in the current table try {totalRows = vehicleDAO.getRows ("select count (*) from VehicleProperty");} catch (Exception ex) {servlet.log (ex.toString ()); return actionMapping.findForward (Constants.FAILURE) } / / get the pager object Pager pager=PagerHelper.getPager (httpServletRequest,totalRows) for output to the page through the PagerHelper class; / / fetch the pageSize line record try {clInfos = vehicleDAO.findWithPage (pager.getPageSize (), pager.getStartRow ()) from startRow;} catch (Exception ex) {servlet.log (ex.toString ()); return actionMapping.findForward (Constants.FAILURE) } / / Save the output recordset and pager object to the request object httpServletRequest.setAttribute ("CLINFOS", clInfos); httpServletRequest.setAttribute ("PAGER", pager); return actionMapping.findForward (Constants.SUCCESS);}
The query statement select count (*) from VehicleProperty can also be changed into any condition you need (select count (*) from VehicleProperty where.)
Thank you for your reading, the above is the content of "how to use the Hibernate DAO class", after the study of this article, I believe you have a deeper understanding of how to use the Hibernate DAO class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.