Use Properties to Modify Database Configuration

JavaJavaBeginner
Practice Now

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.xml file to the db.properties file.
  • How to modify the mybatis-config.xml file to use the properties defined in the db.properties file 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.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ConcurrentandNetworkProgrammingGroup(["`Concurrent and Network Programming`"]) java/ConcurrentandNetworkProgrammingGroup -.-> java/working("`Working`") subgraph Lab Skills java/working -.-> lab-300406{{"`Use Properties to Modify Database Configuration`"}} end

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.

  1. Open the db.properties file located in the MyBatisPropertiesProject/src/main/resources directory.
  2. Add the following properties to the db.properties file:
## 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.

✨ Check Solution and Practice

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.

  1. Open the mybatis-config.xml file located in the MyBatisPropertiesProject/src/main/resources directory.
  2. Add the following line at the beginning of the file to import the properties from the db.properties file:
<properties resource="db.properties"/>
  1. 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>
  1. Update the <mappers> element to use the mapper.package property:
<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.

✨ Check Solution and Practice

Run the Test Case

To run the test case, follow these steps:

  1. In your terminal, navigate to the MyBatisPropertiesProject project directory using the following command:
cd ~/project/MyBatisPropertiesProject/
  1. 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.

✨ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like