(1) spring-boot基础环境搭建

1、开发环境搭建

  • JDK版本:Oracle JDK 1.8+
  • 构建工具:Apache Maven 3.3.0+ (bin.zip)
  • 开发工具:IntelliJ IDEA 2017.2.6

2、创建spring-boot工程

  • 使用idea创建新project:File->New->Project
9795603-838a5eae21169607.png
新建项目.png
  • 填写项目基本信息:
9795603-3787489b159954a9.png
项目信息.png
  • 选择maven依赖:本示例创建web项目,后期需要其他依赖再手动添加
9795603-5bb26efa3d371755.png
maven依赖.png
  • 使用默认项目名称,完成项目创建
    9795603-800db432238a595e.png
    完成创建.png

3、项目说明

  • 目录结构
9795603-1042f5e91a947e4e.png
目录结构.png

目录结构遵循maven的一般目录结构,java中为代码;resources中为资源,包括应用配置及页面等静态资源等;

  • 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>

    <groupId>com.springboot.demo</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <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>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

(1)、parent标签,指明SpringBoot版本为2.0.6.RELEASE。

(2)、starter-web依赖spring-boot-starter-web,它内置了tomcat容器。

(3)、maven打包插件依赖,spring-boot-maven-plugin。

  • 启动主类
@SpringBootApplication
public class TestApplication {
       public static void main(String[] args) {
            SpringApplication.run(TestApplication.class, args)
        }
}

通过运行主类的main方法运行整个项目。

4、配置thymeleaf模板引擎

spring-boot支持如下模板引擎:

  • FreeMarker
  • Groovy
  • Thymeleaf(官方推荐)
  • Mustache

本环境以Thymeleaf为列进行web环境搭建。

  • maven依赖
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>

可以查看依赖关系,发现spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉.

  • 视图解析器配置

spring-boot很多配置都有默认配置,比如默认页面映射路径为classpath:/templates/*.html,同样静态文件路径为classpath:/static/,在application.properties中可以配置thymeleaf模板解析器属性。

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

具体可以配置的参数可以查看 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类,上面的配置实际上就是注入到该类中的属性值。

  • 编写控制器
@Controller
@RequestMapping("/")
public class TestController {

    @RequestMapping("")
    public String index(){
        return "/index.html";
    }
}
  • 前端页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎</title>
</head>
<body>
    <h4>欢迎使用spring-boot!</h4>
</body>
</html>
  • 运行效果

spring-boot默认web端口为8080,效果如下:

9795603-09f1ecc38b387882.png
运行效果.png

5、完整项目

完整目录结构:

9795603-6c319a1223e1013b.png
完整项目.png

相关阅读:
spring-boot配置详解【https://www.jianshu.com/p/1d037ab638ef
spring-boot+druid+mybatis环境搭建【https://www.jianshu.com/p/e6c9e9945e45
spring-boot+logback+log4j2+MDC【https://www.jianshu.com/p/51da61a425ba

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值