Skip to content

In some case, a unmapped column name can not automapping to a similar property in the bean object.  #1562

@lebin

Description

@lebin

MyBatis version

3.4.3~3.5.1

Database vendor and version

Any

Test case or example project

See test case below or example project(https://github.com/lebin/mybatis-test-cases):

Steps to reproduce

1、Create a demo table and insert some data.
create table users (
id int,
name varchar(20),
full_name varchar(20),
phone varchar(20),
phone_number bigint
);
insert into users (id, name, full_name, phone, phone_number) values(1, 'User1', 'Shan Zhang', '+86 12345678901', 12345678901);
insert into users (id, name, full_name, phone, phone_number) values(2, 'User2', 'Shi Li', '+86 12345678902', 12345678902);

2、Create a demo java bean as below:
public class DemoUser{
private Integer id;
private String name;
private String fullName;
private Long phone;
...
}

3、Create a mapper xml with below info:
<select id="getUserWithFullName" resultMap="resultWithFullName">
select id, name, full_name as fullname, phone_number from users where id = #{id}
</select>
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="resultWithFullName" autoMapping="true">
<result property="phone" column="phone_number">
<result property="fullName" column="full_name">
</resultMap>

4、Create a junit test case.
@test
void shouldGetAUserWhithFullName() {
sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
User user = mapper.getUserWithFullName(1);
Assertions.assertEquals("User1", user.getName());
Assertions.assertNotNull(user.getFullName());
}
}

5、Excute Method "shouldGetAUserWhithFullName()".

Expected result

The value of property "fullName" will auto mapper to property "fullName".

Actual result

The value of property "fullName" is null.

Cause of this bug

There is a unreasonable judge condition in org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createAutomaticMappings().

Catch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions