Spring Boot 集成MyBatis和Pagehelper分页插件

mybatis-spring-boot-starter依赖树如下:

pom配置


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.xiaolyuh</groupId>

<artifactId>spring-boot-student-mybatis</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>spring-boot-student-mybatis</name>

<span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">&lt;!-- 添加Spring Boot的父类依赖,这样当前项目就是Spring Boot项目了。 spring-boot-starter-parent是一个特殊的starter,他用来
提供相关的maven默认依赖, 使用它之后,常用的依赖可以省去version标签 --&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">parent</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>spring-boot-starter-parent<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">version</span>&gt;</span>1.5.3.RELEASE<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">version</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">relativePath</span>/&gt;</span> <span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">&lt;!-- lookup parent from repository --&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">parent</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">properties</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">project.build.sourceEncoding</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">project.reporting.outputEncoding</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">java.version</span>&gt;</span>1.8<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">java.version</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">properties</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependencies</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>spring-boot-starter<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>mysql<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>mysql-connector-java<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">version</span>&gt;</span>1.3.0<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">version</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">&lt;!--pagehelper --&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>com.github.pagehelper<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>pagehelper-spring-boot-starter<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">version</span>&gt;</span>1.1.1<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">version</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>fastjson<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">version</span>&gt;</span>1.2.31<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">version</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">scope</span>&gt;</span>test<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">scope</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependency</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">dependencies</span>&gt;</span></br></br> <span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">build</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">plugins</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">plugin</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">groupId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;<span class="hljs-title" style="">artifactId</span>&gt;</span>spring-boot-maven-plugin<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">artifactId</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">plugin</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">plugins</span>&gt;</span></br>
<span class="hljs-tag" style="color:navy">&lt;/<span class="hljs-title" style="">build</span>&gt;</span></br></br>

</project>

application.properties配置

server.port=80
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/ssb_test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.username=root

spring.datasource.password=root

#连接池配置

#spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource

#mybatis


#entity扫描的包名


mybatis.type-aliases-package=com.xiaolyuh.domain.model


#Mapper.xml所在的位置


mybatis.mapper-locations=classpath:/mybaits/Mapper.xml

#pagehelper分页插件配置


pagehelper.helperDialect=mysql


pagehelper.reasonable=true


pagehelper.supportMethodsArguments=true


pagehelper.params=count=countSql

#日志配置


logging.level.com.xiaolyuh=debug


logging.level.org.springframework.web=debug


logging.level.org.springframework.transaction=debug


logging.level.org.mybatis=debug


debug=false




除了上面常见的两项配置,还有:

mybatis.config-location=mybatis-config.xml配置文件的路径

mybatis.type-handlers-package=扫描typeHandlers的包

mybatis.check-config-location=检查配置文件是否存在

mybatis.executor-type=设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE

Mapping XML文件

在resources文件夹下创建mybaits/PersonMapper.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.xiaolyuh.domain.mapper.PersonMapper" >

<resultMap id="BaseResultMap" type="com.xiaolyuh.domain.model.Person" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

<id column="id" property="id" jdbcType="BIGINT" />

<result column="name" property="name" jdbcType="VARCHAR" />

<result column="age" property="age" jdbcType="INTEGER" />

<result column="address" property="address" jdbcType="VARCHAR" />

</resultMap>

<sql id="Base_Column_List" >
<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

id, name, age, address
</sql>

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

select

<include refid="Base_Column_List" />

from person

where id = #{id,jdbcType=BIGINT}

</select>

<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

delete from person

where id = #{id,jdbcType=BIGINT}

</delete>

<insert id="insert" parameterType="com.xiaolyuh.domain.model.Person" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >

SELECT LAST_INSERT_ID()

</selectKey>

insert into person (name, age, address

)

values (#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR}

)

</insert>

<insert id="insertSelective" parameterType="com.xiaolyuh.domain.model.Person" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >

SELECT LAST_INSERT_ID()

</selectKey>

insert into person

<trim prefix="(" suffix=")" suffixOverrides="," >

<if test="name != null" >

name,

</if>

<if test="age != null" >

age,

</if>

<if test="address != null" >

address,

</if>

</trim>

<trim prefix="values (" suffix=")" suffixOverrides="," >

