In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to use JDBC to connect to MySQL in Spring Boot. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
01. Initialize MySQL database
Now that you want to connect to MySQL, you need to install the MySQL service on your computer (this article skips for a moment) and create databases and tables.
BASE `springbootdemo`; DROP TABLE IF EXISTS `mysql_ datasource`; CREATE TABLE `springbootdemo` (`id` varchar (64) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
02. Create a Spring Boot project using Spring Initlallzr
Creating a Spring Boot project is very simple, just through Spring Initlallzr (start.spring.io/).
Check Lombok, Web, MySQL Driver, Actuator, JDBC and other five dependencies.
1) Lombok is a Java utility that can be used to help developers eliminate some redundant code in Java, such as generating getter/setter through annotations. You need to install the plug-in in IDE before using it.
2) Web shows that the project is a Web project, so it is convenient for us to implement it directly through URL.
3) MySQL Driver: the drive that connects to the MySQL server.
4) Actuator is an integrated function provided by Spring Boot for self-examination and monitoring of the application system. You can view the details of the application configuration, such as automation configuration information, created Spring beans and some environment attributes.
5) JDBC: in this article, we use JDBC to connect and operate the database.
Once the options are selected, you can click the [Generate] button to generate an initialized Spring Boot project. What is generated is a compressed package, which needs to be unzipped when imported into IDE.
03. Edit application.properties file
After the project is successfully imported, wait for the Maven to download the dependency, edit the application.properties file after completion, and configure the MySQL data source information.
Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdemo spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
1) spring.datasource. It is a fixed format.
2) URL is the connection address of MySQL.
3) username is the user name for accessing the database.
4) password is the access password of the database.
5) driver-class-name is used to specify the drive of the database. Alternatively, Spring Boot automatically matches the drive based on URL (with the mysql keyword).
04. Edit the Spring Boot project
To make it easier for us to operate, we edit the SpringBootMysqlApplication class to add the following.
@ SpringBootApplication @ RestController public class SpringBootMysqlApplication {@ Autowired private JdbcTemplate jdbcTemplate; @ RequestMapping ("insert") public String insert () {String id = UUID.randomUUID () .toString (); String sql = "insert into mysql_datasource (id,name) values ('" + id+ ", 'Silent King II')"; jdbcTemplate.execute (sql); return "insert complete";}}
1) @ SpringBootApplication, @ RestController, @ RequestMapping annotations have been introduced in [previous article] (), so I won't repeat them here.
2) @ Autowired: as the name implies, it is used to automatically assemble Java Bean.
3) the operation of the database by JdbcTemplate:Spring is deeply encapsulated on jdbc, and the DataSource can be registered in JdbcTemplate by using the injection function of Spring. JdbcTemplate provides four common methods.
①, execute () method: used to execute any SQL statement.
②, update () method: used to execute SQL statements such as add, modify, delete, etc.
③, query () method: used to execute query-related SQL statements.
④, call () method: used to execute SQL statements related to stored procedures and functions.
In this example, we use the execute () method to insert a row of data {id:uuid, name:' Silent King 2'} into the mysql_datasource table.
05. Run the Spring Boot project
Next, we run the SpringBootMysqlApplication class directly, and such a SpringBoot project starts successfully.
At this point, we can type http://localhost:8080/insert directly into the browser's URL to test whether the insert statement of MySQL is executed successfully. It's a pity that something went wrong.
I don't know what to do This requires us to explicitly specify the time zone in the connection string and modify the spring.datasource.url to the following.
Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdemo?serverTimezone=UTC
After rerunning the project, visit it again and find that the data has been inserted successfully.
To make sure the data is really plugged in, let's take a look at it through Navicat, a powerful database management and design tool.
The situation is not good. The Chinese code is garbled. I don't know what to do We need to explicitly specify the character set in the connection string and modify the spring.datasource.url to the following.
Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootdemo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
After rerunning the project, I visited again and found that the Chinese was no longer garbled.
On Spring Boot how to use JDBC to connect to MySQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.