本次我们整合mybatis的分页插件pagehelper,以及mapper自动生成的插件mybatis-generator
先整合mybatis-generator
1、在pom中引入
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.5
mysql
mysql-connector-java
5.1.39
org.mybatis.generator
mybatis-generator-core
1.3.5
Generate MyBatis Artifacts
package
generate
true
true
src/main/resources/mybatis-generator.xml
2、在resources目录下创建文件:mybatis-generator.xml
3、执行自动生成代码
整合分页插件pagehelper
1、引入依赖
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
2、在配置文件中加入分页的配置
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
测试:
1、数据库准备
/*
Navicat MySQL Data Transfer
Source Server : www.streamyear.com
Source Server Version : 50711
Source Host : www.streamyear.com:3306
Source Database : mybatis
Target Server Type : MYSQL
Target Server Version : 50711
File Encoding : 65001
Date: 2018-09-24 10:54:59
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(100) DEFAULT NULL COMMENT '用户名',
`password` varchar(100) DEFAULT NULL COMMENT '密码',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
`age` int(10) DEFAULT NULL COMMENT '年龄',
`sex` tinyint(1) DEFAULT NULL COMMENT '性别,1男性,2女性',
`birthday` date DEFAULT NULL COMMENT '出生日期',
`created` datetime DEFAULT NULL COMMENT '创建时间',
`updated` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tb_user
-- ----------------------------
INSERT INTO `tb_user` VALUES ('1', 'zhangsan', '123456', '张三', '30', '1', '1984-08-08', '2014-09-19 16:56:04', '2014-09-21 11:24:59');
INSERT INTO `tb_user` VALUES ('2', 'lisi', '123456', '李四', '21', '2', '1991-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('3', 'wangwu', '123456', '王五', '22', '2', '1989-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('4', 'zhangwei', '123456', '张伟', '20', '1', '1988-09-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('5', 'lina', '123456', '李娜', '28', '1', '1985-01-01', '2014-09-19 16:56:04', '2014-09-19 16:56:04');
INSERT INTO `tb_user` VALUES ('6', 'lilei', '123456', '李磊', '23', '1', '1988-08-08', '2014-09-20 11:41:15', '2014-09-20 11:41:15');
2、编写后台的代码:controller
package com.streamyear.course.controller;
import com.streamyear.course.entity.User;
import com.streamyear.course.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 用户的Controller
*/
@RestController
@RequestMapping("user")
public class UserController {
/**
* 用户的Service
*/
@Autowired
private UserService userService;
/**
* 分页获取所有的用户信息
* @param pageNum 当前页
* @param pageSize 每页展示的数量
* @return 用户的list
*/
@RequestMapping("/listAllUserInfo")
public List listAllUserInfo(Integer pageNum, Integer pageSize){
List result = userService.listAllRecord(pageNum, pageSize);
return result;
}
}
3、其他的代码:service,dao可以去github上看。地址在文章的末尾
4、项目启动,进行测试出现异常
Field userMapper in com.streamyear.course.service.impl.UserServiceImpl required a bean of type 'com.streamyear.course.mapper.UserMapper' that could not be found.
解决方案:在application上面加入注解
@MapperScan(basePackages = {"com.streamyear.course.mapper"})
package com.streamyear.course;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = {"com.streamyear.course.mapper"})
public class CourseApplication {
public static void main(String[] args) {
SpringApplication.run(CourseApplication.class, args);
}
}
5、重新启动项目,测试
备注:
项目的代码的地址:https://github.com/streamyear/course.git