Springboot——初体验

Posted 名字真的很急用

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot——初体验相关的知识,希望对你有一定的参考价值。

1.Spring Boot优点

1.1 Create stand-alone Spring applications
           创建独立的spring应用

1.2Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
     内嵌web服务器
1.3Provide opinionated 'starter' dependencies to simplify your build configuration
    自动的依赖starter,简化我们构建配置

1.4Automatically configure Spring and 3rd party libraries whenever possible
    自动配置自己的应用和第三方的应用

1.5Provide production-ready features such as metrics, health checks, and externalized configuration
     提供生产级别的监控、进行健康检查、以及外部配置

1.6Absolutely no code generation and no requirement for XML configuration
  无代码生成,不需要编写 xml配置

Spring Boot 是整合Spring技术栈一站式框架
Spring Boot 是一个非常优秀的脚手架

2.Spring Boot缺点
Spring boot 版本帝 ,作为开发人员需要及时关注新功能的出现
封装太深,内部原理复杂,不容易精通

简单的程序案列
application程序的入口

package com.application;

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);
    


POJO类

package com.application.pojo;

import java.io.Serializable;

public class Books implements Serializable 

    private  int id;
    private String name;
    private String author;
    private double money;

    public Books() 
    

    public Books(int id, String name, String author, double money) 
        this.id = id;
        this.name = name;
        this.author = author;
        this.money = money;
    

    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getAuthor() 
        return author;
    

    public void setAuthor(String author) 
        this.author = author;
    

    public double getMoney() 
        return money;
    

    public void setMoney(double money) 
        this.money = money;
    

    @Override
    public String toString() 
        return "Books" +
                "id=" + id +
                ", name='" + name + '\\'' +
                ", author='" + author + '\\'' +
                ", money=" + money +
                '';
    

简单的一个controller类

package com.application.Controller;


import com.application.pojo.Books;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Controller
public class firstbook 
    @RequestMapping("/book")
    public String bok(Model model)
        Books books1 = new Books(1,"math","tom",12.0);
        Books books2 = new Books(2,"PE","tom",13.0);
        Books books3 = new Books(3,"china","tom",14.0);
        Books books4 = new Books(4,"physice","tom",15.0);

        List<Books> list = new ArrayList();
        list.add(books1);
        list.add(books2);
        list.add(books3);
        list.add(books4);

        model.addAttribute("bok",list);

        return "books";
    

配置文件

  • 一是利用thymeleaf
#端口号
server.port=8082
#项目名
server.servlet.context-path=/springboot_test02
#是否开启缓存
spring.thymeleaf.cache=true
#检查模板是否存在
spring.thymeleaf.check-template=true
#检查模板的位置是否存在
spring.thymeleaf.check-template-location=true
#设置模板的编码
spring.thymeleaf.encoding=UTF-8
#设置模板的位置
spring.thymeleaf.prefix=classpath:/templates/
#设置内容
spring.thymeleaf.servlet.content-type=text/html
#设置后缀
spring.thymeleaf.suffix=.html
  • 二是利用freemarker
server.port=8082
#server.servlet.context-path=/springboot_test03
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>application</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>application</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <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>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>

        </dependency>

    </dependencies>

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

</project>

最后就是我们的前端页面

  • 一是利用thymeleaf
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 javascript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</head>
<body>
<table class="table table-striped">
    <tr>
        <td>ID</td>
        <td>姓名</td>
        <td>作者</td>
        <td>价格</td>
    </tr>





<#list bok as book>

    <tr>
        <td>$book.id</td>
        <td>$book.name</td>
        <td>$book.author</td>
        <td>$book.money</td>
    </tr>
    </#list>

</table>
</body>
</html>
  • 二是利用freemarker
<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>图书</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</head>
<body>
<table class="table table-striped">
    <tr>
        <td>ID</td>
        <td>姓名</td>
        <td>作者</td>
        <td>价格</td>
    </tr>


    <tr th:each="boo:$bok">
        <td th:text="$boo.id"></td>
        <td th:text="$boo.name"></td>
        <td th:text="$boo.author"></td>
        <td th:text="$boo.money"></td>
    </tr>


</table>
</body>
</html>

以上是关于Springboot——初体验的主要内容,如果未能解决你的问题,请参考以下文章

Springboot——初体验

SpringBoot初体验及原理解析

SpringBoot 搭建微服务初体验

SpringBoot基础篇- 介绍及HelloWorld初体验

SpringBoot 初体验

23-springboot系列: Memcached初体验