Spring + Hibernate complex query pagination

xiaoxiao2021-04-03  224

There are many paging examples on the Internet, or not, you can't meet the requirements of complex queries, the following is a primary version written, give you a reference.

/ ** Page package

Very simple :)

* /

Package com.xing.cms.model.util;

Public class facower = 0; private int totalobjects = 0; private int pagenumber = 1; private int pageSize = 3; private boolean pageable = true; private int firstresult;

public void calc () {? totalPages = totalObjects% pageSize == 0 totalObjects / pageSize: totalObjects / pageSize 1; firstResult = (pageNumber - 1) * pageSize;} public boolean isPageAble () {return pageAble;}

Public void setpageable (Boolean Page "{this.pageable = pageable;}

Public int gettotalobjects () {return totalObjects;}

Public void settotalobjects (int param) {this.totalObjects = param;}

Public int gettotalpages () {returnitpages;

Public void settotalpages (int param) {this.totalpages = param;}

Public int getpagenumber () {return pagenumber;

Public void setpagenumber (int pagenumber) {this.pagenumber = Pagenumber;

Public int getpagesize () {return pagesize;}

public void setPageSize (int pageSize) {this.pageSize = pageSize;} public String toString () {return ( "/ ntotalPages:" totalPages "/ ntotalObjects:" totalObjects "/ npageNumber:" pageNumber "/ npageSize : " PageSize " / NPageable: " Pageable " / NFirstResult: " firstResult);}

Public int getFirstResult () {return firstResult;}

Public void setfirstResult (int firstresult) {this.firstresult = firstResult;}}

/ * Userdao interface * /

Package com.xing.cms.model.da;

Import java.util.list;

import com.xing.cms.model.businessobject.User; import com.xing.cms.model.util.Pager; public interface UserDao {public abstract boolean save (User model); public abstract boolean update (User model); public abstract User get (int ID); int ID); public abstract void truncate (); public abstract boolean delete (int [] id); public abstract list find (String [] [] Searchpara, Pager PAGER); public abstract list find (String [] [] searchpara);

/ * Userdao implementation * /

Package com.xing.cms.model.dao.cms.model.dao.impl;

Import java.sql.sqlexception; import java.util.list;

import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; // import org.hibernate.criterion.Expression; import org.hibernate .criterion.Restrictions; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm. Hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.hibernatedaosupport;

Import com.xing.cms.model.businessObject.user; import com.xing.cms.model.dao.cms.model.util.Generaltool; Import com.xing.cms.model.util. Pager;

Public Class UserdaoImpl Extends Hibernatedaosupport IMPLEments Userdao {

Private JDBCTemplate JDBCTemplate; // Spring Injection

Public userDaoImpl () {super ();

Public JDBCTemplate GetjdbcTemplate () {Return JDBCTemplate;}

Public void setjdbctemplate (jdbctemplate param) {this.jdbcTemplate = param;}

@Suppresswarnings ("unchecked") public list Find (Final String [] [] Searchpara, Final Pager Pager) {

return (List ) this.getHibernateTemplate () execute (new HibernateCallback () {public Object doInHibernate (Session session) throws SQLException, HibernateException {Criteria ca_count = null;. if (pager.isPageAble ()) ca_count = session.createCriteria (User.class); // Do not avoid it, you need to query the two databases, this acquisition criteria ca = session.createcriteria (user.class); if (SearchPara! = Null) {ca.setProjection (null); for (int); i = 0; i

IF (SearchPara [I] [1] .Equalsignorecase ("=")) {if (Pager.Ispageable ()) ca_count.add (restricts.eq (searchpara [i] [0], searchpara [i] [2]) ); Ca.add (restrictions.eq (SearchPara [i] [0], searchpara [i] [2]));} else if (SearchPara [i] [1] .EqualsignoreCase ("like") {ix ( Pager.ispageable ()) ca_count.add (restricts.like (Searchpara [i] [0], Searchpara [i] [2], matchmode.Anywhere); ca.add (restricts.like (SearchPara [i] [0 ], Searchpara [i] [2], matchmode.anyywhere);

}}} List return_list;

IF (Pager.Ispageable ()) {

Pager.SetTotalObjects ((Integer) (Projections.rowcount ()) .UniQueResult ())). INTVALUE ()); // Get count

Pager.calc (); ca.ddorder ("UID")); ca.setfirstResult (); ca.setMaxResults (Pager.getPageSize ()); RET_LIST = ca.list (); } else {ca.addorder (Order.desc ("UID")); RET_LIST = ca.list ();

Return RET_LIST;}});

// ... other methods

/ * Test class * /

package test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; import com.xing.cms.model.businessobject.User; import com. Xing.cms.model.dao.documentdao; import com.xing.cms.model.dao.cms.Model.util.htmlGenerator; Import com.xing.cms.model.util.pager; import com.xing.cms.model.util.UtilXml; import com.xing.cms.action.UserAction; public class TestSpring extends AbstractTransactionalDataSourceSpringContextTests {private Log logger = LogFactory.getLog (TestSpring.class);

Public string [] getconfiglocations () {string [] config = new string [] {"ApplicationContext.xml"}; return config;

public void testUserAction () throws Exception {UserDao dao = (UserDao) applicationContext.getBean ( "userDao"); assertTrue (dao = null!); User user = (User) applicationContext.getBean ( "user"); assertTrue (user! = NULL); Pager Pager = (PAGER) ApplicationContext.getBean ("Pager"); asserttrue; useraction action = (userAction) ApplicationContext.getBean ("useerAction"); asserttrue (action! = null); // Clear all first dao.truncate (); //super.setcomplete (); // add for (int i = 0; i <20; i ) {user u = new user (); u.SETGID (1) U.SETUSERNAME ("xzw _" i); u.SetPassword ("abcd"); DAO.SAVE (U);} // no Pager / * assertequals (0, DAO.FIND (New String [] [] { New string [] {"UserName", "=", "xzw"}}) .size ()); assertequals (20, dao.find (null) .size ()); * / // with Pager // Action .GETPAGER (). setPageSize (9); // 10 11 12 ... 19 1 = 11 / * Page, take the first page, the first 4, should return 4 * / action.getPager (). setPageSize (4 ); Assertequals (4, dao.find (new string "[] {new string [] {" username "," like "," x ZW_1 "}}, action.getpager ()). size ()); / * Do not pagin, take all, should return 20 * / action.getPager (). setPageAble (false); action.getpager (). setPageSize (4); Assertequals (11, DAO.Find (new string "[] {new string [] {" username "," like "," xzw_1 "}}, action.getpager ()). Size ());

}

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

New Post(0)