<if test="name != null" >
#{name,jdbcType=VARCHAR},

</if>

<if test="age != null" >

#{age,jdbcType=INTEGER},

</if>

<if test="address != null" >

#{address,jdbcType=VARCHAR},

</if>

</trim>

</insert>

<update id="updateByPrimaryKeySelective" parameterType="com.xiaolyuh.domain.model.Person" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

update person

<set >

<if test="name != null" >

name = #{name,jdbcType=VARCHAR},

</if>

<if test="age != null" >

age = #{age,jdbcType=INTEGER},

</if>

<if test="address != null" >

address = #{address,jdbcType=VARCHAR},

</if>

</set>

where id = #{id,jdbcType=BIGINT}

</update>

<update id="updateByPrimaryKey" parameterType="com.xiaolyuh.domain.model.Person" >

<!--

WARNING - @mbggenerated

This element is automatically generated by MyBatis Generator, do not modify.

-->

update person

set name = #{name,jdbcType=VARCHAR},

age = #{age,jdbcType=INTEGER},

address = #{address,jdbcType=VARCHAR}

where id = #{id,jdbcType=BIGINT}

</update>

<select id="findAll" resultMap="BaseResultMap">


select


<include refid="Base_Column_List" />

from person


</select>

<select id="findByPage" resultMap="BaseResultMap">


select


<include refid="Base_Column_List" />


from person


</select>


</mapper>

DAO层Mapper类

在Mapper接口上需要加上@Mapper注解,@Mapper注解声明成mybatis Dao层的Bean。也可以在配置类上使用@MapperScan("com.xiaolyuh.domain.mapper")注解声明。

package com.xiaolyuh.domain.mapper;

import com.github.pagehelper.Page;


import com.xiaolyuh.domain.model.Person;


import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper//声明成mybatis Dao层的Bean,也可以在配置类上使用@MapperScan("com.xiaolyuh.domain.mapper")注解声明


public interface PersonMapper
{
<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">int</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">deleteByPrimaryKey</span><span class="hljs-params" style="">(Long id)</span></span>;</br></br>

<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">int</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">insert</span><span class="hljs-params" style="">(Person record)</span></span>;</br></br>

<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">int</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">insertSelective</span><span class="hljs-params" style="">(Person record)</span></span>;</br>

<span class="hljs-function" style="">Person <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">selectByPrimaryKey</span><span class="hljs-params" style="">(Long id)</span></span>;</br></br>

<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">int</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">updateByPrimaryKeySelective</span><span class="hljs-params" style="">(Person record)</span></span>;</br></br>

<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">int</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">updateByPrimaryKey</span><span class="hljs-params" style="">(Person record)</span></span>;</br></br>

<span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">/**</br>
* 获取所有数据</br>
* <span class="hljs-doctag" style="color:rgb(221,17,68)">@return</span></br>
*/</span></br>
<span class="hljs-function" style="">List&lt;Person&gt; <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">findAll</span><span class="hljs-params" style="">()</span></span>;</br> <span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">/**</br>
* 分页查询数据</br>
* <span class="hljs-doctag" style="color:rgb(221,17,68)">@return</span></br>
*/</span></br>
<span class="hljs-function" style="">Page&lt;Person&gt; <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">findByPage</span><span class="hljs-params" style="">()</span></span>;</br>

}

实体类

package com.xiaolyuh.domain.model;

public class Person {
<span class="hljs-keyword" style="font-weight:700">private</span> Long id;</br>



/**

* 名称

*/

private String name;

<span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">/**</br>
* 年龄</br>
*/</span></br>
<span class="hljs-keyword" style="font-weight:700">private</span> Integer age;</br> <span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">/**</br>
* 地址</br>
*/</span></br>
<span class="hljs-keyword" style="font-weight:700">private</span> String address;</br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> Long <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">getId</span><span class="hljs-params" style="">()</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">return</span> id;</br>
}</br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">setId</span><span class="hljs-params" style="">(Long id)</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">this</span>.id = id;</br>
}</br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> String <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">getName</span><span class="hljs-params" style="">()</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">return</span> name;</br>
}</br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">setName</span><span class="hljs-params" style="">(String name)</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">this</span>.name = name;</br>
}</br></br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> Integer <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">getAge</span><span class="hljs-params" style="">()</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">return</span> age;</br>
}</br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">setAge</span><span class="hljs-params" style="">(Integer age)</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">this</span>.age = age;</br>
}</br></br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> String <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">getAddress</span><span class="hljs-params" style="">()</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">return</span> address;</br>
}</br></br> <span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">setAddress</span><span class="hljs-params" style="">(String address)</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">this</span>.address = address;</br>
}</br>

}

