话不多说,直接开始我们的Hello world程序
官网创建springBoot项目
- https://start.spring.io/
- 点击生成按钮得到压缩包解压,然后idea中导入,右下角提示选择import导入即可
- 在主类同级目录下,新建开发的各种个层级的包,编写我们开发的代码
- /controller/HelloController
springBoot帮我们下载了很多jar依赖包,我们都无需配置tomcat环境,启动后即可访问localhost:8080
http://localhost:8080/hello1 页面呈现:hello world
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello1")
public String hello(){
return "hello world";
}
}
springBoot创建一个项目太简单了,它就是一个spring的简化版操作框架,帮我们省去了很多开发流程,以后我们只需要潜心编写我们conrtoller,service,pojo,dao层的代码即可。
idea创建springBoot项目
file->project->Spring Initializr->next->最后选择web下的spring web依赖,同普通创建maven项目一样,不在赘述
如果出现问题,大概率是这个问(解决办法)
yaml简单语法(待续)
JSR303校验
若springboot不支持,则要导入一下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
配合使用@Validated注解和类似@Email,@Null等注解
@Component
@ConfigurationProperties(prefix = "student")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Validated
public class Student {
@Null()
private String name;
private int age;
private boolean happy;
private Map<String,Object> map;
private List<Object> list;
private Date birth;
private Dog dog;
@Email(message = "请输入邮箱格式")
private String email;
在yaml属性配置文件中要符合该校验规范,否则会报错
常见校验注解
多套环境搭建
在单一文件中application.yaml配置开发,测试,生产环境的端口号
使用spring.profiles.active来指定当前要使用的环境
每个环境之间必须使用 ‘- - -’ 连接起来,否则会报错
server:
port: 8080
spring:
profiles:
active: test
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
thymeleaf模板引擎
- 导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
- 创建一个controller
@controller是返回一个ModelAndView,@Restcontroller注解是返回一个字符串
@Controller
public class TyhmeleafController {
@RequestMapping("/greeting")
public ModelAndView thymeleaf(ModelAndView mv){
mv.setViewName("/greeting");
mv.addObject("hello","欢迎来到thymeleaf模板");
mv.addObject("city", Arrays.asList("北京","上海","广州","深圳"));
return mv;
}
}
- 模板/greeting.html文件放在/resources/templates文件目录下
在html声明语言的标签中要引入thymeleaf;
xmlns=“http://www.w3.org/1999/xhtml” xmlns:th=“http://www.thymeleaf.org”;“这个坑我帮大家踩了”
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2 th:text="${hello}"></h2>
<ul>
<li th:each="city:${city}" th:text="${city}"></li>
</ul>
</body>
</html>
前后端分离,前端使用vue就好啦,这个语法貌似神似vue啊,究竟是谁借鉴的谁?
- 效果图
yaml给属性赋值@ConfigurationProperties
注解:@ConfigurationProperties(prefix = “student”),并在测试类中再次理解spring的自动装配
/resources/application.yaml文件中:
注意:冒号后的空格一定不能少
#注意冒号后面一定要有空格
student:
name: 黄杰
age: 21
happy: true
map: {身高:170,体重:120}
list:
- 唱歌
- 跳舞
- 打篮球
birth: 1997/10/01
dog:
name: 旺财
age: 1
dog:
age: 1
name: 旺财
实体类:
/student.java
@Component
@ConfigurationProperties(prefix = "student")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private String name;
private int age;
private boolean happy;
private Map<String,Object> map;
private List<Object> list;
private Date birth;
private Dog dog;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", map=" + map +
", list=" + list +
", birth=" + birth +
", dog=" + dog +
'}';
}
}
/dog.java
@Component
@ConfigurationProperties(prefix = "dog")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dog {
private String name;
private int age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类
/DemoApplicationTests
@SpringBootTest
class DemoApplicationTests {
@Autowired
private Student student;
@Autowired
private Dog dog;
@Test
void contextLoads() {
System.out.println("---------旺财信息---------");
System.out.println(dog.toString());
System.out.println("---------我的信息---------");
System.out.println(student.toString());
}
}
对比我们之前给属性赋值:可以借助注解@Value(value值)
指定配置文件给属性赋值@PropertySource
注意使用classpath导入配置文件后,在对应属性上一定要配合使用${}来指定匹配字段
实体类:/user.java
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
@PropertySource(value = "classpath:user.properties")
public class User {
@Value("${username}")
private String username;
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
'}';
}
}
.resources/user.properties文件
username=root
补充几个有趣的点
- 修改默认启动端口
/resources/application.properties文件中配置:server.port=8081
springboot推荐使用yaml语法
- 更改idea中启动web服务的logo图
/resources下新建banner.txt文件,放入任何图案,重启项目即可
- 在黑窗口开启微服务
找到该jar包的文件目录后,cmd打开黑窗口 运行java -jar + jar包名