Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Hibernate DAO class

Shulou Source: shulou.com Published: 2022-06-02 08:00:20 09月15日 Update

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!

Tags: Object data page learning query output code content head office situation server structure service usage interest parameter client client that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Redmi MySQL Shulou Information