SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for user
- -- ----------------------------
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE `user` (
- `test_id` bigint(20) NOT NULL COMMENT '主键ID',
- `tenant_id` bigint(20) NOT NULL COMMENT '租户ID',
- `name` varchar(30) DEFAULT NULL COMMENT '名称',
- `age` int(11) DEFAULT NULL COMMENT '年龄',
- `test_type` int(11) DEFAULT NULL COMMENT '测试下划线字段命名类型',
- `test_date` datetime DEFAULT NULL COMMENT '日期',
- `role` bigint(20) DEFAULT NULL COMMENT '测试',
- `phone` varchar(11) DEFAULT NULL COMMENT '手机号码',
- PRIMARY KEY (`test_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of user
- -- ----------------------------
- INSERT INTO `user` VALUES ('0', '1', '雷锋', '1', '1', '2017-01-01 01:01:01', '1', '10010');
- INSERT INTO `user` VALUES ('1', '1', '三毛', '2', '1', '2017-02-02 01:01:01', '1', '10086');
- INSERT INTO `user` VALUES ('2', '1', '小马', '1', '1', '2017-03-03 01:01:01', '1', '10000');
- INSERT INTO `user` VALUES ('3', '2', '麻花藤', '1', '1', '2017-03-03 01:01:01', '1', '10000');
- INSERT INTO `user` VALUES ('4', '2', '东狗', '2', '1', '2017-03-03 01:01:01', '1', '10086');
- INSERT INTO `user` VALUES ('5', '1', '王五', '2', '1', '2017-03-03 01:01:01', '1', '10010');
- INSERT INTO `user` VALUES ('6', '1', '小小三毛', '2', '1', '2017-02-02 01:01:01', '1', '10086');
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
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 + '\'' +
'}';
}
}
#运行的端口
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
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();
}
<?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);
}
}
}
SpringBoot集成Mybatis并具有分页功能PageHelper的更多相关文章
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- SpringBoot集成MyBatis底层原理及简易实现
MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...
- springboot集成mybatis(逆向工程),热部署以及整合Swagger2
本文是作者原创,版权归作者所有.若要转载,请注明出处. springboot集成mybatis和mybatis-generator插件 1.新建Springboot项目(略) 2.导入相关依赖 < ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- Mybatis Generator实现分页功能
Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
随机推荐
- (笔记)Linux下如何查看高CPU占用率线程
在 Linux 下 top 工具可以显示 cpu 的平均利用率(user,nice,system,idle,iowait,irq,softirq,etc.),可以显示每个 cpu 的利用率.但是无法显 ...
- 第三百八十二节,Django+Xadmin打造上线标准的在线教育平台—xadmin管理员详情页面布局,导航图标设置
第三百八十二节,Django+Xadmin打造上线标准的在线教育平台—xadmin进阶 1.后台管理员详情页面布局 后台管理员详情页面,区块是可以拖动的,而且分为了很多个区块 这个页面的布局在xadm ...
- JUnit套件测试实例
“套件测试”是指捆绑了几个单元测试用例并运行起来.在JUnit中,@RunWith 和 @Suite 这两个注解是用来运行套件测试. 下面的例子演示这两个单元测试:JunitTest1 和 Junit ...
- 异常之交叉编译---arm-vfp-linux-gnu/bin/ld: cgic.o: Relocations in generic ELF (EM: 3)
/opt/arm-2009q1/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/l ...
- ubuntu 安装 SVN 后的错误:Subversion Native Library Not Available & Incompatible JavaHL library loaded
问题一 安装了SVN的eclipse插件,使用的时候就会弹出一个错误的提示框: Subversion Native Library Not Available,加载不到JavaHL. 解决方法 ...
- C# 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.T ...
- ubuntu安装mongo数据库
安装mongo数据库,在shell下输入 sudo apt-get install mongodb 如果需要在Python中使用mongo数据库,还需要额外安装Python封装库 pip instal ...
- WebGL 浏览器函数
1.requestAnimationFrame(func) 请求浏览器在将来某时刻回调函数func以完成重绘.requestAnimationFrame()成功的一个关键是确定你在执行其他用户diam ...
- 安装MySQL-python: EnvironmentError:mysql config not found
1执行 sudo yum install python-devel 2 find / -name mysql_config 在/usr/bin/下发现了这个文件 3. 后修改MySQL-python- ...
- A股最新的自由现金流和折现估值查询
A股最新的自由现金流折现估值,利用自由现金流折现的经典公式,采用 8%.9%.10%.11%.12%.15% 等贴现率来进行估值. SH600000:浦发银行的最新自由现金流和折现估值模型: 浦发银行 ...