In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how SSH implements conditional query and paging query. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. QueryHelper and PageResult
QueryHelper is the helper class for HQL queries, and PageResult is the result class for paging queries.
QueryHelper.java
Package com.rk.core.utils;import java.util.ArrayList;import java.util.List;public class QueryHelper {/ from clause private String fromClause = "; / / where clause private String whereClause ="; / / order by clause private String orderByClause = ""; private List parameters; / / sort order public static String ORDER_BY_DESC = "DESC" / / descending public static String ORDER_BY_ASC = "ASC"; / / Ascending / * * construct from clause * @ param clazz entity class * @ alias corresponding to param alias entity class * / public QueryHelper (Class clazz,String alias) {fromClause = "FROM" + clazz.getSimpleName () + "" + alias } / * construct where clause * @ param condition query condition statement For example, the corresponding query condition value in i.title like? * @ param params query condition statement; for example:% title% * / public void addCondition (String condition, Object...) Params) {if (whereClause.length () > 1) {/ / non-first query condition whereClause + = "AND" + condition;} else {/ / first query condition whereClause + = "WHERE" + condition } / / set the query condition value to if (parameters = = null) {parameters = new ArrayList () in the query condition value set } if (params! = null) {for (Object param: params) {parameters.add (param) } / * construct order by clause * @ param property sort attribute For example: i.createTime * @ param order sort order For example: DESC or ASC * / public void addOrderByProperty (String property, String order) {if (orderByClause.length () > 1) {/ / non-first sort attribute orderByClause + = "," + property + "" + order } else {/ / first sort attribute orderByClause = "ORDER BY" + property + "" + order;}} / / query hql statement public String getQueryListHql () {return fromClause + whereClause + orderByClause } / / hql statement public String getQueryCountHql () {return "SELECT COUNT (*)" + fromClause + whereClause;} / / query the corresponding set of query condition values public List getParameters () {return parameters;}} in the query hql statement
QueryHelper analysis: consists of three parts
(1) from clause
(2) where clause
(3) order by clause
PageResult.java
Package com.rk.core.entity;import java.util.ArrayList;import java.util.List;public class PageResult {/ / Total records private long totalCount; / / current page number private int pageNo; / / total pages private int totalPageCount; / / page size private int pageSize; / / list record private List items / / avoid paging too large or too small public static final int MAX_PAGE_SIZE = 100; public static final int MIN_PAGE_SIZE = 3; public PageResult (long totalCount, int totalPageCount,int pageSize, int pageNo, List items) {this.totalCount = totalCount; this.totalPageCount = totalPageCount; this.pageSize = pageSize This.pageNo = pageNo; this.items = items;} public long getTotalCount () {return totalCount;} public void setTotalCount (long totalCount) {this.totalCount = totalCount;} public int getPageNo () {return pageNo } public void setPageNo (int pageNo) {this.pageNo = pageNo;} public int getTotalPageCount () {return totalPageCount;} public void setTotalPageCount (int totalPageCount) {this.totalPageCount = totalPageCount;} public int getPageSize () {return pageSize } public void setPageSize (int pageSize) {this.pageSize = pageSize;} public List getItems () {return items;} public void setItems (List items) {this.items = items;}}
Analysis of PageResult:
(1) Total number of records totalCount (obtained from the database according to the sql statement)
(2) the size of each page pageSize (generally specified by the front desk)
(3) the total number of pages totalPageCount obtained from (1) and (2) (calculated)
(4) judge whether the pageNo of the current page is reasonable (whether it is less than 1, whether it is greater than totalPageCount); if not, correct it
(5) fetch the data items of the current page
2. Use QueryHelper
Using QueryHelper is mainly implemented at the dao level, while the service layer only invokes the functions of the next layer (dao layer).
2.1Tier of Dao
BaseDao.java
Package com.rk.core.dao;import java.io.Serializable;import java.util.List;import com.rk.core.entity.PageResult;import com.rk.core.utils.QueryHelper;public interface BaseDao {/ / add void save (T entity); / / update void update (T entity); / / delete void delete (Serializable id) according to id; / / find T findById (Serializable id) based on id / / find list List findAll (); / / conditional query entity list List findList (String hql, List parameters); / / conditional query entity list-query assistant queryHelper List findList (QueryHelper queryHelper); / / paging condition query entity list-query assistant queryHelper PageResult getPageResult (QueryHelper queryHelper, int pageNo, int pageSize);}
The methods added are
/ / conditional query entity list List findList (String hql, List parameters); / / conditional query entity list-query assistant queryHelper List findList (QueryHelper queryHelper); / / paging condition query entity list-query assistant queryHelper PageResult getPageResult (QueryHelper queryHelper, int pageNo, int pageSize)
BaseDaoImpl.java
Package com.rk.core.dao.impl;import java.io.Serializable;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.rk.core.dao.BaseDao;import com.rk.core.entity.PageResult;import com.rk.core.utils.HibernateConfigurationUtils;import com.rk.core.utils.QueryHelper;public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {private Class clazz @ SuppressWarnings ("unchecked") public BaseDaoImpl () {ParameterizedType pt = (ParameterizedType) this.getClass () .getGenericSuperclass (); Type [] args = pt.getActualTypeArguments (); this.clazz = (Class) args [0];} public void save (T entity) {getHibernateTemplate () .save (entity) } public void update (T entity) {getHibernateTemplate () .update (entity);} public void delete (Serializable id) {String identifierPropertyName = HibernateConfigurationUtils.getIdentifierPropertyName (clazz); Session session = getSession () Session.createQuery ("delete from" + clazz.getSimpleName () + "where" + identifierPropertyName + "=?") .setParameter (0, id) .executeUpdate ();} public T findById (Serializable id) {return getHibernateTemplate () .get (clazz, id) } @ SuppressWarnings ("unchecked") public List findAll () {Session session = getSession (); Query query = session.createQuery ("from" + clazz.getSimpleName ()); return query.list ();} public List findList (String hql, List parameters) {Query query = getSession () .createQuery (hql) If (parameters! = null) {for (int iTun0) I previous page previous page next page Next page to
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.