How to connect SpringBoot to MySQL to get data and write back-end interface
This article will explain in detail how SpringBoot connects to MySQL to get data and writes the back-end interface. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. New project
two。 Add dependency
Mysql mysql-connector-java 8.0.20 org.springframework spring-jdbc 5.3.8 org.springframework spring-tx 5.3.8 org.springframework spring-aop 5.3.8
Create DriverManagerDataSource and JdbcTemplate objects in 3.spring Container 3.1 method of loading spring container in springboot 1. Create spring container file under resource
Resource-> new---- > Directory---- > new Directory (application)
two。 Define a normal class and add the above comments to the class to automatically load the spring container after springboot startup
4. Create an object class and control the class Book
Note the @ Data annotation here, which is a part of lombok and is mainly used to generate get,set methods automatically at compile time, so there is no need for us to write get set methods manually in this class, which reduces our workload. It is very convenient and highly recommended.
BookDao
TestController
Application.properties
5. Start the MySQL database
You can refer to the previous article
Install MySQL8.0 and visualize the operation in Navicat
6. Run the test
As shown in the following figure, the operation is successful
Enter it in the browser to see if it is successful.
Description:
8080 and springboot reference application.properties
GetbookList is customized in RequestMapping in TestController
At this point, the SpringBoot back-end interface is written.
This is the end of the article on "how to connect SpringBoot to MySQL to obtain data and write back-end interface". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.