1. file -> new ->project
2.选择 spring initializr 选择Project SDK 点击下一步
3. 因为想要创建web工程 多疑选择Packaging 为War
Java Version 为8
点击下一步
4.选择需要依赖的jar包
Web下的 Spring Web
Template Engines 下的Thymleaf
SQL 下的 Spring Data JPA 和 MySQL Driver
5.点击next创建完成
6. 进入项目 修改 application.yml文件如下
spring:
datasource:
username: root
password:
url: jdbc:mysql://49.232.211.141:3306/demo
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# schema:
# - classpath:department.sql
7.在pom.xml引入druid数据源
<!--引入druid数据源-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
8.编写controller文件
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class HomeController {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/query")
public Map<String, Object> map() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM department");
return list.get(0);
}
}
9.启动项目
10.postman 访问接口 数据查询成功