1、工具 idea
2、新建一个工程
3、项目结构图:
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
9、启动完成:
10.打开浏览器: http://localhost:8090/
124 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!