In the previous two articles, learned what is MyBatis, a simple implementation of MyBatis program, using MyBatis to achieve CRUD operation, involving some common configurations of MyBatis…..

In this article, I will write about the simple implementation of ResultMap, logging function, paging, these more fragmentary things.

Below is the address of the previous article

(a)

(2)

ResultMap

Throw out problem

When we write an entity class, we usually write it for a database field.

There are some problems if we don’t write by database field.

We can change the attribute names in the entity class so that they do not correspond to the data in the database. Run our previous query SQL again

Rename the attribute PWD to password, which does not correspond to a field in the database

Call the query method and use junit for a quick test

public class MapperTest {
    @Test
    public void getUserByIdTest(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // Query information about user 2 in the database
        System.out.println(mapper.getUserById(2)); sqlSession.close(); }}Copy the code

As you can see, if YOU return password, it’s null.

Why is it null?

Let’s go to mapper.xml to find out

<mapper namespace="com.molu.mapper.UserMapper">
    <select id="getUserById" parameterType="int" resultType="user">
        select * from mybatis.user where id = #{id}
    </select>
</mapper>
Copy the code

Let’s look at the SQL statement in the body of the SELECT tag. The actual execution of this SQL statement should be:

select id,name,pwd from mybatis.user where id = #{id};
Copy the code

We see a problem. The field value returned after executing the SQL query does not correspond to our properties.

The easiest way to resolve this situation, of course, is to alias the SQL statement. That is:

select id,name,pwd as password from mybatis.user where id = #{id};
Copy the code

Now copy this line of SQL into the body of our SELECT tag, and our password query should have a value.

This is the dumbest, most direct, but effective solution

Simple use ResultMap

Admittedly, the problem can be solved quickly by using as to take an alias in the SQL statement. But we will not only write this section of SQL, also cannot write SQL every time with an as......

Instead of using crude AS to solve the problem, we can also use the resltMap tag to solve the problem:

  1. In the Mapper tag, write a resultMap tag
<! -- id can be customized, type is written to our entity class -->
<resultMap id="UserMap" type="User">
    <! -- column corresponds to the column in our database, and property corresponds to the property in our entity class -->
	<result column="pwd" property="password"/>
</resultMap>
Copy the code
  1. Replace the resultType in the SELECT label with a resultMap, and take the ID from the previously written resultMap label
<select id="getUserById" resultMap="UserMap">
	select * from mybatis.user where id = #{id};
</select>
Copy the code
  1. Perform the query operation again

The resultMap element is the most important and powerful element in MyBatis. It frees you from 90% of the JDBC ResultSets data extraction code, and in some cases allows you to do things that ARE not supported by JDBC. In fact, when writing mapping code for complex statements such as joins, a resultMap can replace thousands of lines of code that do the same thing. ResultMap is designed to achieve zero configuration for simple statements, while complex statements only need to describe the relationship between statements.

To learn more about ResltMap, visit our website

The log

Why logs are needed

Logging is not optional in online business, but plays a very important role. Logs are an important part of a system, recording user operations, system running status, and error information. The quality of logging is directly related to the speed of locating system faults. In addition, you can observe and analyze logs to discover possible system risks in advance and prevent online accidents.

Typically, logging serves two purposes:

Diagnostic logging: Logs related to application operations. For example, you can search diagnostic logs for context information about error messages to analyze and troubleshoot online problems. For example, if an internal error occurs on the website, the error information should be recorded in logs for subsequent troubleshooting.

Audit logs: Logs recorded for business analysis. User transaction information can be extracted from audit logs and combined with other user data to form user reports or to optimize business objectives. For example, The Apache access log is a kind of audit log. Through the Apache access log, we can know the PV, UV, hottest resources, error ratio and other important information of the website.

MyBatis provides logging functionality by using a built-in log factory. The built-in logging factory will delegate logging to one of the following implementations:

  • SLF4J
  • Apache Commons Logging
  • Log4j 2
  • Log4j
  • JDK logging
  • .

MyBatis built-in log factory selects log delegate implementation based on runtime detection information. It uses the first implementation found (in the order listed above). When these implementations are not found, logging is disabled.

We can specify the logging implementation via the setting tag

This is the first time that we use the setting tag in the learning process. The setting tag has some points to pay attention to

  • Like the other configurations, it is written in the Configuration TAB
  • It must be written in the second place, under the Properties tag and above the typeAliases tag
<settings>
    <setting name="" value=""/>
</settings>
Copy the code

He needs to pass in a name and a value, and for name we’ll say logImpl value and write the log implementation that we specify

    <settings>
        <! You can use the standard log factory to test the effect.
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
Copy the code

With the specified log factory implementation, our output will have a lot more log information.

Things like opening JDBC, creating connection objects, and displaying pre-compiled SQL……. It prints things that we wouldn’t normally notice.

This is one of the purposes of logging.

LOG4J

