1. Think about

In the last article JavaWeb advanced road: MyBatis CURD (add, delete and change check), we use MyBatis for the database to do the operation of add, delete and change check, I believe you have a certain understanding of MyBatis add, delete and change check. However, in our real business requirements, the field name of the database is inconsistent with the attribute name of the entity class in our project. In this case, the queried data will be NUll. Why is that? How to solve this problem?

There are two ways to solve this problem. One is to use the alias in the SQL statement to solve the inconsistency between the field name and the attribute name. Another is a ResultMap, which we will introduce next.

2. Introduction

The resultMap element is the most important and powerful element in MyBatis. It frees you from the 90% JDBC ResultSets extraction code, and in some cases allows you to perform operations that JDBC does not support. 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. The design idea of The ResultMap is to do zero configuration for simple statements. For complex statements, you only need to describe the relationship between statements.

Simple resultMap usage, for example:

<! -- mybatis - config. XML - >
<configuration>.<typeAliases>
        <typeAlias type="com.lmx.demo3.pojo.User" alias="User"/>
    </typeAliases>.</configuration>

<!-- UserMapper.xml 中 -->
<mapper namespace="com.lmx.demo3.mapper.UserMapper">
    
    <! -- id global unique id, type return type -->
    <resultMap id="UserMap" type="User">
        <! - the primary key - >
        <id column="id" property="id"/>
        <! -- column name of database table, property name of entity class -->
        <result column="name" property="account"/>
        <result column="password" property="password"/>
    </resultMap>
    
    <select id="selectUserById" parameterType="int" resultMap="UserMap">
        select * from user where id = #{id};
    </select>
    
</mapper>
Copy the code

Practice 3.

3.1 the problem

The field name of the database is inconsistent with the attribute name of the Java entity class. As a result, the attribute value in the query result is NULL.

3.2 Setting up the Environment

Database:

Java entity classes:

package com.lmx.demo3.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private long id;
	// Corresponds to the name field in the database
    private String account;
    private String password;

}
Copy the code

UserMapper Interface class:

package com.lmx.demo3.mapper;

import com.lmx.demo3.pojo.User;

public interface UserMapper {

    User selectUserById(int id);
    
}

Copy the code

Usermapper. XML SQL mapping file:


      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lmx.demo3.mapper.UserMapper">

    <select id="selectUserById" parameterType="int" resultType="User">
        select * from user where id = #{id};
    </select>
    
</mapper>
Copy the code

The test class:

package com.lmx.test.mapper;

import com.lmx.demo3.mapper.UserMapper;
import com.lmx.demo3.pojo.User;
import com.lmx.demo3.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

public class UserMapperTest {

    @Test
    public void selectUserById(a) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1); System.out.println(user); sqlSession.close(); }}Copy the code

Output results:

Analysis:

  • Select * from user where id = #{id

    select id,name,password from user where id = #{id}

  • Mybatis will look for the value of the set method of the corresponding column name in the corresponding entity class according to the column name (will be converted to lowercase, the database is case insensitive), and return null because setName() method is not found. [Automatic mapping]

3.3 Troubleshooting

We can solve this problem with a powerful resultMap:

Simply modify the userMapper. XML file:


      
<! DOCTYPEmapper
        PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lmx.demo3.mapper.UserMapper">
    <! -- The first way -->
    <! -- id global unique id, type return type -->
    <resultMap id="UserMap" type="User">
        <! - the primary key - >
        <id column="id" property="id"/>
        <! -- column name of database table, property name of entity class -->
        <result column="name" property="account"/>
        <result column="password" property="password"/>
    </resultMap>
    
    <! -- write the column name and attribute name that do not match -->
<! --<resultMap id="UserMap" type="User">-->
<! -- <result column="name" property="account"/>-->
<! --</resultMap>-->
    
    <select id="selectUserById" parameterType="int" resultMap="UserMap">
        select * from user where id = #{id};
    </select>
</mapper>
Copy the code

Running result:

4. At the end

If only the world were always this simple. But certainly not, in the database, there is a one-to-many, many-to-one situation, then we now learned the resultMap is obviously not enough. So what? Don’t worry, Mybatis provides advanced result mapping, if you are interested, you can go to the official documentation: Result Mapping, AND I will write a special article about it later.