1. Create a new project2. Add dependencies<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.8</version> </dependency> 3. Create DriverManagerDataSource and JdbcTemplate objects in the spring container3.1 How to load spring container in springboot1. Create a spring container file under resource
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <!-- 1.1. Database driver--> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <!-- 1.2. URL to connect to the database --> <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/> <!-- 1.3. Username to connect to the database --> <property name="username" value="root"></property> <!-- 1.4. Password to connect to the database --> <property name="password" value="root"></property> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> 2. Define a common class and add the above annotations to the class to automatically load the spring container after springboot is started. 4. Create object class, control class BookNote the @Data annotation here. It is part of lombok. Its main function is to automatically generate get and set methods at compile time. Therefore, we do not need to manually write get and set methods in this class, which reduces our workload. It is very convenient and highly recommended. BookDao TestController application.properties 5. Start MySQL databaseYou can refer to the previous article to install MySQL 8.0 and visualize it in Navicat 6. Run the Tests As shown below, the operation is successful Enter in the browser to check whether it is successfully obtained
At this point, the SpringBoot backend interface is written This is the end of this article about SpringBoot connecting to MySQL to obtain data and write backend interfaces. For more relevant SpringBoot connection to MySQL content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML Basics_General Tags, Common Tags and Tables
>>: Share 8 very useful CSS development tools
When there are tens of thousands of records in th...
What I want to share today is to use native JS to...
This article uses examples to explain the concept...
Table of contents Overview Application scenarios ...
Preface This article was written by a big shot fr...
Table of contents 1. typeof operator 2. instanceo...
Code: Copy code The code is as follows: <!DOCTY...
As shown below: SELECT count(DISTINCT(a.rect_id))...
Must read before operation: Note: If you want to ...
Preface In the application of database, programme...
Recently the company has arranged to do some CCFA...
Table of contents 1. Global beforeEach 1. Global ...
MySQL replace and replace into are both frequentl...
1. Virtual Machine Preparation 1. Create a new vi...
The hyperlink a tag represents a link point and i...