Home > Database > Mysql Tutorial > body text

How SpringBoot connects to MySQL to obtain data and write back-end interface

王林
Release: 2023-05-30 12:26:17
forward
1473 people have browsed it

1. Create a new project

How SpringBoot connects to MySQL to obtain data and write back-end interface
How SpringBoot connects to MySQL to obtain data and write back-end interface
How SpringBoot connects to MySQL to obtain data and write back-end interface

2.Add dependencies

How SpringBoot connects to MySQL to obtain data and write back-end interface

<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>
Copy after login

3. Create DriverManagerDataSource and JdbcTemplate objects in spring container 3.1 Method to load spring container in springboot 1. Create spring container file under resource

resource ---->new- --->Directory---->new Directory(application)
How SpringBoot connects to MySQL to obtain data and write back-end interface
How SpringBoot connects to MySQL to obtain data and write back-end interface

##
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <!-- 1.1.数据库驱动 -->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <!-- 1.2.连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&serverTimezone=UTC"/>
        <!-- 1.3.连接数据库的用户名 -->
        <property name="username" value="root"></property>
        <!-- 1.4.连接数据库的密码 -->
        <property name="password" value="root"></property>

    </bean>

    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
Copy after login
2. Define a common class and add Add the above annotation to automatically load the spring container after springboot starts

How SpringBoot connects to MySQL to obtain data and write back-end interface

4. Create an object class and control class Book

Pay attention to the @Data annotation here , it is a part of lombok, its main function is to automatically generate get and set methods during compilation, so we do not need to manually write get and set methods in this class, reducing our workload, very convenient, highly recommended

How SpringBoot connects to MySQL to obtain data and write back-end interface

BookDao

How SpringBoot connects to MySQL to obtain data and write back-end interface

TestController

How SpringBoot connects to MySQL to obtain data and write back-end interface##application.properties

How SpringBoot connects to MySQL to obtain data and write back-end interface5. Start the MySQL database

You can refer to the previous article

Install MySQL8.0 and operate visually in Navicat


How SpringBoot connects to MySQL to obtain data and write back-end interface6. Run the test

As shown below, the operation is successful

How SpringBoot connects to MySQL to obtain data and write back-end interfaceEnter in the browser to see if the acquisition is successful

How SpringBoot connects to MySQL to obtain data and write back-end interface

Note:
8080 and springboot refer to application.properties

getbookList is customized in RequestMapping in TestController

At this point, the SpringBoot backend interface is written alright

The above is the detailed content of How SpringBoot connects to MySQL to obtain data and write back-end interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!