一、导入依赖

二、build.gradle
- 整合SpringBoot
plugins {
id 'java'
}
group 'com.qiang'
version '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.4.RELEASE'
}
三、Application
package com.qiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author 小强崽
* @ClassName: Application
* @Description: Gradle整合SpringBoot
* @date 2019/9/16 1:17
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 启动项目

- 启动成功

四、整合Mabatis
- 导入依赖
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
compile group: 'com.alibaba', name: 'druid', version: '1.1.18'
compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '2.0.1'
- 设置资源文件夹
sourceSets {
main {
resources {
srcDirs("src/main/java", "src/main/resources")
}
}
}
- 在主函数上加上注解
@MapperScan({"com.qiang.mapper"})
五、application.yml
server:
port: 8080
spring:
application:
name: FirstGradle
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CTT
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
六、整合PageHelper
- 导入依赖
compile group: 'com.github.pagehelper', name: 'pagehelper', version: '4.1.6'
- 配置PageHelper
package com.qiang.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* @author 小强崽
* @ClassName: MybatisConfig
* @Description: PageHelper配置
* @date 2019/9/16 16:37
*/
@Configuration
public class MybatisConfig {
/**
* 配置Mybatis的分页插件
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
- 使用
package com.qiang.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiang.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author 小强崽
* @ClassName: UserController
* @Description: 用户控制器
* @date 2019/9/16 1:51
*/
@RestController
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Resource
private UserService userService;
@RequestMapping("/user")
public Object getAllUser() {
Page