美文网首页
手动创建SpringBoot项目

手动创建SpringBoot项目

作者: 小熊哈哈 | 来源:发表于2020-03-15 15:57 被阅读0次

    Idea创建SpringBoot项目2种方式

    1.Spring Initializr
    2.Maven
    

    Spring Initializr特点:

    1.创建方便快捷
    2.有时候Initializr服务不可用,创建会失败
    3.傻瓜式配置,开发者不清楚创建的过程与配置
    4.创建时不能控制版本
    

    Maven特点:

    1.创建目方便灵活
    2.所有配置都需要自己添加
    3.有利于熟悉配置的详情
    

    基于以上特点分析,做手动创建SpringBoot项目实录

    1.创建maven项目

    2.pom文件配置parent节点

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    

    3.pom文件配置properties

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    

    4.pom文件配置国内maven仓库

    <repositories>
            <repository>
                <id>alimaven1</id>
                <name>aliyun maven 1</name>
                <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
            </repository>
            <repository>
                <id>alimaven2</id>
                <name>aliyun maven 2</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            </repository>
            <repository>
                <id>central1</id>
                <name>Central Repository 1</name>
                <url>http://repo1.maven.apache.org/maven2/</url>
            </repository>
            <repository>
                <id>central2</id>
                <name>Central Repository 2</name>
                <url>http://repo2.maven.apache.org/maven2/</url>
            </repository>
            <repository>
                <id>spring-snapshots</id>
                <url>http://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <url>http://repo.spring.io/milestone</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>spring-snapshots</id>
                <url>http://repo.spring.io/snapshot</url>
            </pluginRepository>
            <pluginRepository>
                <id>spring-milestones</id>
                <url>http://repo.spring.io/milestone</url>
            </pluginRepository>
        </pluginRepositories>
    

    5.pom文件配置依赖

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.5.RELEASE</version>
            </dependency>
        </dependencies>
    

    6.创建程序入口

    在/src/main/java/目录创建启动类

    package com.wxs.springweb;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    7.创建配置文件

    在/src/main/resources/目录创建配置文件application.yml

    server:
      port: 8000
    
    spring:
      profiles:
        active: dev
      mvc:
        view:
          prefix: classpath:/templates
          suffix: .html
        static-path-pattern: /static/**
      thymeleaf:
        mode: HTML
        encoding: utf-8
        servlet:
          content-type: text/html
        cache: false
      servlet:
        multipart:
          enabled: true
          max-file-size: 20MB
          max-request-size: 20MB
      session:
        timeout: 15m
      resources:
        cache:
          cachecontrol:
            cache-private: true
            max-age: 1m
            must-revalidate: true
    

    8.添加控制器

    添加IndexController

    package com.wxs.springweb.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/index")
    public class IndexController {
    
        @GetMapping("/page1")
        String Page1() {
            return "/index/page1";
        }
    }
    
    

    9.添加页面

    在src.main.resources.templates.index下添加页面page1.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Page1</title>
    </head>
    <body>
      <h1>Page1</h1>
    </body>
    </html>
    

    10.访问

    http://localhost:8000/index/page1

    手动配置基本的SpringBoot项目结束,如果需要更多的配置,比如Spring MVC、Security、shiro、Redis、MyBatis、MySQL等可参考相关文档配置。

    相关文章

      网友评论

          本文标题:手动创建SpringBoot项目

          本文链接:https://www.haomeiwen.com/subject/kgtsehtx.html