How to connect SpringBoot to MySQL to get data and write to the backend interface

How to connect SpringBoot to MySQL to get data and write to the backend interface

1. Create a new project

insert image description here
insert image description here
insert image description here

2. Add dependencies

insert image description here

<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 container

3.1 How to load spring container in springboot

1. Create a spring container file under resource

resource ---->new---->Directory---->new Directory(application)
insert image description here
insert image description here

    <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&amp;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.

insert image description here

4. Create object class, control class Book

Note 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.

insert image description here

BookDao

insert image description here

TestController

insert image description here

application.properties

insert image description here

5. Start MySQL database

You can refer to the previous article to install MySQL 8.0 and visualize it in Navicat

insert image description here

6. Run the Tests

As shown below, the operation is successful

insert image description here

Enter in the browser to check whether it is successfully obtained

insert image description here

illustrate:
8080 and springboot reference application.properties
getbookList is customized in RequestMapping in TestController

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:
  • SpringBoot integrates Druid connection pool to connect to MySQL8.0.11
  • Teach you how to use springboot to connect to mysql and implement add, delete, modify and query
  • Implementation of SpringBoot multi-database connection (mysql+oracle)
  • Springboot configures mysql connection example code
  • Solution to the error "using password: NO" when springboot connects to mysql
  • SpringBoot connects to MYSQL database and uses JPA

<<:  HTML Basics_General Tags, Common Tags and Tables

>>:  Share 8 very useful CSS development tools

Recommend

MySQL million-level data paging query optimization solution

When there are tens of thousands of records in th...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

How to implement the observer pattern in JavaScript

Table of contents Overview Application scenarios ...

Detailed explanation of MySQL index principles and optimization

Preface This article was written by a big shot fr...

Add ico mirror code to html (favicon.ico is placed in the root directory)

Code: Copy code The code is as follows: <!DOCTY...

MySQL column to row conversion and year-month grouping example

As shown below: SELECT count(DISTINCT(a.rect_id))...

How to deploy Rancher with Docker (no pitfalls)

Must read before operation: Note: If you want to ...

Summary of ten principles for optimizing basic statements in MySQL

Preface In the application of database, programme...

HTML realizes real-time monitoring function of Hikvision camera

Recently the company has arranged to do some CCFA...

Detailed explanation of Vue router routing guard

Table of contents 1. Global beforeEach 1. Global ...

Explanation of the usage of replace and replace into in MySQL

MySQL replace and replace into are both frequentl...

Web page creation for beginners: Learn to use HTML's hyperlink A tag

The hyperlink a tag represents a link point and i...