What is Log4j?

  • Log4j is an open source project from Apache that allows you to control the destination of log messages to the console, files, GUI components, and even the socket server, NT event logger…….
  • We can also control the output format of each log
  • By defining the level of each log message, we can more carefully control the log generation process.
  • The most interesting thing is that these can be configured flexibly through a configuration file without the need to modify the application code.

The use of log4j

To use log4j, we need to import the LOG4j JAR package

<! Import log4j to pom.xml-->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Copy the code

Log4j, Maven repository

After importing the LOG4j JAR package, we were still unable to use it.

We also need to write a configuration file for it, which can be configured with a lot of parameters

Do not go to rote inside the configuration, the general online to find a more complete, after understanding the deletion to change to their own satisfaction with the storage of good.

There’s not much point in remembering things like this unless you have a specific need.

Write a log4j.properties configuration file in the Resources directory

Output DEBUG logs to the console and file destinations. Console and file are defined below
log4j.rootLogger=DEBUG,console,file
Settings for 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
# File output Settings
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/molu.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.PreparedStatement=DEBUG

Copy the code

After running the test, you can see that the console outputs dense logs

Since we set this up, the log output will generate a molu.log file in the current path

Of course, Log4j is much more than that. My level is also limited, so I don't want to expand the interest can move to other blogs.

paging

In normal development, we use paging a lot.

What is paging?

The following quote

Paging queries are generally divided into two types: logical paging and physical paging.

Logical page is in user access for the first time, the database query all the records of all out, added to a large collection, and then stored in the session object, and then out of the current page by page number calculation need to display the data content, stored in a small list of collection, and store it to the request object, jump to the JSP page, Perform traversal display. When the user visits for the second time, as long as the browser is not closed, data is retrieved from the session for display. This method is called logical paging because it does computational paging in an in-memory session object rather than actually paging our database.

Disadvantages: If a large amount of data needs to be queried, the session consumes a large amount of memory. Since the data is retrieved from the session, a second or more open browser access to the session will directly access the session, thus not ensuring that the data is up to date.

Advantages: Unified code processing, easy to do cross-database migration.

Physical paging, using the database’s own paging mechanism, such as Rownum in Oracle database, or limit in Mysql database]. Physical paging is called because it is a paging condition query for data in a database. Every physical page is connected to the database.

Advantages: Data is kept up to date and does not take up too much memory because small amounts of data are queried based on paging conditions

In MyBatis, we can also use the limit statement to achieve paging operations

Limit

Paging requires a certain amount of data. You are advised to add more data to the database

If you’re confused about pagination, you can check it out here. I’ve written about sorting and pagination in my MySQL Getting Started blog, and you can check it out if you’re interested.

Use limit to achieve paging, the core is still through THE SQL statement to pass this way, relatively simple.

  • Start by writing a corresponding method in the Mapper interface
public interface UserMapper {
    // paging query, pass parameters through map
    List<User> getUserByLimit(Map<String,Integer> map);
}
Copy the code
  • Synchronized to the mapper. The XML
<resultMap id="UserMap" type="User">
    <result column="pwd" property="password"/>
</resultMap>

<select id="getUserByLimit" resultMap="UserMap" parameterType="map">
    select * from user limit #{startIndex},#{pageSize}
</select>
<! Limit paging by receiving parameters from map -->
Copy the code
  • Writing test classes
    @Test
    public void getUserByLimit(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // new 一个HasMap
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        // Put values to map
        map.put("startIndex".0);
        map.put("pageSize".3);
        // Call the method with a value map, passing parameters to limit
        List<User> userByLimit = mapper.getUserByLimit(map);
        / / traverse
        for(User user : userByLimit) { System.out.println(user); }}Copy the code
  • Query result:

Successful paging operation with limit

RowBounds

In addition to using limit for paging with SQL statements at the core, there is a Java class called RowBounds that implements paging in pure Java code, but this class is actually implemented by limit

Because this kind of way is not commonly used, so I do not want to repeat, the annotation is simple to write a good

  • Write a method in the Mapper interface
public interface UserMapper {
    // Simple query method
    List<User> getUserByRowBounds(a);
}
Copy the code
  • mapper.xml
    <resultMap id="UserMap" type="User">
        <result column="pwd" property="password"/>
    </resultMap>

    <select id="getUserByRowBounds" resultMap="UserMap">
        select * from user
    </select>
Copy the code
  • The test class
    @Test
    public void getUserByRowBoundsTest(a){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // New a RowBounds, passing the starting value and page size, as above
        RowBounds rowBounds = new RowBounds(0.3);
        // Use the old sqlsession.selectList (); methods
        // This method takes three arguments: the first argument is the fully qualified name of the interface method written to death, the second argument is null, and a rowBounds is passed
        List<User> userList = sqlSession.selectList("com.molu.mapper.UserMapper.getUserByRowBounds".null, rowBounds);
        for(User user : userList) { System.out.println(user); }}Copy the code
  • Test results: Omitted

This approach also allows for paging, but we’re not going to use it.


The original address

Painters home page