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:
result:
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} </select>
- 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"/> </resultMap>
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
- JDK_LOGGING
- COMMONS_LOGGING
- STDOUT_LOGGING [mastering]
- NO_LOGGING
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!
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
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
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
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 log4j.rootLogger=DEBUG,console,file #Settings related to console output 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 #Settings related to file output log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/hou.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 #Log output level 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
3. Configure log4j as the log implementation mybatis config xml
<settings> <setting name="logImpl" value="LOG4J"/> </settings>
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
@Test public void testLog4j(){ logger.info("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