1 SpringBoot介绍
1.2 优势
- 创建独立的 Spring 应用程序
- 嵌入的 Tomcat,无需部署 WAR 文件
- 简化 Maven 配置
- 自动配置 Spring
- 提供生产就绪型功能,如指标,健康检查和外部配置
1.2 特性
- 为基于 Spring 的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需 XML 配置。同时也可以修改默认值来满足特定的需求
- 提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
- Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式
2 搭建SpringBoot项目
2.1 通过spring initializr创建spring boot项目选择
2.2 选择spring boot的版本号和所需要的依赖
项目就可以直接运行起来,如果创建项目的时候忘记勾选了,后续在手动的加上这些依赖
默认包扫描的地址,Application(main函数)所在的那个包
3 访问前端页面
3.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.2 编写Controller层
@Controller
public class DemoController {
@RequestMapping("index")
public String index(String name, Model model) {
model.addAttribute("name", name);
return "index";
}
}
3.3 编写前端页面
放到resource/templates目录下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>项目首页</title>
</head>
<body>
<h1>第一个页面</h1>
<div th:text="${name}"></div>
</body>
</html>
4 读取后端的值
通过Model可以设置这个值
可以通过模板引擎读取值
<div th:text="${name}"></div>
5 配置文件介绍
读取resource/application.properties\ application.yml
端口号配置,项目前缀配置
yml
server:
port: 8080
servlet:
context-path: /api
注意
冒号后面必须加空格
每一个层级都得对齐
properties
server.port=8085
server.servlet.context-path=/springboot
不同的环境配置
application-环境名.后缀名
指定激活环境
spring.profiles.active=dev
或者在IDEA的Edit Configurations里Active profiles里配置
6 项目打包
mvn clean package打成jar包
运行java -jar jar包名
mvn clean package
在target目录下生成一个jar包
如果想启动这个项目只要有java环境就可以了
java -jar jar包
启动项目指定参数
java -jar -Dserver.port=8082 jar包
springboot读取配置的顺序
1. 启动参数上的配置
2. jar包目录下config/application.properties
3. classpath:application.properties
4. classpath:application.yml
7 集成JDBCTemplate
7.1 引入依赖
<!-- 添加mysql jdbc依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 添加springboot jdbcTemplate依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
7.2 添加配置文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/erp16?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
dbcp2:
max-idle: 20
min-idle: 10
7.3 注入JDBCTemplate执行sql语句
@Controller
@AllArgsConstructor
public class DemoController {
private final JdbcTemplate jdbcTemplate;
@RequestMapping("index")
public String index(String name, Model model) {
model.addAttribute("name", name);
return "index";
}
@RequestMapping("user")
public String test() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from t_user");
System.out.println(list);
return "user";
}
}
7.4 前端循环渲染
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="ch">
<head>
<meta charset="UTF-8">
<title>用户界面</title>
</head>
<body>
用户界面
<table>
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<tr th:each="item:${list}">
<td
th:text="${item.id}"></td>
<td
th:text="${item.username}"></td>
<td
th:text="${item.password}"></td>
</tr>
</tbody>
</table>
</body>
</html>
8 自动打开浏览器
package com.tledu.springboot01.core;
import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class OpenBrowser implements CommandLineRunner {
private final Environment environment;
@Override
public void run(String... args) throws Exception {
System.out.println("应用已经准备就绪 ... 启动浏览器并自动加载指定的页面 ... ");
try {
String port = environment.getProperty("server.port");
String contextPath = environment.getProperty("server.servlet.context-path");
//指定自己项目的路径
Runtime.getRuntime().exec("cmd /c start http://localhost:"+port+contextPath);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}