SpringBoot project connects to the database

SpringBoot project connects to the database

After successfully building the springboot project, we need to connect the database. First, we need to go to the dependency URL of maven to find the dependencies jdbc and MySQL Connector we need to connect to the database.

http://www.mvnrepository.com/

image-20221110082929599

image-20221109162922494

image-20221109163014302

image-20221109163401211

Copy and paste the corresponding dependencies into the pom.xml configuration file of the project

image-20221110083322916

If there is a red flag, just refresh the dependency. Note that the choice of dependency should be selected according to your current database version and jdk version. Although the dependency of the high version will be compatible with the database of the low version, the dependency of adaptation is not easy to appear in the follow-up question

Next, we need to connect to our database in the idea, select the data source, and click MySQL

image-20221110083630824

The name here is the connection name, which can be customized without any requirements. The host is connected according to the IP address of the database. If it is a local database, just fill in localhost or 127.0.0.1. Here I use the cloud database of Alibaba Cloud. Fill in It is the external network connection of the cloud database. The user and password need to fill in the user account with the permission to connect to the database. Idea will automatically generate the URL of the database. Remember the URL here, which will also be used in project configuration. Note that you must click Test Connection. When the test connection is successful, click OK to connect to the database

image-20221110083748450

This is what the IDE looks like after the database is connected. We can visually see the situation of the database through the IDE.

image-20221110084312993

Next, we configure the database for the project, open the application.yml configuration file, and write the following statement

# 数据库配置
spring:
  datasource:
    username: root
    password: 
    url: jdbc:mysql://rm-2ze4l9h346r9853u8jo.mysql.rds.aliyuncs.com :3306/fmms?useUnicode=true&characterEncoding=utf-8&SSL=true&serverTimezone=Asia/Shanghai
    driverClassName: com.mysql.cj.jdbc.Driver

The user name and password here can be filled in with the user name and password of the database, and the URL can be filled in with the URL generated automatically during the connection just now, or you can replace the corresponding part according to the format (in the URL? The latter part is some I added Character encoding and time limit, can not be written), driverClassName should write the driver of the machine to connect to the database, this may be different for each person, you can modify it according to your own situation

image-20221110084818310

At this point, our database is connected, we can write a test to test whether the connection is successful.

Let's query all the contents in the users table in the database, and then run the program

    @Resource
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/list")
    public List<Map<String, Object>> userList() {
    
    
        String sql = "select * from users";
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

image-20221110084929788

After the program runs without problems, go to the browser to access the corresponding interface

image-20221110085146108

image-20221110085206172

You can see that he has returned the contents of the users table in our database, so far the database connection is successful

Guess you like

Origin blog.csdn.net/qq_49288154/article/details/127782047