Springboot+mybatis分頁

application.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://140.143.168.242:3306/wechargame?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: m3HGiWhAh2R2ChXs
    driver-class-name: com.mysql.jdbc.Driver
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用於防火牆
    filters: stat,wall
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  thymeleaf:
    cache: true
    prefix: classpath:/templates/
    suffix: .html
    encoding: utf-8
    mode: HTML5

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mapper/*.xml

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.wechargame.cms.entity"/>
    </typeAliases>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--分頁參數合理化  -->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

service

@Service
public class TravelServiceImpl implements TravelService {

    @Autowired
    private TravelMapper travelMapper;

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Travel> lists = travelMapper.selectByAll(null);
        PageInfo pages = new PageInfo(lists, pageSize);
        return pages;
    }
}

controller

@GetMapping("/list")
@ResponseBody
public LayerMsg getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
    PageInfo pageInfo = travelService.getPage(pageNum, pageSize);
    return LayerMsg.success(pageInfo.getTotal(), pageInfo.getList());
}

json

{
    "code": 0,
    "msg": "獲取數據成功!",
    "count": 2,
    "data": [
        {
            "id": 21,
            "uOpenid": null,
            "lName": "大范甘迪",
            "lPhone": null,
            "lIntegral": null,
            "cityFolo": null,
            "lImage": null,
            "lMileage": null,
            "lType": "瓦爾特",
            "paceDeparture": "天津",
            "startTime": "2018-08-24T09:45:04.000+0000",
            "endTime": null,
            "lMessage": null,
            "thumpUp": 1,
            "status": null,
            "latitude": null,
            "longitude": null,
            "createTime": null,
            "modifiedTime": null
        }
    ]
}

注意: 如果使用yml文件進行配置不好使用,可以嘗試我博客的方法;下面的代碼先後順序不要寫錯!!!

PageHelper.startPage(pageNum, pageSize);
List<Travel> lists = travelMapper.selectByAll(null);
PageInfo pages = new PageInfo(lists, pageSize);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章