Mybatis分页插件 - PageHelper与springBoot的整合
2017-08-01 00:00
471 查看
摘要: 因项目需求用到分页插件pagehelper,故整合pagehelper与springboot的结果记录下来,插件作者github地址见:https://github.com/abel533/MyBatis-Spring-Boot
该实例在:https://github.com/gholly/Mybatis-PageHelper-SpringBoot
1.首先添加项目依赖
2.yml配置
3.实体类model层:
BaseModel.java
User.java
4.Mybatis的xml文件
5.controller文件
运行结果:
打断点获取pageInfo的值,其中查询的结果在pageInfo的list中:
该实例在:https://github.com/gholly/Mybatis-PageHelper-SpringBoot
1.首先添加项目依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.0.1</version> </dependency> <!--分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--作者的mybatis插件--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.1.3</version> </dependency>
2.yml配置
server: port: 8383 spring: datasource: url: jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true username: upgrade password: upgrade driverClassName: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource #有关插件的配置情况 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql logging: level.com.test.data: debug
3.实体类model层:
BaseModel.java
package com.test.data.model; import javax.persistence.*; /** * Created by guanguan on 2017/7/31. */ public class BaseModel { @Id //标注用于声明一个实体类的属性映射为数据库的主键列,也可置于属性的getter方法之前 @Column(name="Id") @GeneratedValue(strategy= GenerationType.AUTO) //用于标注主键的生成策略,通过strategy属性指定,默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer对应identity,MySQL 对应 auto increment。 private Integer id; @Transient //声明一个实例变量,当对象存储时,它的值不需要维持 private Integer page=1; @Transient private Integer rows=10; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getRows() { return rows; } public void setRows(Integer rows) { this.rows = rows; } }
User.java
package com.test.data.model; /** * Created by guanguan on 2017/7/20. */ public class User extends BaseModel{ private Integer id; private String userName; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
4.Mybatis的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.test.data.dao.UserMapper"> <resultMap id="userResultMap" type="com.test.data.model.User"> <id property="id" column="id"/> <result property="userName" column="user_name"/> </resultMap> <sql id="sql1"> `id`,`user_name` </sql> <select id="findBy" resultMap="userResultMap" parameterType="HashMap"> select <include refid="sql1"/> from `user` <trim prefixOverrides="and" prefix="where"> <if test="id!=null"> and `id`=#{id} </if> <if test="userName!=null"> and `user_name`=#{userName} </if> </trim> </select> </mapper>
5.controller文件
package com.test.data.controller; import com.alibaba.fastjson.JSON; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.test.data.model.User; import com.test.data.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.List; /** * Created by guanguan on 2017/7/20. */ @Controller public class controller { @Autowired UserService userService; @RequestMapping("/") @ResponseBody public String index() { Integer page=2; Integer rows=3; PageHelper.startPage(page, rows); List<User> userList = userService.findBy(new HashMap<Object, Object>()); PageInfo pageInfo = new PageInfo(userList); return JSON.toJSONString(userList); } }
运行结果:
打断点获取pageInfo的值,其中查询的结果在pageInfo的list中:
相关文章推荐
- spring-boot | 整合通用Mabatis 分页插件PageHelper
- SpringBoot Mybatis PageHelper分页插件的两种用法(二)
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- Spring Boot系列六 Spring boot集成mybatis、分页插件pagehelper
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
- SpringBoot Mybatis PageHelper分页插件的两种用法(一)
- [置顶] MyBatis基于Spring-boot集成通用Mapper以及pagehelper分页插件(含源码下载)
- springboot如何集成mybatis的pagehelper分页插件
- 记录问题:springboot中使用Mybatis-pagehelper分页插件遇到的版本兼容问题
- SpringBoot集成MyBatis的分页插件PageHelper(回头草)
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
- spring-boot 集成mybatis的分页插件PageHelper和Generator (番外)
- spring-boot 集成mybatis的分页插件PageHelper和Generator
- Springboot +pagehelper 分页插件
- SpringBoot + Mybatis + Mapper + PageHelper + Spring Batch 整合
- spring整合mybatis逆向工程+PageHelper插件
- Spring + Mybatis 使用 PageHelper 插件分页
- springboot整合mybatis和pageHelper异常问题记录
- Spring Boot : 集成mybatis、pagehelper插件、开启事务、druid数据源配置、mybatis-generator(十一)