spring boot学习笔记(二)创建spring boot项目

用eclipse(需要用高版本,要不然弄不出来):new →Spring Sarter Project

用IDEA:一般默认

一般默认

 入门级的先

剩下的一般默认。。。

一、项目至少有下面的东西,里面一共俩类,带test的是测试类

二、先看依赖pom,点开依赖看下就发现为啥不用自己配置基本pom文件了——它自带一部分基础配置

<?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>
    <!--boot的父类依赖引入,提供基本的依赖关系和插件管理-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.bwm</groupId>
    <artifactId>boot_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot_demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--提供基本的web支持组件,默认tomcat容器等依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--提供测试单元组件等依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“java -jar”命令就可以直接运行,不过需要指定运行的main方法,父依赖给他指定了
            所以如果没有引入上面的父依赖,就需要自己指定,就是下面的注释,类路径指向上面的正式类-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--
                <configuration>
                    <mainClass>cn.xx.xx</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                -->
            </plugin>
        </plugins>
    </build>

</project>

三、打开正式类,直接启动main方法,浏览器访问就可以,然后。。。

 

出来个错误界面!还没写东西很正常

四、来个栗子

package cn.bwm.boot_demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基于spring的东西,跟spring注解差不多
 * @author: zyt
 * @date: 2019/7/23 22:54
 */
@RestController
@RequestMapping(value = "/boot_demo")
public class HelloController {

    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    public String sayHello(){
        return "how are you?";
    }
}

猜你喜欢

转载自www.cnblogs.com/of-course/p/11235041.html