Spring Boot2.0 整合mybatis、分页插件、druid

Stella981
• 阅读 544

前言

本文主要是针对SpringBoot2.0.2版本,实现整合mybatis、分页插件、druid等组件,实现完整的web服务,提供restful风格接口。

SpringBoot集成MyBatis有两种方式,一种简单的方式就是使用MyBatis官方提供的:
一、mybatis-spring-boot-starter (本文讲解的)
二、 另外一种方式也是我推荐的整合方式:
        就是仍然用类似mybatis-spring的配置方式,这种方式需要自己写一些代码,但是可以很方便的控制MyBatis的各项配置,与添加组件。参考:https://my.oschina.net/bianxin/blog/1602958

基础框架

①:在http://start.spring.io/,配置你的项目信息并下载我的是《2.0.2.RELEASE》,我的如下图:![](https://oscimg.oschina.net/oscnet/8d919f10e14ac1c29acf121c2933a981f28.jpg)

添加整合相关的包:

<!-- 2.0后包含spring-boot-starter的web服务包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

唯一的属性配置文件

项目不使用application.properties文件 而使用更加简洁的application.yml文件(直接改后缀名): 

server:
  port: 8080
spring:
    application:
        name: user-center
    datasource:
        name: test
        url: jdbc:mysql://127.0.0.1:3306/xin
        username: root
        password: root
        # 使用druid数据源
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        maxActive: 20
        initialSize: 1
        maxWait: 60000
        minIdle: 1
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxOpenPreparedStatements: 20
mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.winter.model
#pagehelper分页插件
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
#日志级别
logging:
  level:
    com.xin.usercenter.dao: debug

别的东西和以前一样的,这样就整合完了。

分页实现代码:

public PageInfo<User> getUserBySearch(AppPage<User> page) {
    // TODO Auto-generated method stub
    PageHelper.startPage(page.getPageNum(),page.getPageSize());
    List<User> list=userDao.queryUserList(page.getParam());
    PageInfo<User> pageInfo = new PageInfo<User>(list);
    return pageInfo;
}

返回的PageInfo的数据结构如下:

{
  "total": 5,
  "list": [
    {
      "id": 1,
      "loginName": "admin",
      "password": "123123",
      "nickname": "ADMIN",
      "type": 1,
      "state": 1,
      "note": "超级管理员",
      "createTime": "2018-04-28 15:15:46",
      "updateTime": "2018-04-28 15:16:37",
      "updateUid": 1,
      "loginIp": null,
      "loginAddr": null
    },
    {
      "id": 2,
      "loginName": "bian",
      "password": "123456",
      "nickname": "Bian",
      "type": 1,
      "state": 1,
      "note": "普通用户",
      "createTime": "2018-06-21 11:25:31",
      "updateTime": "2018-06-21 11:40:52",
      "updateUid": 0,
      "loginIp": null,
      "loginAddr": null
    }
  ],
  "pageNum": 1,
  "pageSize": 2,
  "size": 2,
  "startRow": 1,
  "endRow": 2,
  "pages": 3,
  "prePage": 0,
  "nextPage": 2,
  "isFirstPage": true,
  "isLastPage": false,
  "hasPreviousPage": false,
  "hasNextPage": true,
  "navigatePages": 8,
  "navigatepageNums": [
    1,
    2,
    3
  ],
  "navigateFirstPage": 1,
  "navigateLastPage": 3,
  "firstPage": 1,
  "lastPage": 3
}

个人感觉官方返回的这个数据结构很完善了,所以就直接用PageInfo了。

源码地址:https://gitee.com/flying-cattle/earn_knife

欢迎加入技术讨论群:340697945

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例。使用技术:SpringBoot、mybatis、shiro、thymeleaf、pagehelper、Mapper插件、druid、dataTables、ztree、jQuery开发工具:intellijidea数据库:mys
Stella981 Stella981
2年前
SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例。使用技术:SpringBoot、mybatis、shiro、thymeleaf、pagehelper、Mapper插件、druid、dataTables、ztree、jQuery开发工具:intellijidea数据库:mys
Stella981 Stella981
2年前
SpringBoot学习之路:05.Spring Boot集成pagehelper分页插件
      前面说了SpringBoot集成持久层框架Mybatis的过程,和使用mybatis进行对数据库进行CRUD的操作,然而当对多数据进行查询时就需要进行分页了,分页技术分为客户端分页和服务器端分页(数据库分页),客户端分页是前端的数据插件对返回的数据集进行分页(bootstruptable、quitable等),客户端分页会对数据库和客
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Easter79 Easter79
2年前
SpringBoot学习之路:05.Spring Boot集成pagehelper分页插件
      前面说了SpringBoot集成持久层框架Mybatis的过程,和使用mybatis进行对数据库进行CRUD的操作,然而当对多数据进行查询时就需要进行分页了,分页技术分为客户端分页和服务器端分页(数据库分页),客户端分页是前端的数据插件对返回的数据集进行分页(bootstruptable、quitable等),客户端分页会对数据库和客
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这