Mybatis learning notes: resultMap result mapping set and log

Posted by keyoung on Thu, 06 Jan 2022 00:24:12 +0100

Learning content: resultMap result mapping set and log

1. resultMap result mapping set

Problem: find users by id
When the column in the database is inconsistent with the attribute name in the entity class
Columns in database:

Attribute name in entity class:


Because sql statements
select * from mybatis.user where id=#{id} is actually
select id,name,pwd from mybatis.user where id = #{id}
However, you can find it according to pwd, but the entity class is passwrod. If it cannot be found, it will be empty.

terms of settlement:

  • Alias
<select id="getUserById" resultType="com.kuang.pojo.User">
    select id,name,pwd as password from mybatis.user where id = #{id}
  • resultMap result mapping set
<!--Result mapping set-->
    <resultMap id="UserMap" type="User">
    <!--column Fields in the database, property Properties in entity classes-->
 	<!--Use only inconsistent settings-->
        <result column="pwd" property="password"/>

The design idea of ResultMap is that there is no need to configure explicit result mapping for simple statements, but only need to describe their relationship for more complex statements.

2. Log

Log factory
If an exception occurs in a database operation, we need to troubleshoot it. Log is the best assistant!
Once: South, debug
Now: log factory!

  • SLF4J
  • LOG4J [Master]
  • LOG4J2
  • STDOUT_LOGGING [mastering]

Which log implementation is used in Mybatis? Set it in settings!
STDOUT_LOGGING standard log output
In the mybatis core configuration file, configure our logs!

    <setting name="logImpl" value="STDOUT_LOGGING"/>

3. Log4j

What is Log4j?

  • Log4j is an open source project of Apache. By using log4j, we can control that the destination of log information transmission is console, file and GUI components
  • We can also control the output format of each log;
  • By defining the level of each log information, we can control the log generation process in more detail.
  • It can be configured flexibly through a configuration file without modifying the application code.

1. Import the package of log4j first


2. Write log4j Properties file

#Output the log information with the level of DEBUG to the two destinations of console and file. The definitions of console and file are in the following code

#Settings related to console output
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout

#Settings related to file output
log4j.appender.file = org.apache.log4j.RollingFileAppender

#Log output level

3. Configure log4j as the log implementation mybatis config xml

     <setting name="logImpl" value="LOG4J"/>

Easy to use:
1. In the class to use Log4j, import the package importorg apache. Log4j. Logger;
2. Log object. The parameter is the class of the current class

static Logger logger = Logger.getLogger(UserDaoTest.class);

3. Run test program

    public void testLog4j(){"info:Entered testLog4j");
       logger.debug("debug:Entered testLog4j");
       logger.error("error:Entered testLog4j");

Because log4j appender. file. File=./ log/hou.log, so an hou Log file, and the output results will be in this file

Topics: Java SSM intellij-idea