免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
序文
この記事では、RESTfulなスタイルのインターフェイスを提供する、完全なWebサービスを実現するために、MyBatisの、ページネーションプラグイン、ドルイドやその他のコンポーネントを統合し、SpringBoot2.0.2バージョン用です。
SpringBootはMyBatisの一つの簡単な方法は、MyBatisの公式のオファーを使用することで二つの方法、持っている統合:
、MyBatisのスプリング・ブート・スターターを ( 記事は説明)
第二に、私は別の方法をお勧めします統合の方法です:
まだ使用されています同様の構成MyBatisのスプリングは、このアプローチは、いくつかのコードを記述する必要がありますが、簡単に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の直接使用されます。
送信元アドレス:https://gitee.com/flying-cattle/earn_knife
技術的なディスカッショングループへようこそ:340 697 945