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;}@Overridepublic 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


<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.0modelVersion><parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>2.4.12version><relativePath/> parent><groupId>comgroupId><artifactId>applicationartifactId><version>0.0.1-SNAPSHOTversion><name>applicationname><description>Demo project for Spring Bootdescription><properties><java.version>1.8java.version>properties><dependencies><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-testartifactId><scope>testscope>dependency><dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-freemarkerartifactId>dependency>dependencies><build><plugins><plugin><groupId>org.springframework.bootgroupId><artifactId>spring-boot-maven-pluginartifactId>plugin>plugins>build>project>

最后就是我们的前端页面

  • 一是利用thymeleaf
DOCTYPE html><html lang="en">
<head><meta charset="UTF-8"><title>图书title><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"><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"><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>IDtd><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><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"><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"><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>IDtd><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>


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部