Service层

接口

package com.xiaolyuh.service;

import com.github.pagehelper.Page;


import com.xiaolyuh.domain.model.Person;

import java.util.List;

/**
  • Created by yuhao.wang on 2017/6/19.


    */


    public interface PersonService
    {

    List<Person> findAll();

    /**
    • 分页查询
    • @param pageNo 页号
    • @param pageSize 每页显示记录数
    • @return


      */


      Page<Person> findByPage(int pageNo, int pageSize);

    void insert(Person person);



    }

实现类

package com.xiaolyuh.service.impl;

import com.github.pagehelper.Page;


import com.github.pagehelper.PageHelper;


import com.xiaolyuh.domain.mapper.PersonMapper;


import com.xiaolyuh.domain.model.Person;


import com.xiaolyuh.service.PersonService;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
  • Created by yuhao.wang on 2017/6/19.


    */


    @Service


    @Transactional(readOnly = true)


    public class PersonServiceImpl
    implements PersonService {

    @Autowired


    private PersonMapper personMapper;

    @Override


    public List<Person> findAll() {


    return personMapper.findAll();


    }

    @Override


    public Page<Person> findByPage(int pageNo, int pageSize) {


    PageHelper.startPage(pageNo, pageSize);


    return personMapper.findByPage();


    }

    @Override


    @Transactional


    public void insert(Person person) {


    personMapper.insert(person);


    }
}

分页的包装类PageInfo

需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo

package com.xiaolyuh.page;
import java.io.Serializable;


import java.util.Collection;


import java.util.List;

import com.github.pagehelper.Page;

/**
  • 对Page<E>结果进行包装
  • <p/>
  • 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907

  • @author liuzh/abel533/isea533
  • @version 3.3.0
  • @since 3.2.2
  • 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper


    */


    @SuppressWarnings({"rawtypes", "unchecked"})

    public class PageInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;


    //当前页


    private int pageNum;


    //每页的数量


    private int pageSize;


    //总记录数


    private long total;


    //总页数


    private int pages;


    //结果集


    private List<T> list;

    //是否为第一页


    private boolean isFirstPage = false;


    //是否为最后一页


    private boolean isLastPage = false;

    public PageInfo() {


    }
    /**
    • 包装Page对象
    • @param list


      */


      public PageInfo(List<T> list) {

      if (list instanceof Page) {


      Page page = (Page) list;


      this.pageNum = page.getPageNum();


      this.pageSize = page.getPageSize();

       <span class="hljs-keyword" style="font-weight:700">this</span>.pages = page.getPages();</br>
      <span class="hljs-keyword" style="font-weight:700">this</span>.list = page;</br>
      <span class="hljs-keyword" style="font-weight:700">this</span>.total = page.getTotal();</br>

      } else if (list instanceof Collection) {


      this.pageNum = 1;


      this.pageSize = list.size();

       <span class="hljs-keyword" style="font-weight:700">this</span>.pages = <span class="hljs-number" style="color:teal">1</span>;</br>
      <span class="hljs-keyword" style="font-weight:700">this</span>.list = list;</br>
      <span class="hljs-keyword" style="font-weight:700">this</span>.total = list.size();</br></br>

      }


      if (list instanceof Collection) {


      //判断页面边界


      judgePageBoudary();


      }


      }

    /**

    • 判定页面边界

      */

      private void judgePageBoudary() {


      isFirstPage = pageNum == 1;


      isLastPage = pageNum == pages;


      }

    public int getPageNum() {


    return pageNum;


    }





    public void setPageNum(int pageNum) {


    this.pageNum = pageNum;


    }

    public int getPageSize() {


    return pageSize;


    }

    public void setPageSize(int pageSize) {


    this.pageSize = pageSize;


    }

    public long getTotal() {


    return total;


    }

    public void setTotal(long total) {


    this.total = total;


    }

    public int getPages() {


    return pages;


    }

    public void setPages(int pages) {


    this.pages = pages;


    }

    public List<T> getList() {


    return list;


    }

    public void setList(List<T> list) {


    this.list = list;


    }

    public boolean isIsFirstPage() {


    return isFirstPage;


    }

    public void setIsFirstPage(boolean isFirstPage) {


    this.isFirstPage = isFirstPage;


    }

    public boolean isIsLastPage() {


    return isLastPage;


    }

    public void setIsLastPage(boolean isLastPage) {


    this.isLastPage = isLastPage;


    }

    @Override

    public String toString() {

    final StringBuffer sb = new StringBuffer("PageInfo{");

    sb.append("pageNum=").append(pageNum);


    sb.append(", pageSize=").append(pageSize);


    sb.append(", total=").append(total);


    sb.append(", pages=").append(pages);


    sb.append(", list=").append(list);


    sb.append(", isFirstPage=").append(isFirstPage);


    sb.append(", isLastPage=").append(isLastPage);


    sb.append(", navigatepageNums=");


    sb.append('}');


    return sb.toString();


    }


    }

