SpringBoot summary and springboot Summary

Source: Internet
Author: User
Tags spring initializr

SpringBoot summary and springboot Summary
Directory

1. Create a project

2.

How to create a project in Springboot Step by Step open Intellij Idea editor to create a new project
  • Click Create New Project
  • Select Spring Initializr on the left
  • On the right, select default to create from https://start.spring.io and click Next
  • On the following page, enter relevant information, such as the group name or project name, and specifyGradleProject, click Next
  • On this page, add the Spring plug-in we need in the project. In general, click Next on Web/Jpa/Thymeleaf/MySQL.
  • Then, enter the project name, specify the Save directory, and click Finish to initialize the Gradle project.
Configure our Springboot Parameters
  • Modify the configuration of our build. gradle and refresh it.
    apply plugin: 'idea'ext['thymeleaf.version'] = '3.0.9.RELEASE'ext['thymeleaf-layout-dialect.version'] = '2.2.2'
  • Modify basic parameters in application. properties
    server.port=6666spring.datasource.url=jdbc:mysql://localhost:3306/another_springboot_dbspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=updatespring.thymeleaf.mode=htmlspring.thymeleaf.cache=falsespring.messages.encoding=GBK
  • Enable MySQL service and create related databases in MySQL
    create database another_springboot_db
  • Enable our server
    localhost:6666
Create the Book module and create entity/dao/controller
  • Create model/Author. java
    @Entitypublic class Author {    @Id    @GeneratedValue    private Long id;    private String name;    private String telephone;    @OneToMany(mappedBy = "author")    private List<Book> books;    //...}
  • Create model/Book. java
  • Create dao/AuthorDAO. java
    public interface AuthorDAO extends JpaRepository<Author, Long> {}
  • Create dao/BookDAO. java
  • Create web/AuthorController. java
    @Controllerpublic class AuthorController {    @Autowired    private AuthorDAO authorDAO;    @GetMapping("/author_add")    public String add() {        return "book/add_author";    }    @PostMapping("/author_add")    public String store(Author author) {        authorDAO.save(author);        return "redirect:author_add";    }}
  • Create web/BookController. java
Create the Thymeleaf template Layout for the Book Module
  • First, create the resource file messages. properties under the resource Directory.
  • Add the public parameters to be used in the resource file.
  • Create a public layout file layout/book.html
Example of adding a function to Book
  • Create two add/save methods in BookController to present the added form and process the form request respectively.
    @GetMapping("/add")public String add(Model model) {    model.addAttribute("authors", authorDAO.findAll());    return "book/add";}@PostMapping("/add")public String save(Book book, Model model) {    bookDAO.save(book);    return "redirect:index";}
  • Create a form page for the newly added book and use the layout extension we have defined
    <! DOCTYPE html> Add verification for the new page
      
       
    • First, it is recommended to use a Java verification framework for JSR-303 specifications
    • Second, use the errors. reject () method for manual verification in the Controller for other simple errors.
      If (book. getAuthor () = null | book. getAuthor (). getId () <1) {errors. rejectValue ("author", null, "Enter the author's information! ");} If (errors. hasErrors () {model. addattriors (" authors ", authorDAO. findAll (); return" book/add ";}
    • Then, you can:
      • Customize verification annotations and verification classes for the JSR Standard
      • Custom Spring validator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.