Springboot是什么?
springboot是spring家族中微型框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。Springboot本身并不是提供spring框架的核心特性扩展功能,知识用于快速、敏捷地开发新一代基于spring框架的应用程序。
它同时集成了大量常用的第三方库配置(例如Jackson,jdbc,Mongo,Redis,Mail等),大部分的SpringBoot应用只需要非常少量的配置代码,开发者能够更加专注于业务逻辑。
特性:
1、创建独立的spring应用程序
2、直接嵌套tomcat
3、简化构建配置
4、绝对没有代码生成,没有xml配置
5、敏捷式开发
使用idea创建springboot项目
可勾选需要的配置
创建完成后目录如下
java源文件夹中的Springboot01Application.java是整个项目的启动类
static:存放的是静态资源的文件
templetes:存放的项目所需的页面
application.properties里面存放的是项目的全局配置信息
创建一个controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello spring boot!";
}
}
配置文件application.properties中写入
server.port=8081
server.servlet.context-path=/springboot
运行后
注意:启动项目时报
异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
原因1:
Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包
原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.
原因2:
在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题:
当pom文件下的spring-boot-starter-paren版本高时使用:
spring.mvc.view.prefix/spring.mvc.view.suffix
当pom文件下的spring-boot-starter-paren版本低时使用:
spring.view.prefix/spring.view.suffix
原因3:
控制器的URL路径书写问题
@RequestMapping(“xxxxxxxxxxxxxx”)
实际访问的路径与”xxx”不符合.
自定义属性
application.properties中配置
server.port=8081
server.servlet.context-path=/springbootmysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/db_travel?useUnicode=true&characterEncoding=UTF-8
mysql.username=root
mysql.password=123
controller中
@Value("${mysql.driver}") private String driver; @Value("${mysql.url}") private String url; @Value("${mysql.username}") private String userName; @Value("${mysql.password}") private String password;
@RequestMapping("/mysql") public String mysql(){ return this.driver+"<br>"+this.url+"<br>"+this.userName+"<br>"+this.password+"<br>"; }
运行如下:
属性封装类
创建一个类entity类
@ToString
@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlEntity {
private String driver;
private String url;
private String userName;
private String password;
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
controller中
@Resource
private MysqlEntity mysqlEntity;
@RequestMapping("/mysql1")
public MysqlEntity mysql1(){
return this.mysqlEntity;
}
运行后:
application.properties与application.yml有什么区别
yml文件的好处,天然的树状结构,一目了然,实质上与properties查不多。
application.properties
server.port=8081
server.servlet.context-path=/springboot
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/db_travel?useUnicode=true&characterEncoding=UTF-8
mysql.username=root
mysql.password=123
application.yml
server:
port: 8081
servlet:
context-path: /springboot
mysql:
url: jdbc:mysql://localhost:3306/db_travel?useUnicode=true&characterEncoding=UTF-8
driver: com.mysql.jdbc.Driver
user-name: root
password: 123
springboot未完待续