栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot——初体验

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.12
         
    
    com
    application
    0.0.1-SNAPSHOT
    application
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.boot
            spring-boot-starter-freemarker

        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


最后就是我们的前端页面

  • 一是利用thymeleaf




    
    图书
    
    

    
    

    
    








<#list bok as book>

    
ID 姓名 作者 价格
${book.id} ${book.name} ${book.author} ${book.money}
  • 二是利用freemarker




    
    图书
    
    

    
    

    
    


ID 姓名 作者 价格
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345987.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号