1. 程式人生 > 實用技巧 >SpringBoot入門一(能夠用spring boot搭建專案)

SpringBoot入門一(能夠用spring boot搭建專案)

目的:可以搭建一個簡單的SpringBoot專案

步驟:

1.建立工程(普通的maven專案)

2.新增依賴(啟動器依賴,Spring-boot-start-web)

3.建立啟動器

4.建立處理器(controller)

5.測試訪問地址

========================================

1.專案建立就建立一個普通的maven專案

2.新增依賴pom.xml(SpringBoot啟動器依賴)

<?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> <!--SpringBoot父工程--> <parent> <artifactId>spring-boot-starter-parent</
artifactId> <groupId>org.springframework.boot</groupId> <version>2.3.4.RELEASE</version> </parent> <groupId>org.cc8w</groupId> <artifactId>javaboot_test</artifactId> <version>1.0-SNAPSHOT</version> <!--
java編譯版本,父工程已經指定了這樣的變數--> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--SpringBoot Web啟動器(不用寫版本跟隨父版本)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

3.建立啟動器

package com.cc8w;

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

/**
 * spring boot 工程都有一個啟動引導類,
 * 並且新增 @SpringBootApplication 組合註解
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

4.建立處理器(controller)

package com.cc8w.home.controller;

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

@RestController
@RequestMapping("home/test")
public class JdbcTest {

    @RequestMapping("jtest")
    public String jTest(){

        System.out.println("jTest22");
        return "null";
    }


}

5.測試訪問地址(專案目錄及結果)