MyBatis (3)-ResultMap and LOG

1.1 ResultMap

The problem to be solved: the attribute name and the field name are inconsistent

The structure of the database
Insert picture description here
Insert picture description here

public class User {
    
    
    private int userId;
    private String userName;
    private String passWord;

The way to solve the problem

<!--namespace 绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.xxxx.dao.UserMapper">
<!--    结果集映射-->
    <resultMap id="UserMap" type="User">
<!--        column数据库中的字段 property实体类中的属性-->
        <result column="id" property="userId"/>
        <result column="name" property="userName"/>
        <result column="pwd" property="passWord"/>
    </resultMap>
<!--    查询-->
   <select id="getUserById" resultMap="UserMap">
        select id,name,pwd  from user where id = #{
    
    id}
   </select>
</mapper>

Insert picture description here

1.2 Log

1.2.1 Log Factory

If there is a problem with a database-related operation, we can use the log to troubleshoot the error.
Insert picture description here
The built-in log factory of Mybatis provides the log function. The specific log implementation has the following tools:
SLF4J
LOG4J
LOG4J2
JDK_LOGGING
COMMONS_LOGGING
STDOUT_LOGGING
NO_LOGGING The
specific log implementation tool selected is determined by the built-in log factory of MyBatis.

STDOUT_LOGGING

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

Insert picture description here
LOG4J
1 Log4j is an open source project of Apache.
2 By using Log4j, we can control the destination of log information delivery: console, text, GUI components...
3 We can also control the output format of each log;
4 By defining each log At the level of information, we can control the log generation process in more detail. The most interesting thing is that these can be flexibly configured through a configuration file without modifying the application code.
Insert picture description here

1. Add the jar package of Log4J

pom.xml

<dependencies>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

2 Configure the Log4J
classpath to create a file named log4j.properties

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file

#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n

#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/xxxx.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{
    
    yy-MM-dd}][%c]%m%n

#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

setting log implementation

    <settings>
<!--        标椎日志输出-->
        <setting name="logImpl" value="LOG4J"/>
    </settings>

Test and see the console output!
Insert picture description here

Use Log4j for output in the program!

//注意导包:org.apache.log4j.Logger
static Logger logger = Logger.getLogger(UserDaoTest.class);
  @Test
    public void testLog4j() {
    
    
        logger.info("info:进入了testLog4J方法");
        logger.debug("debug:进入了testLog4J方法");
        logger.error("error:进入了testLog4J方法");
    }

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/110819268