Introduction
In this project, you will learn how to use properties to modify the database configuration in a MyBatis-based application. MyBatis is a popular Java persistence framework that simplifies the interaction between Java applications and databases.
👀 Preview
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.labex.test.MyBatisTest
Data deleted successfully
Data inserted successfully
[course number:1 course name:Data Structures teacher:John Smith, course number:2 course name:Java teacher:Mary Johnson, course number:3 course name:Python teacher:David Brown, course number:4 course name:C++ teacher:Jennifer Davis, course number:6 course name:C teacher:Michael Wilson, course number:8 course name:Artificial Intelligence teacher:Emily Thompson]
Data updated successfully
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.1 sec
🎯 Tasks
In this project, you will learn:
- How to move the database configuration from the
mybatis-config.xmlfile to thedb.propertiesfile. - How to modify the
mybatis-config.xmlfile to use the properties defined in thedb.propertiesfile to configure the database connection. - How to verify the application's functionality by running the provided test cases.
🏆 Achievements
After completing this project, you will be able to:
- Separate the database configuration from the MyBatis configuration file.
- Use properties to configure the database connection in a MyBatis-based application.
- Import and use property values in the MyBatis configuration file.
- Ensure the application's functionality by running test cases.
Move the Database Configuration to the db.properties File
In this step, you will learn how to move the database configuration from the mybatis-config.xml file to the db.properties file.
- Open the
db.propertiesfile located in theMyBatisPropertiesProject/src/main/resourcesdirectory. - Add the following properties to the
db.propertiesfile:
## Database Configuration
## Database driver class name
driver=com.mysql.cj.jdbc.Driver
## Database connection URL
url=jdbc:mysql://localhost:3306/MyBatisDemo?characterEncoding=utf-8
## Database username
username=root
## Database password
password=
## Mapper Configuration
## Package containing MyBatis mapper interfaces
mapper.package=org.labex.mapper
These properties define the database driver, connection URL, username, and password, as well as the package containing the MyBatis mapper interfaces.
Modify the mybatis-config.xml File to Use the Properties
In this step, you will learn how to modify the mybatis-config.xml file to use the properties defined in the db.properties file.
- Open the
mybatis-config.xmlfile located in theMyBatisPropertiesProject/src/main/resourcesdirectory. - Add the following line at the beginning of the file to import the properties from the
db.propertiesfile:
<properties resource="db.properties"/>
- Update the database connection configuration in the
<dataSource>element to use the imported properties:
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
- Update the
<mappers>element to use themapper.packageproperty:
<mappers>
<package name="${mapper.package}"/>
</mappers>
The complete mybatis-config.xml file should now look like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Import properties from db.properties file -->
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- Configure database connection using imported properties -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- Register SQL mapping files -->
<mappers>
<package name="${mapper.package}"/>
</mappers>
</configuration>
Now, the database configuration is stored in the db.properties file, and the mybatis-config.xml file uses the properties defined in db.properties to configure the database connection.
Run the Test Case
To run the test case, follow these steps:
- In your terminal, navigate to the
MyBatisPropertiesProjectproject directory using the following command:
cd ~/project/MyBatisPropertiesProject/
- Run the following command to execute the test case:
mvn test
The output should be similar to the following:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.labex.test.MyBatisTest
Data deleted successfully
Data inserted successfully
[course number:1 course name:Data Structures teacher:John Smith, course number:2 course name:Java teacher:Mary Johnson, course number:3 course name:Python teacher:David Brown, course number:4 course name:C++ teacher:Jennifer Davis, course number:6 course name:C teacher:Michael Wilson, course number:8 course name:Artificial Intelligence teacher:Emily Thompson]
Data updated successfully
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.1 sec
This shows that the test class uses MyBatis to perform CRUD operations on a database table named Course, including select, insert, update, and delete operations.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



