RestultMap solves the problem that the query is null due to the inconsistency of the class attribute name and the table field name

RestultMap

Query is null problem

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

Environment: Create a new project and copy the previous project

1. View the field name of the previous database

image

2. Entity class design in Java

public class User {
    
    

   private int id;  //id
   private String name;   //姓名
   private String password;   //密码和数据库不一样!
   
   //构造
   //set/get
   //toString()
}

3. Interface

//根据id查询用户
User selectUserById(int id);

4. Mapper mapping file

<select id="selectUserById" resultType="user">
  select * from user where id = #{id}
</select>

5. Test

@Test
public void testSelectUserById() {
    
    
   SqlSession session = MybatisUtils.getSession();  //获取SqlSession连接
   UserMapper mapper = session.getMapper(UserMapper.class);
   User user = mapper.selectUserById(1);
   System.out.println(user);
   session.close();
}

result:

User{
    
    id=1, name='狂神', password='null'}

The query shows that the password is empty. It means there is a problem!

analysis:

select * from user where id = #{id} 可以看做

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

mybatis会根据这些查询的列名(会将列名转化为小写,数据库不区分大小写) ,
 去对应的实体类中查找相应列名的set方法设值 , 由于找不到setPwd() ,
  所以password返回null ; 【自动映射】

solution

Solution 1: Specify an alias for the column name, the alias is the same as the attribute name of the java entity class.

<select id="selectUserById" resultType="User">
  select id , name , pwd as password from user where id = #{id}
</select>

Option 2: Use result set mapping -> ResultMap [recommended]

<resultMap id="UserMap" type="User">
   <!-- id为主键 -->
   <id column="id" property="id"/>
   <!-- column是数据库表的列名 , property是对应实体类的属性名 -->
   <result column="name" property="name"/>
   <result column="pwd" property="password"/>
</resultMap>

<select id="selectUserById" resultMap="UserMap">
  select id , name , pwd from user where id = #{id}
</select>

ResultMap

Automatic mapping

The resultMap element is the most important and powerful element in MyBatis. It can free you from 90% of the JDBC ResultSets data extraction code.

In fact, when writing mapping code for some complex statements such as connection, a resultMap can replace thousands of lines of code that achieves the same function.

The design idea of ​​ResultMap is that for simple statements, there is no need to configure explicit result mapping, and for more complex statements, only their relationship needs to be described.

You have seen examples of simple mapping statements, but did not specify resultMap explicitly. such as:

<select id="selectUserById" resultType="map">
select id , name , pwd
  from user
  where id = #{id}
</select>

The above statement simply maps all the columns to the keys of the HashMap, which is specified by the resultType attribute. Although sufficient in most cases, HashMap is not a good model. Your program is more likely to use JavaBean or POJO (Plain Old Java Objects) as a model.

The best thing about ResultMap is that although you already know it quite well, you don't need to use them explicitly.

Manual mapping

1. The return value type is resultMap

<select id="selectUserById" resultMap="UserMap">
  select id , name , pwd from user where id = #{id}
</select>

2. Write resultMap to realize manual mapping!

<resultMap id="UserMap" type="User">
   <!-- id为主键 -->
   <id column="id" property="id"/>
   <!-- column是数据库表的列名 , property是对应实体类的属性名 -->
   <result column="name" property="name"/>
   <result column="pwd" property="password"/>
</resultMap>

Guess you like

Origin blog.csdn.net/david2000999/article/details/115164433