SpringBoot集成Mybatis并具有分页功能PageHelper

 
环境:IDEA编译工具
 
第一步:生成测试的数据库表和数据
  1.  
    SET FOREIGN_KEY_CHECKS=0;
  2.  
     
  3.  
    -- ----------------------------
  4.  
    -- Table structure for user
  5.  
    -- ----------------------------
  6.  
    DROP TABLE IF EXISTS `user`;
  7.  
    CREATE TABLE `user` (
  8.  
    `test_id` bigint(20) NOT NULL COMMENT '主键ID',
  9.  
    `tenant_id` bigint(20) NOT NULL COMMENT '租户ID',
  10.  
    `name` varchar(30) DEFAULT NULL COMMENT '名称',
  11.  
    `age` int(11) DEFAULT NULL COMMENT '年龄',
  12.  
    `test_type` int(11) DEFAULT NULL COMMENT '测试下划线字段命名类型',
  13.  
    `test_date` datetime DEFAULT NULL COMMENT '日期',
  14.  
    `role` bigint(20) DEFAULT NULL COMMENT '测试',
  15.  
    `phone` varchar(11) DEFAULT NULL COMMENT '手机号码',
  16.  
    PRIMARY KEY (`test_id`)
  17.  
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  18.  
     
  19.  
    -- ----------------------------
  20.  
    -- Records of user
  21.  
    -- ----------------------------
  22.  
    INSERT INTO `user` VALUES ('0', '1', '雷锋', '1', '1', '2017-01-01 01:01:01', '1', '10010');
  23.  
    INSERT INTO `user` VALUES ('1', '1', '三毛', '2', '1', '2017-02-02 01:01:01', '1', '10086');
  24.  
    INSERT INTO `user` VALUES ('2', '1', '小马', '1', '1', '2017-03-03 01:01:01', '1', '10000');
  25.  
    INSERT INTO `user` VALUES ('3', '2', '麻花藤', '1', '1', '2017-03-03 01:01:01', '1', '10000');
  26.  
    INSERT INTO `user` VALUES ('4', '2', '东狗', '2', '1', '2017-03-03 01:01:01', '1', '10086');
  27.  
    INSERT INTO `user` VALUES ('5', '1', '王五', '2', '1', '2017-03-03 01:01:01', '1', '10010');
  28.  
    INSERT INTO `user` VALUES ('6', '1', '小小三毛', '2', '1', '2017-02-02 01:01:01', '1', '10086');
将以上的sql的代码放到Navicat中进行新建查询的操作,,生成数据库和表的数据
 
第二步:创建springboot的项目
 
选中这几个类包,,然后进行springboot和mybatis的集成
 
然后需要在pom文件里面集成pageHelper的插件
 
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
 
 
第三步:在工程目录下面创建应有的包,例如下图展示了工程目录,并标记了哪些是新建立的包
 
 
第四步:生成数据库表中推应的实体类的文件(User实体类)
 
package com.example.demo.entity;
import lombok.Data;
import java.util.Date; @Data
public class User { private Integer testId;
private Integer tenantId;
private String name;
private Integer age;
private Integer testType;
private Date testDate;
private Integer role;
private String phone; @Override
public String toString() {
return "User{" +
"testId=" + testId +
", tenantId=" + tenantId +
", name='" + name + '\'' +
", age=" + age +
", testType=" + testType +
", testDate=" + testDate +
", role=" + role +
", phone='" + phone + '\'' +
'}';
}
}
第五步:配置mybatis的mapper的文件在哪里,并且配置pageHelper的配置文件
 
修改application.yml文件里面的内容
 
#运行的端口
server:
port: 8088 #数据库连接池
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 521521
url: jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8&useSSL=false #mybatis知道mapper在哪里
mybatis:
mapper-locations: classpath:mapper/*.xml #分页的配置
pagehelper:
offset-as-page-num: true
row-bounds-with-count: true
reasonable: true
 
第六步:写UserMapper.java文件,,,是一个interface
 
package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List; @Mapper
@Component
public interface UserDao {
List<User> findAll();
}
第七步:生成对应的mapper.xml文件
 
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.UserDao"> <select id="findAll" resultType="com.example.demo.entity.User">
SELECT
u.test_id AS testId,
u.age,
u.name,
u.phone,
u.role,
u.tenant_id AS tenantId,
u.test_date AS testDate
FROM user u
</select>
</mapper>
第八步:进行测试在单元测试里面,,看看具体好不好用
 
 
package com.example.demo.mapper;

import com.example.demo.entity.User;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List; @RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest { @Autowired
private UserDao userDao; @Test
public void findAll() {
PageHelper.startPage(4, 2);
List<User> all = userDao.findAll();
for (User user : all) {
System.out.println(user);
}
}
}
这个单元测试类里面,,,在查询之前就规定好了,第几页,每页几条数据。
 
转自https://blog.csdn.net/lovePaul77/article/details/79581107

SpringBoot集成Mybatis并具有分页功能PageHelper的更多相关文章

  1. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  2. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...

  3. SpringBoot集成MyBatis底层原理及简易实现

    MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...

  4. springboot集成mybatis(逆向工程),热部署以及整合Swagger2

    本文是作者原创,版权归作者所有.若要转载,请注明出处. springboot集成mybatis和mybatis-generator插件 1.新建Springboot项目(略) 2.导入相关依赖 < ...

  5. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  6. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  7. Mybatis Generator实现分页功能

    Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...

  8. SpringBoot 集成Mybatis 连接Mysql数据库

    记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...

  9. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

随机推荐

  1. 域名映射ip

    windows: 修改文件hosts文件 地址是C:\WINDOWS\system32\drivers\etc\hosts 加进你自己的如: Linux: hosts 文件目录: sudo vi /e ...

  2. Ubuntu+Eclipse+SVN 版本控制配置笔记

    第一步:先更新系统内部软件包缓存(预防出错) #  sudo dpkg --clear-avail #  sudo apt-get update 第二步:安装Eclipse的SVN接口组件“javaH ...

  3. (原创)OpenStack服务如何使用Keystone(一)---Keystone端的操作

    (一)Keystone端的操作 (二)如何在OpenStack服务上部署Keystone中间件 (三)详细配置keystonemiddleware OpenStack项目如果要使用Keystone作为 ...

  4. Java设计模式之十一种行为型模式(附实例和详解)

    Java经典设计模式共有21中,分为三大类:创建型模式(5种).结构型模式(7种)和行为型模式(11种). 本文主要讲行为型模式,创建型模式和结构型模式可以看博主的另外两篇文章:J设计模式之五大创建型 ...

  5. ubuntu下gedit和vim输入中文和中文显示

    安装和配置VIM,参考   http://jingyan.baidu.com/album/046a7b3efd165bf9c27fa915.html?picindex=4 在home/你的用户名 这个 ...

  6. spring原理机制

    转自:http://blog.csdn.net/nrain2/article/details/45459311 1,关于spring容器: spring容器是Spring的核心,该 容器负责管理spr ...

  7. Java编程思想学习笔记——字符串

    前言 字符串操作是计算机程序设计中最常见的行为. 不可变String String对象是不可变的 重载"+"与StringBuilder String对象是不可变的,可以给Stri ...

  8. git绑定两个远程仓库

    最近在做公司项目,之前公司项目统一托管在codding 码云,最近我想把项目与自己的gitlab私人仓库再连接一下,作为自己作品收录的地方,这里总结一下用到的git命令及问题. 1.首先, 找到当前已 ...

  9. Dos命令大全(1)

    MS DOS 命令大全 一.基础命令 1 dir 无参数:查看当前所在目录的文件和文件夹. /s:查看当前目录已经其所有子目录的文件和文件夹. /a:查看包括隐含文件的所有文件. /ah:只显示出隐含 ...

  10. OpenLayers 比较有用的对象和属性

    1.ol.interaction.Pointer     是管理地图down.move.up事件的基类,也用于管理地图拖拽事件.     构造函数结构如下: new ol.interaction.Po ...