spring boot gradle 集成(一)

1、工具 idea 2、新建一个工程 3、项目结构图: 4、controller  5、spring boot application 启动类 6、application配置文件,我这用的是属性文件格式进行配置,也可以使用yml格式进...

1、工具 idea

2、新建一个工程

attachments-2017-11-o5VNUEs55a050521078ed.attachments-2017-11-EjjNXDXC5a0505a9df4bc.


attachments-2017-11-nyXAe7F55a0505b53a8d3.

attachments-2017-11-foXddfUG5a0505c50711d.3、项目结构图:

attachments-2017-11-3yrwMUya5a050768a5e33.4、controller 

package com.hellojava.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author dengjihai
* @create 2017-11-10
**/
@RestController
public class HelloJavaController {


@GetMapping("/hellojava")
public String sayHelloJava() {
return "Hello`Java!";
}


@GetMapping("/")
public String sayIndex() {
return "Hello`Java`index!";
}
}

5、spring boot application 启动类

package com.hellojava;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Spring Boot应用启动类
* @author dengjihai
* @create 2017-11-10
**/
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}

6、application配置文件,我这用的是属性文件格式进行配置,也可以使用yml格式进行配置

##端口
server.port=8090
# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:4969/dsds?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
spring.datasource.useGlobalDataSourceStat=true
spring.jpa.show-sql=true

7、gradle 配置文件

group 'com.hellojava'
version '1.0-SNAPSHOT'
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public"}
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'


sourceCompatibility = 1.8
bootRun {
addResources = true
}

repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public"}
jcenter()
}

dependencies {

compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-freemarker')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-data-redis")
compile("org.springframework.boot:spring-boot-starter-data-elasticsearch")
compile('org.springframework.boot:spring-boot-starter-tomcat')
compile("org.springframework.boot:spring-boot-devtools")
compile("mysql:mysql-connector-java")
compile group: 'com.alibaba', name: 'druid', version: '1.0.18'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

8、启动ing

attachments-2017-11-9B4dxwKR5a0509365605b.9、启动完成:

attachments-2017-11-dM1eYvPr5a05094b16482.

10.打开浏览器: http://localhost:8090/

attachments-2017-11-JAYz4pFX5a050961297dd.attachments-2017-11-OqOkew0L5a050971c8bed.

  • 发表于 2017-11-10 09:58
  • 阅读 ( 2414 )
  • 分类:springboot

1 条评论

请先 登录 后评论
不写代码的码农
威猛的小站长

124 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章