Use of Paging Plug-in Page Helpler (in ssm framework) Server-side Paging

Keywords: SQL Database github xml


1. maven dependence:

	<!-- jPaginate -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>3.4.2-fix</version>
		</dependency>



2. Configure in sqlMapConfig.xml:

	<! - Configure Paging Plug-ins - >
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<! - Set up six database types: Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL - >.        
        	<property name="dialect" value="mysql"/>
        	<! - This parameter defau lt s to false - >.  
            <! - When set to true, the first parameter offset of RowBounds will be used as pageNum page number - >  
            <! - The same page Num effect as in startPage - >.  
            <property name="offsetAsPageNum" value="true"/>  
            <! - This parameter defau lt s to false - >. 
            <! - When set to true, count queries are performed using RowBounds Pages - > The  
            <property name="rowBoundsWithCount" value="true"/>  
            <! - Version 3.3.0 is available - Paging parameters are rationalized, default false is disabled - >  
            <! - When rationalization is enabled, pageNum < 1 will query the first page, and pageNum > pages will query the last page - >  
            <! - When rationalization is disabled, empty data is returned if pageNum < 1 or pageNum > pages  
            <property name="reasonable" value="true"/>  
            <! - Support the transfer of paging parameters through Mapper interface parameters - >.  
            <property name="supportMethodsArguments" value="true"/>  
            <! - Always returns PageInfo type, check s to see if the return type is PageInfo, and none returns Page - >.  
            <property name="returnPageInfo" value="check"/>  
		</plugin>
	</plugins>




3. Query sql is written unchanged, but not paginated:

	<!-- Query Administrator Information -->
	<select id="selectAuthAdmin" parameterType="AuthAdmin" resultMap="authAdminResultMap">
		SELECT 
			ID, 
		    ACCOUNT,
		    PASSWORD,
		    SALT,
		    ROLE_IDS,
		    ROLE_NAME,
		    RES_IDS,
		    LOCKED
		FROM TS_AUTH_ADMIN 		
	</select>

4. Service Impl receives the results from mapper.xml and uses page helper paging.

pagehelper determines the database to be used according to the configuration, automatically intercepts the corresponding paging statements in SQL spelling and executes sql.

The input sql can be viewed from the console with paging statements.


Parameter pageNum - Page N, pageSize - Number of M entries per page

For example: PageHelper.startPage(1, 10); that is, Articles 1 to 10, database data less than 10, how many returns.

	/**
	 * Paging for User Information
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Override
	public PageInfo<AuthAdmin> selectAuthAdminByPage(Integer pageNum, Integer pageSize ,AuthAdmin adm)
			throws Exception {
		
		PageHelper.startPage(pageNum, pageSize);
		// All user information
		List<AuthAdmin> authAdmins = _authAdminMapper.selectAuthAdmin(adm);
		if(authAdmins == null){
			return null;
		}
		PageInfo<AuthAdmin> pageInfo = new PageInfo<AuthAdmin>(authAdmins);
		return pageInfo;
	}


5.controller gets the paginated data: (It doesn't matter where the final data is, controller and service can do it, just encapsulate it as a pageInfo object, and get it from this object.)

	// Loading User List
	@ResponseBody
	@RequestMapping("/adminList")
	public Object adminList(Model model, int draw, int start, int length ,AuthAdmin adm)throws Exception{
		
		_logger.info("+++++++++++++++++++++++  Show a list of users   +++++++++++++++++++++++");
		PageInfo<AuthAdmin> pageInfo = _authAdminService.selectAuthAdminByPage((start/length)+1 ,length ,adm);
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> data = new ArrayList<Object>();
		for(AuthAdmin admin : pageInfo.getList()){
			Map<String, Object> obj = new HashMap<String, Object>();
			obj.put("id", admin.getId());
			obj.put("account", admin.getAccount());
			obj.put("locked", admin.getLocked());
			obj.put("roleIds", admin.getRoleIds());
			obj.put("roleName", admin.getRoleName());
			obj.put("resIds", admin.getResIds());
			data.add(obj);
		}
		
		map.put("draw", draw);
		map.put("recordsTotal", pageInfo.getTotal());
		map.put("recordsFiltered", pageInfo.getTotal());
		map.put("data", data);
		return map;
	}
	

As shown above, pageInfo.getList() can get the data after paging. pageInfo.getTotal() gives the total number of records.




Another blogger suggested this plug-in: http://www.cnblogs.com/xiaoxinwt/p/5329840.html







Posted by igul222 on Mon, 01 Apr 2019 17:27:29 -0700