The last part of the integration is to integrate a data source
Druid.
We still open the idea to create a project,
adjust the path and version, and choose a good directory. The version is preferably java 8 JDK 1.8 and Next, the next step
here is the version of spring boot. Remember Choose not to make it too high. 2. Just a few.
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.
Then click Finish to confirm the creation of the project.
Then our project came out
. 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
and press Enter and it will be listed.
Let’s click on the version of Spring Boot Starter.
If the version is 1.2.6, click on it.
After entering, pull down and we can see one of his coordinates.
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.
Let’s right click and operate Maven
and wait for the download to be completed here
, and then no error will be reported.
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
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.
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.
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,
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
Then run the code
and the result
is as follows
And when we check the log output content, we
also left traces of druid