Springboot入门

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未完待续

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值