测试类

package com.xiaolyuh;

import com.github.pagehelper.Page;


import com.xiaolyuh.domain.model.Person;


import com.xiaolyuh.page.PageInfo;


import com.xiaolyuh.service.PersonService;


import org.junit.Assert;


import org.junit.Before;


import org.junit.Test;


import org.junit.runner.RunWith;


import com.alibaba.fastjson.JSON;


import org.slf4j.Logger;


import org.slf4j.LoggerFactory;


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 PersonMapperTests {





private Logger logger = LoggerFactory.getLogger(PersonMapperTests.class);

<span class="hljs-annotation" style="">@Autowired</span></br>
<span class="hljs-keyword" style="font-weight:700">private</span> PersonService personService;</br> <span class="hljs-annotation" style="">@Before</span></br>
<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">testInsert</span><span class="hljs-params" style="">()</span> </span>{</br>
Person person = <span class="hljs-keyword" style="font-weight:700">new</span> Person();</br>
person.setName(<span class="hljs-string" style="color:rgb(221,17,68)">"测试"</span>);</br>
person.setAddress(<span class="hljs-string" style="color:rgb(221,17,68)">"address"</span>);</br>
person.setAge(<span class="hljs-number" style="color:teal">10</span>);</br>
personService.insert(person);</br></br> Assert.assertNotNull(person.getId());</br>
logger.debug(JSON.toJSONString(person));</br>
}</br> <span class="hljs-annotation" style="">@Test</span>
<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">testFindAll</span><span class="hljs-params" style="">()</span> </span>{</br>
List&lt;Person&gt; persons = personService.findAll();</br></br> Assert.assertNotNull(persons);</br>
logger.debug(JSON.toJSONString(persons));</br>
}</br> <span class="hljs-annotation" style="">@Test</span></br>
<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">testFindByPage</span><span class="hljs-params" style="">()</span> </span>{</br>
Page&lt;Person&gt; persons = personService.findByPage(<span class="hljs-number" style="color:teal">1</span>, <span class="hljs-number" style="color:teal">2</span>);</br>
<span class="hljs-comment" style="color:rgb(153,153,136); font-style:italic">// 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo</span></br>
PageInfo&lt;Person&gt; pageInfo = <span class="hljs-keyword" style="font-weight:700">new</span> PageInfo&lt;&gt;(persons);</br>
Assert.assertNotNull(persons);</br>
logger.debug(pageInfo.toString());</br>
logger.debug(JSON.toJSONString(pageInfo));</br>
}</br></br> <span class="hljs-annotation" style="">@Test</span></br>
<span class="hljs-function" style=""><span class="hljs-keyword" style="font-weight:700">public</span> <span class="hljs-keyword" style="font-weight:700">void</span> <span class="hljs-title" style="color:rgb(153,0,0); font-weight:700">testCacheByPage</span><span class="hljs-params" style="">()</span> </span>{</br>
<span class="hljs-keyword" style="font-weight:700">long</span> begin = System.currentTimeMillis();</br>
List&lt;Person&gt; persons = personService.findAll();</br>
<span class="hljs-keyword" style="font-weight:700">long</span> ing = System.currentTimeMillis();</br>
personService.findAll();</br>
<span class="hljs-keyword" style="font-weight:700">long</span> end = System.currentTimeMillis();</br>
logger.debug(<span class="hljs-string" style="color:rgb(221,17,68)">"第一次请求时间:"</span> + (ing - begin) + <span class="hljs-string" style="color:rgb(221,17,68)">"ms"</span>);</br>
logger.debug(<span class="hljs-string" style="color:rgb(221,17,68)">"第二次请求时间:"</span> + (end - ing) + <span class="hljs-string" style="color:rgb(221,17,68)">"ms"</span>);</br></br> Assert.assertNotNull(persons);</br>
logger.debug(persons.toString());</br>
logger.debug(JSON.toJSONString(persons));</br>
}</br>

}

  </div>

