Springboot+Mybatis+PageHelper快速实现分页功能

话不多说,直接上干货:

pom文件:



	4.0.0

	com.zhaojun
	websocket-test
	0.0.1-SNAPSHOT
	jar

	websocket-test
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
		
		
			org.projectlombok
			lombok
			true
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			1.2.3
		
		
			com.alibaba
			druid
			1.0.15
		
		
			org.projectlombok
			lombok
			1.16.18
			provided
		
		
			mysql
			mysql-connector-java
			runtime
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


 

springboot配置文件application.yml:

pagehelper:
  reasonable: true
  support-methods-arguments: true
  params: count=countSql
  helper-dialect: mysql


spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/db_name?useSSL=true&characterEncoding=UTF-8
    username: root
    password: root
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

mybatis:
  mapper-locations: classpath:mapper/**/*.xml

 

controller代码:

package com.zhaojun.user.controller;

import com.zhaojun.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户controller
 *
 * @author ZhaoJun
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Object usersData(int pageNum, int pageSize) {
        return userService.usersData(pageNum, pageSize);
    }
}

service:

package com.zhaojun.user.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaojun.user.dao.UserDao;
import com.zhaojun.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service
 *
 * @author ZhaoJun
 */
@Service
public class UserService  {

    @Autowired
    private UserDao userDao;

    public Object usersData(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List users = userDao.usersData();
        PageInfo appsPageInfo = new PageInfo<>(users);
        return appsPageInfo;
    }
}

 

dao代码:

package com.zhaojun.user.dao;

import com.zhaojun.user.entity.User;

import java.util.List;

/**
 * 用户Dao
 *
 * @author ZhaoJun
 */
public interface UserDao {

    List usersData();

}

daoImpl代码:

package com.zhaojun.user.dao.impl;

import com.zhaojun.user.dao.UserDao;
import com.zhaojun.user.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ZhaoJun
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private SqlSession sqlSession;

    @Override
    public List usersData() {
        return sqlSession.selectList("selectUsers");
    }
}

mapper文件:






    
    
        
        
        
        
        
        
        
    

    

sql文件:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50722
Source Host           : localhost:3306
Source Database       : adamas

Target Server Type    : MYSQL
Target Server Version : 50722
File Encoding         : 65001

Date: 2018-08-16 19:00:36
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` varchar(64) NOT NULL COMMENT '主键id',
  `login_name` varchar(200) DEFAULT NULL COMMENT '登陆账号',
  `user_name` varchar(200) DEFAULT NULL COMMENT '用户名',
  `password` varchar(255) DEFAULT NULL COMMENT '密码',
  `tel` varchar(64) DEFAULT NULL COMMENT '电话号码',
  `org` varchar(64) DEFAULT NULL COMMENT '机构',
  `role` varchar(200) DEFAULT NULL COMMENT '角色',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1223456789', null, 'qq', null, null, null, null);
INSERT INTO `user` VALUES ('122345689', null, 'wwwwww', null, null, null, null);
INSERT INTO `user` VALUES ('12234589', null, 'eeeeeee', null, null, null, null);
INSERT INTO `user` VALUES ('1223489', null, 'rrrrrrr', null, null, null, null);
INSERT INTO `user` VALUES ('122389', null, 'ttttt', null, null, null, null);
INSERT INTO `user` VALUES ('12289', null, 'yyyyyyyyyyy', null, null, null, null);
INSERT INTO `user` VALUES ('123', null, 'uuuuuuuu', null, null, null, null);
INSERT INTO `user` VALUES ('1234', null, 'iiiiiiiiiii', null, null, null, null);
INSERT INTO `user` VALUES ('12345', null, 'aaaaaaa', null, null, null, null);
INSERT INTO `user` VALUES ('123456', null, 'sssssssss', null, null, null, null);
INSERT INTO `user` VALUES ('1234567', null, 'dddddddd', null, null, null, null);
INSERT INTO `user` VALUES ('12345678', null, 'fffffffff', null, null, null, null);
INSERT INTO `user` VALUES ('123456789', null, 'gggggggg', null, null, null, null);
INSERT INTO `user` VALUES ('1289', null, 'hhhhhhhhh', null, null, null, null);
INSERT INTO `user` VALUES ('18ce23a9027b4fe29e7a9c5b4828a20b', 'lsmife', 'lshis', '0a113ef6b61820daa5611c870ed8d5ee', '18000000000', '工商银行', 'usual');
INSERT INTO `user` VALUES ('8197fdc71d7a4838b2d81fccdc150ac1', 'lshis', 'Mr.Li', 'b706835de79a2b4e80506f582af3676a', '18899998888', '金融集团', 'usual');
INSERT INTO `user` VALUES ('868560ec718d4cb5a969ab8ccdb54ac7', 'admin', 'admin', '123456', '15512345678', 'root', 'admin');

 

 

小伙伴们 就此结束

你可能感兴趣的:(Springboot+Mybatis+PageHelper快速实现分页功能)