The basic Dao Layer Based on Struts2, Spring, and Hibernate that contains multi-condition query pages helps implement the jar package and struts2dao

Source: Internet
Author: User

The basic Dao Layer Based on Struts2, Spring, and Hibernate that contains multi-condition query pages helps implement the jar package and struts2dao

Common Operations for operating databases are addition, deletion, query, and modification. It is too troublesome and unnecessary to write these operations once every time. It is too painful to Write multi-condition queries and pagination, so I took the time to write a dao help jar and import it! You don't have to worry about my projects anymore!

Reprinted, please specify the source !!!

Instructions for use:

1. For more information about how to use classes, see the document!

2. Paging jsp writing. You only need to package the paging condition in <form action = "Address" id = "pageForm">, and then set pageView in pageJsp in jar. copy the jsp file and introduce it below the data display !!!

3. You need to configure it in the spring configuration file applicationContext. xml:

<! -- Automatically scan and assemble beans --> <context: component-scan base-package = "com. liang"> </context: component-scan> <! -- Configure declarative Transaction Management (using annotation-based methods) --> <bean id = "txManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "> </property> </bean> <tx: annotation-driven transaction-manager =" txManager "/>


There are various configuration file samples in the config of the jar package. You can refer

Note: <form> the form id must be "pageForm"

: Http://download.csdn.net/detail/liang5630/8133017


The test is as follows:



How do I write the action code for struts2 spring and hibernate paging queries?

1. Define a PageInfo. java paging class:
2. Use hibernate for paging.
3. Call the getPageInfo method directly in the Action and return the PageInfo class.

Public class PageInfo {
Private int totalNum; // The total number of records.
Private int pageNum; // the total number of pages.
Private int current = 1; // current page number
Private int limit = 25; // number of records per page
@ SuppressWarnings ("unchecked ")
Private List result; // page record information
Private int start; // starts to record the index number.
Private int type;

// Private boolean first; // whether it is the first page
// Private boolean last; // whether it is the last page

/**
* @ Return the type.
*/
Public int getType (){
Return this. type;
}

/**
* @ Param type the type to set
*/
Public void setType (int type ){
This. type = type;
}

Public PageInfo (){
Super ();
}

@ SuppressWarnings ("unchecked ")
Public PageInfo (List result, int totalNum, int limit ){
This. result = result;
This. totalNum = totalNum;
This. limit = limit;
}

Public int getTotalNum (){
Return totalNum;
}

Public void setTotalNum (int totalNum ){
This. totalNum = totalNum;
This. getPageNum ();
}

Public int getPageNum (){
If (totalNum <= 0 ){
This. pageNum = 1;
} Else {
This. pageNum = totalNum % limit = 0? TotalNum/limit: totalNum
/Limit + 1;
}
Return this. pageNum;
}

Pu ...... remaining full text>

Hibernate spring multi-condition dynamic query + Paging

// Pagination (current page)
Public List findPage (final Class clazz, final int currentPage, final int pageSize)
Throws Exception {
List listparts (listdomainthis.gethibernatetemplate(cmd.exe cute (new HibernateCallback (){

Public Object doInHibernate (Session arg0)
Throws HibernateException, SQLException {
Query query = arg0.createQuery ("from" + clazz. getName ());
Query. setFirstResult (currentPage-1) * pageSize );
Query. setMaxResults (currentPage * pageSize );
Return query. list ();
}

});
Return list;
}

// Calculate the number of records
Public int getRows (final String hql ){
Long l((long1_this.gethibernatetemplate(cmd.exe cute (new HibernateCallback (){

Public Object doInHibernate (Session arg0)
Throws HibernateException, SQLException {
Return arg0.createQuery (hql). uniqueResult ();
}});
Return l. intValue ();
}
Number of records written in another way
CurrentPage is the current page
PageSize is the number of entries displayed per page.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.