Introduction
In this project, you will learn how to configure logging and use batch aliasing for entity classes in a MyBatis-based project.
👀 Preview

🎯 Tasks
In this project, you will learn:
- How to configure the logging component using the log4j library
- How to add logging dependencies to the project
- How to use the logging component in your code to log messages
- How to implement batch aliasing for entity classes in the MyBatis configuration file
🏆 Achievements
After completing this project, you will be able to:
- Set up and configure the logging component in a Java project
- Use the logging component to log messages at different levels (e.g., DEBUG, INFO, ERROR)
- Apply batch aliasing to simplify the usage of entity classes in your MyBatis code
Configure Logging
In this step, you will learn how to configure the logging component in your project.
Create a
log4j.propertiesconfiguration file in thesrc/main/resourcesdirectory of your project.Add the following configuration to the
log4j.propertiesfile:
## Set root logger level to DEBUG and its only appender to stdout.
log4j.rootLogger=DEBUG, stdout
## Define the pattern for the logger
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t] - %m%n
This configuration sets the root logger level to DEBUG and directs the logs to the console (stdout) with a specific pattern.
Add Logging Dependencies
In this step, you will add the necessary dependencies for the logging component to your project.
Open the
pom.xmlfile in theMyBatisCourseDemo02project.Add the following dependency to the
<dependencies>section:
<!-- Log4j dependency -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version> <!-- Adjust version as needed -->
</dependency>
This dependency will provide the necessary classes and methods for the logging component.
Use Logging in Your Code
In this step, you will create a Logger instance and use it to log messages in your code.
Open the
MyBatisTest.javafile in the/src/test/java/org/lanqiao/test/directory.Add the following code at the top of the file:
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
- Create a private static Logger instance:
private static final Logger logger = Logger.getLogger(MyBatisTest.class);
- Load the log4j configuration file in the
before()method:
static {
PropertyConfigurator.configure("/home/labex/project/MyBatisCourseDemo02/src/main/resources/log4j.properties");
}
- Use the logger instance to log messages in your test methods:
@Test
public void testSel() throws IOException{
logger.debug("Executing query operation...");
CourseMapper cMapper = session.getMapper(CourseMapper.class);
List<Course> courses = cMapper.queryAllCourse();
System.out.println(courses);
session.close();
}
Now, when you run the tests, the log messages will be displayed in the console according to the configured log4j properties.
Use Batch Aliasing for Entity Classes
In this step, you will use batch aliasing to give aliases to the entity classes in your project.
Open the
mybatis-config.xmlfile in the/src/main/resources/directory.Add the following
<typeAliases>section:
<typeAliases>
<typeAlias type="org.lanqiao.pojo.Course" alias="Course" />
</typeAliases>
This will create an alias "Course" for the org.lanqiao.pojo.Course class, allowing you to use the alias in your mapper files and other parts of the code.
Now, you can use the alias "Course" instead of the full class name org.lanqiao.pojo.Course in your mapper files and other parts of the code.
Run
Use the following commands to compile and run it in a terminal and check the test files with Maven:
cd MyBatisCourseDemo02
mvn test
Use the command to run the test in the terminal and you can refer to the following results:

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



