小程序入门学习19--springboot之HelloWorld

1 项目结构

在这里插入图片描述

2 代码

Application.java

package com.next;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication//入口类,启动Springboot项目
@ComponentScan(basePackages = {"com.next"})//扫描同目录及同目录下级目录的类文件
public class Application {

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

}

HelloController.java

package com.next.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //代表下面方法返回类型和接收对象是json,
public class HelloController {
	//只能接受get方式的请求
	@GetMapping("/hello")
	public Object hello() {
		return "Hello World~~";
	}
}

application.properties

# Server 服务器端相关配置
# 配置api端口号
server.port=8081
#Server -tomcat 相关常用配置
server.tomcat.uri-encoding=UTF-8

#不配置下方数据,需在启动类时这样写:@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
#不然会出现:Failed to configure a DataSource: 'url' attribute is not specified and no embedded 
#配置数据源相关 使用HikariCP 数据源

#jdbc_config datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/news?useUnicode=true&&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLExpection,默认:30秒
spring.datasource.hikari.connection-timeout=30000
#最小连接数
spring.datasource.hikari.minimum-idle=5
#最大连接数
spring.datasource.hikari.maximum-pool-size=15
#自动提交
spring.datasource.hikari.auto-commit=true
#一个连接idle的最大时长(毫秒),超时则被释放(retired),默认:10分钟
spring.datasource.hikari.idle-timeout=600000
#连接池名字
spring.datasource.hikari.pool-name=DatebookHikariCP
#一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms 
spring.datasource.hikari.max-lifetime=28740000
spring.datasource.hikari.connection-test-query=SELECT 1

3 启动

Application.java->Run as->Java Application
在这里插入图片描述
在这里插入图片描述
浏览器输入http://localhost:8081/hello 访问(8081是自己设置的端口号)

  • 1 eclipse的.properties无法输入中文
    点击Window->Preferences,进行如下操作
    在这里插入图片描述
  • 2 创建的working set找不到了,即使点击Top Level Elements->Working Sets依然找不到
    进入Select a working set,选择需要显示的working set
    在这里插入图片描述
    在这里插入图片描述
发布了114 篇原创文章 · 获赞 32 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/moqianmoqian/article/details/104455542
今日推荐