Spring Boot+Mybatis+Pagehelper分页的更多相关文章

  1. spring boot 整合pagehelper分页插件

    Spring Boot 整合pagehelper分页插件 测试环境: spring boot  版本 2.0.0.M7 mybatis starter 版本  1.3.1 jdk 1.8 ------ ...

  2. Spring Boot + MyBatis + Pagehelper 配置多数据源

    前言: 本文为springboot结合mybatis配置多数据源,在项目当中很多情况是使用主从数据源来读写分离,还有就是操作多库,本文介绍如何一个项目同时使用2个数据源. 也希望大家带着思考去学习!博 ...

  3. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

  4. 【转】spring boot mybatis 读取配置文件

    spring boot mybatis 配置整理 一.加载mybatis的配置 1.手写配置,写死在代码里 import java.io.IOException; import java.util.P ...

  5. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  6. 详解spring boot mybatis全注解化

    本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...

  7. (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

    大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...

  8. spring boot + mybatis + layui + shiro后台权限管理系统

    后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...

  9. spring boot+mybatis搭建项目

    一.创建spring boot项目 1.File->New->Project 2.选择 Spring Initializr ,然后选择默认的 url 点击[Next]: 3.修改项目信息 ...

随机推荐

  1. c、c++ 结构体的嵌套

    c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...

  2. animation- 动画效果实现(xml形式实现)

    1.定义xml动画 1)在anim文件夹下定义xml文件 解释:这个文件夹下面的文件会被默认为动画文件,如果这个文件不存在,需要自己创建 display_result_anim.xml <?xm ...

  3. zenDiscovery和master选举

    上一篇通过 ElectMasterService源码,分析了master选举的原理的大部分内容:master候选节点ID排序保证选举一致性及通过设置最小可见候选节点数目避免brain split.节点 ...

  4. Cookie应用--显示看过的商品

    package cn.itcast; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHa ...

  5. sql查看依赖关系

    select OBJECT_NAME(object_id) as name,object_NAME(referenced_major_id) as ref from sys.sql_dependenc ...

  6. CMake设置生成vs工程的动态库输出路径

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在网上搜了很多的资料,发现CMake不能设置一个动态库工程的输出目录和中间目录,难道除了VC之外其它编译器如gcc中没 ...

  7. Linux系统捕获数据包流程

    Linux系统捕获数据包流程 为了提高数据包的捕获效率,瓶颈问题是一个需要非常关注的焦点.减少在捕获数据包过程中的瓶颈,就能够提高数据包捕获的整体性能.下面本文将以Linux操作系统为平台,分析捕获数 ...

  8. 【2017 Multi-University Training Contest - Team 7】Just do it

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6129 [Description] 设定b[i]=a[1]^a[2]^a[3]^------a[i] ...

  9. 可执行EXE在windows调用过程

    举例图中, 一个C#编写的测试程序, 输出两句话分别 : Hello, GoodBye, 介绍其在windows上CLR的调用过程. 1.在执行Main方法之前, CLR会检测出Main的代码引用的所 ...

  10. 洛谷 P1689 方程求解

    P1689 方程求解 题目描述 给一个方程,形如X+Y=Z或X-Y=Z.给出了其中两个未知数,请求出第三个数.未知数用‘?’表示,等式中也许会出现一些多余的空格. 输入输出格式 输入格式: 一行,方程 ...