首页 > 其他分享 >Spring Boot study

Spring Boot study

时间:2022-09-03 18:33:56浏览次数:43  
标签:String Spring study Boot springframework reader import public org

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.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </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>

src/main/resources/banner.txt

////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^xiejiaohui^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                    //
////////////////////////////////////////////////////////////////////

src/main/resources/application.properties

server.port=8001

src/main/resources/static/style.css

body {
    background-color: #cccccc;
    font-family: arial, helvetica, sans-serif;
}

.bookHeadline {
    font-size: 12pt;
    font-weight: bold;
}

.bookDescription {
    font-size: 10pt;
}

label {
    font-weight: bold;
}

src/main/resources/templates/readingList.html

<html>
<head>
    <title>Reading List</title>
    <link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
<body>
<he>Your Reading List</he>
<div th:unless="${#lists.isEmpty(books)}">
    <dl th:each="book : ${books}">
    <dt class="bookHeadline">
        <span th:text="${book.title}">Title 标题</span> by
        <span th:text="${book.author}">Author 作者</span>
        (ISBN: <span th:text="${book.isbn}">ISBN</span>)
    </dt>
    <dd class="bookDescription">
        <span th:if="${book.description}"
               th:text="${book.description}">Description 描述</span>
        <span th:if="${book.description eq null}">
            No description available
        </span>
    </dd>
    </dl>
</div>
<hr/>
<h3>Add a book</h3>
<form method="POST">
    <label for="title">标题 Title:</label>
      <input type="text" name="title" size="50"></input><br/>
    <label for="author">作者 Author:</label>
      <input type="text" name="author" size="50"></input><br/>
    <label for="isbn">ISBN:</label>
      <input type="text" name="isbn" size="15"></input><br/>
    <label for="description">Description</label>
      <textarea name="description" cols="80" rows="5"></textarea><br/>
      <input type="submit"></input>
</form>
</body>
</html>
com.example.demo.Book.java
package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String reader;
    private String isbn;
    private String title;
    private String author;
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getReader() {
        return reader;
    }

    public void setReader(String reader) {
        this.reader = reader;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


}
com.example.demo.DemoApplication.java
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
com.example.demo.ReadingListController.java
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/readingList")
public class ReadingListController {

    private ReadingListRepository readingListRepository;

    @Autowired
    public ReadingListController(ReadingListRepository readingListRepository) {
        this.readingListRepository = readingListRepository;
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.GET)
    public String readersBooks(@PathVariable("reader") String reader, Model model) {
        List<Book> readingList = readingListRepository.findByReader(reader);
        if (readingList != null) {
            model.addAttribute("books", readingList);
        }
        return "readingList";
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.POST)
    public String addToReadingList(@PathVariable("reader") String reader, Book book) {
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:/readingList/{reader}";
    }

}
com.example.demo.ReadingListRepository.java
package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReadingListRepository extends JpaRepository<Book, Long> {

    List<Book> findByReader(String reader);

}

 

标签:String,Spring,study,Boot,springframework,reader,import,public,org
From: https://www.cnblogs.com/xiejh/p/16653282.html

相关文章

  • springboot简单使用(4)
    1.9第九章Thymeleaf模版1.9.1认识ThymeleafThymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语......
  • 引入版本问题 spring-cloud版本:Finchley.RC1 和 spring-boot版本:2.0.4.RELEASE No
    springcloud和springboot引入版本问题spring-cloud版本:Finchley.RC1spring-boot版本:2.0.4.RELEASE改为:spring-cloud版本:Finchley.RELEASEspring-boot版本:2.0.3.R......
  • spring cloud 多模块打包部署解决坑
    springcloud多模块打包部署解决坑 前置条件笔者使用IntellijIDEA进行SpringCloud项目创建和部署IntellijIDEA版本:IntelliJIDEA2019.1.3(UltimateEdition)......
  • SpringBoot集成Dubbo和Zookeeper
    15、SpringBoot集成Dubbo和Zookeeper15.1、分布式理论什么是分布式系统?在《分布式系统原理与范型》一书中有如下定义:“分布式系统是若干独立计算机的集合,这些计算机对......
  • Docker基础知识 (8) - 使用 Docker 部署 SpringBoot + MariaDB(MySQL)项目
    本文在“ Docker基础知识(7)-使用Docker部署SpringBoot项目”里的SpringbootWebDocker项目的基础上,添加JDBC、MariaDB和MyBatis相关依赖包和数据库操作代......
  • SpringSecurity - 企业应用的安全框架
    参考资料1、官方网站:https://docs.spring.io/spring-security/reference/index.html认证(Authentication)用户认证:验证某个用户是否为系统中的合法主体,也就是说用户能否访......
  • CVE-2022-22978 Spring-Security 漏洞复现
    1说明在SpringSecurity中使用RegexRequestMatcher且规则中包含带点号的正则表达式时,攻击者可以通过构造恶意数据包绕过身份认证2环境搭建环境搭建地址可以参考如下的......
  • springboot项目打jar包运行出现乱码
    springboot项目打jar包运行出现乱码1、控制台乱码在运行程序之前,执行chcp65001,即可将控制台切换到utg8编码格式,之后在继续运行jar包即可。chcp650012、数据库乱码......
  • Spring 中使用自定义的 ThreadLocal 存储导致的坑
    Spring中有时候我们需要存储一些和Request相关联的变量,例如用户的登陆有关信息等,它的生命周期和Request相同。一个容易想到的实现办法是使用ThreadLocal:pub......
  • SpringBoot 异步输出 Logback 日志
    一、介绍1.1LogbackLogback是由log4j创始人设计的另一个开源日志组件,它分为下面下个模块:logback-core:其它两个模块的基础模块logback-classic:它是log4......