Java springboot integrates Druid data source configuration

The last part of the integration is to integrate a data source
Druid.
We still open the idea to create a project,
insert image description here
adjust the path and version, and choose a good directory. The version is preferably java 8 JDK 1.8 and Next, the next step
insert image description here
here is the version of spring boot. Remember Choose not to make it too high. 2. Just a few.
insert image description here
Druid is obviously not found here, so we have to add it manually later. Here we need to introduce two
startups that depend on MyBatis and MySql.
insert image description here
Then click Finish to confirm the creation of the project.

Then our project came out
insert image description here
. The first problem we need to solve when creating a good project must be Druid. Today’s protagonist has not been introduced yet.
Let’s visit the top of
https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient/3.1
Search for Druid
insert image description here
and press Enter and it will be listed.
insert image description here
Let’s click on the version of Spring Boot Starter.
insert image description here
If the version is 1.2.6, click on it.
insert image description here
After entering, pull down and we can see one of his coordinates.
insert image description here
Copy this to the one we just created Under the dependencies tag of the pom.xml of the project

So if you use 2019 like me, then copy it up and report an error and can’t find it.
insert image description here
Let’s right click and operate Maven
insert image description here
and wait for the download to be completed here
insert image description here
, and then no error will be reported.
insert image description here
Then we still change application.properties to application.yml.
The reference code is as follows

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root

Here we set the url to access the MySql database of this machine
to the test database under the local ip 3306.
You need to take a look here because there is a test database under my MySql. You still have to write according to the situation
insert image description here
and then the user name and password because I have not touched it. So it's root

Then here we need to configure the data source.
The first method,
you add a type: Druid at the back, and the prompt will come out naturally. Choose the first one.
insert image description here
This is the first method. This method can only be said to be usable but not integrated. Matching method

The second method is
to enter druid below, and a prompt will appear.
insert image description here
What is the user name and password in the prompt? In fact, just do this directly.

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test
      username: root
      password: root

This is the dedicated configuration after druid integration

Then we still set up the environment to
check the staff database table I want to operate today,
insert image description here
find the directory where the startup class is located, create a folder called domain in the same directory,
and create a java attribute class staff with the same name as the database.
The reference code is as follows

package com.example.druid.domain;

public class staff {
    
    
    private int id;
    private String name;
    private int age;
    private int status;
    private int departmentid;

    @Override
    public String toString(){
    
    
        return "staff{"+
                "id"+id+
                "namne"+name+
                "age"+age+
                "status"+status+
                "departmentid"+departmentid+
                "}";
    }

    public int getId() {
    
    
        return id;
    }
    public void setId(int id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }

    public int getStatus() {
    
    
        return status;
    }
    public void setStatus(int status) {
    
    
        this.status = status;
    }

    public int getDepartmentid() {
    
    
        return departmentid;
    }
    public void setDepartmentid(int departmentid) {
    
    
        this.departmentid = departmentid;
    }
}

We declare the corresponding attributes according to the database table fields , and then create an interface staffDao under
the startup similar directory creation package dao. The reference code is as follows

package com.example.druid.dao;

import com.example.druid.domain.staff;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface staffDao {
    
    
    @Select("select * from staff where id = #{id}")
    staff getById(Integer id);
}

Here we are still a normal MyBatis operation
and then we write in the test class

package com.example.druid;

import com.example.druid.dao.staffDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DruidApplicationTests {
    
    

    @Autowired
    private staffDao staffDao;

    @Test
    void contextLoads() {
    
    
        System.out.println(staffDao.getById(1));
    }
}

Then we call the getById we just wrote in the test class to query the data with id 1
insert image description here

Then run the code
insert image description here
and the result
insert image description here
is as follows
insert image description here

And when we check the log output content, we
insert image description here
also left traces of druid

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131228956