前言
本文主要是針對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》,我的如下圖:
添加整合相關的包:
<!-- 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了。