Mybatis-Solve the problem of inconsistent fields, lead to the result set mapping ResultMap

Database field name
Insert picture description here
entity class attribute name
Insert picture description here

Query result: [id=1, password=null, name=Milan] It
can be seen that when the returned entity class is inconsistent with the database field, the corresponding assignment reason will not be found
:

<select id="getUsers" resultType="COM.MLJ.MYBATIS_STUDY.User">
    select * from user1;
    //select id,name,pwd from user1;
    //查出来的pwd与password不对应
  </select>

Solution:
1. Make an alias

    select id,name,pwd as password from user1;

2.resultMap

  <resultMap type="User" id="UserMap">
  	<result column="id" property="id"/>
  	<result column="name" property="name"/>
  	<result column="pwd" property="password"/>
  </resultMap>
  
  
  <select id="getUsers" resultMap="UserMap">
    select * from user1;
  </select>

**

2. resultMap

The resultMap element is the most important and powerful element in MyBatis.
The design of ResultMap is that simple statements do not require explicit result mapping, and many complex statements do need to describe their relationship.

You have seen examples of simple mapping statements, but there is no clear resultMap. such as:

<select id="selectUsers" resultType="map">
  select id, username, hashedPassword
  from some_table
  where id = #{
    
    id}
</select>

Such a statement simply acts on the keys of all columns that are automatically mapped to the HashMap, which is specified by the resultType attribute. This is useful in many situations, but HashMap cannot describe a domain model well. That way your application will use JavaBeans or POJOs (Plain Old Java Objects) as the domain model. MyBatis supports both.
Take a look at this JavaBean:

package com.someapp.model;
public class User {
    
    
  private int id;
  private String username;
  private String hashedPassword;

  public int getId() {
    
    
    return id;
  }
  public void setId(int id) {
    
    
    this.id = id;
  }
  public String getUsername() {
    
    
    return username;
  }
  public void setUsername(String username) {
    
    
    this.username = username;
  }
  public String getHashedPassword() {
    
    
    return hashedPassword;
  }
  public void setHashedPassword(String hashedPassword) {
    
    
    this.hashedPassword = hashedPassword;
  }
}

Based on the JavaBean specification, the above class has 3 attributes: id, username and hashedPassword. These will exactly match the column names in the select statement.

Such a JavaBean can be mapped to the result set, as simple as mapping to HashMap.

<select id="selectUsers" resultType="com.someapp.model.User">
  select id, username, hashedPassword
  from some_table
  where id = #{
    
    id}
</select>

Remember that type aliases are your partner. You can use them without entering the full path of the class. such as:

<!-- In mybatis-config.xml file -->
<typeAlias type="com.someapp.model.User" alias="User"/>

<!-- In SQL Mapping XML file -->
<select id="selectUsers" resultType="User">
  select id, username, hashedPassword
  from some_table
  where id = #{
    
    id}
</select>

In these cases, MyBatis will automatically create a ResultMap behind the scenes, and map it to the properties of the JavaBean based on the property name. If the column name does not exactly match, you can use the alias of the select clause (a basic SQL feature) on the column name to match the label. such as:

<select id="selectUsers" resultType="User">
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password     as "hashedPassword"
  from some_table
  where id = #{
    
    id}
</select>

You already know a lot about the best part of ResultMap, but you haven't really seen one yet. These simple examples don't need more than what you see. Just for the sake of example, let's take a look at what the external resultMap looks like in the last example. This is another way to solve the column name mismatch.

<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="username"/>
  <result property="password" column="password"/>
</resultMap> 

The statement that refers to it uses the resultMap attribute (note that we have removed the resultType attribute). such as:

<select id="selectUsers" resultMap="userResultMap">
  select user_id, user_name, hashed_password
  from some_table
  where id = #{
    
    id}
</select>

Guess you like

Origin blog.csdn.net/mmmmmlj/article/details/108727385