0
点赞
收藏
分享

微信扫一扫

4-7 SpringBoot

单调先生 2022-03-10 阅读 40

maven依赖 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.st</groupId>
  <artifactId>springboot1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot1</name>

  <properties>
    <!-- 输入编码 -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 输出编码 -->
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!--
    设置maven-compiler-plugin插件使用的jdk版本
    如果不指明版本,maven项目会用maven-compiler-plugin默认的jdk版本来进行编译,
    就容易出现版本不匹配的问题,导致编译不通过
    -->
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <!-- jdk版本-->
    <java.version>1.8</java.version>
  </properties>

  <!-- 包括了待继承的父工程所需要的信息 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <!--springboot启动类,核心类-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- 引入springMVC -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

application.yml配置文件

  • 放在classpath下,即resource目录下。
  • yml文件要严格缩进,每次缩进空两个空格,不能用tab键缩进。
server:
  # 服务段口号
  port: 8080
  # 服务名称
  servlet:
    context-path: /boot1

启动类

  • SpringBoot规定,启动类不能放在其他类的下级目录。
  • 要运行项目时只需运行启动类的main方法即可。
package com.st;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Application
 * springboot项目的启动类
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class);
    }
}

Controller类举例

  • 其他类的写法和ssm相同,但不要放在启动类的上级目录。
package com.st.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("d")
public class DefController {

    @RequestMapping("hello")
    @ResponseBody
    public String test(){
        System.out.println("hello springboot");
        return "success";
    }
}
举报

相关推荐

0 条评论