Skip to content

Commit 978b922

Browse files
杜龙harawata
杜龙
authored andcommitted
unit test for avoiding unexpected automapping issue
1 parent fc73779 commit 978b922

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

src/test/java/org/apache/ibatis/submitted/automapping/AutomappingTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ public void shouldGetAUser() {
6464
}
6565
}
6666

67+
@Test
68+
public void shouldGetAUserWhithPhoneNumber() {
69+
sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
70+
SqlSession sqlSession = sqlSessionFactory.openSession();
71+
try {
72+
Mapper mapper = sqlSession.getMapper(Mapper.class);
73+
User user = mapper.getUserWithPhoneNumber(1);
74+
Assert.assertEquals("User1", user.getName());
75+
Assert.assertEquals(new Long(12345678901L), user.getPhone());
76+
} finally {
77+
sqlSession.close();
78+
}
79+
}
80+
6781
@Test
6882
public void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
6983
sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);

src/test/java/org/apache/ibatis/submitted/automapping/CreateDB.sql

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ drop table books if exists;
2020

2121
create table users (
2222
id int,
23-
name varchar(20)
23+
name varchar(20),
24+
phone varchar(20),
25+
phone_number bigint
2426
);
2527

2628
create table books (
@@ -40,8 +42,9 @@ create table breeder (
4042
name varchar(20)
4143
);
4244

43-
insert into users (id, name) values(1, 'User1');
44-
insert into users (id, name) values(2, 'User2');
45+
-- '+86 12345678901' can't be converted to a number
46+
insert into users (id, name, phone, phone_number) values(1, 'User1', '+86 12345678901', 12345678901);
47+
insert into users (id, name, phone, phone_number) values(2, 'User2', '+86 12345678902', 12345678902);
4548

4649
insert into books (version, name) values(99, 'Learn Java');
4750

src/test/java/org/apache/ibatis/submitted/automapping/Mapper.java

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public interface Mapper {
2121

2222
User getUser(Integer id);
2323

24+
User getUserWithPhoneNumber(Integer id);
25+
2426
User getUserWithPets_Inline(Integer id);
2527

2628
User getUserWithPets_External(Integer id);

src/test/java/org/apache/ibatis/submitted/automapping/Mapper.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@
2323
<mapper namespace="org.apache.ibatis.submitted.automapping.Mapper">
2424

2525
<select id="getUser" resultMap="result">
26-
select * from users where id = #{id}
26+
select id, name from users where id = #{id}
2727
</select>
2828

2929
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="result" autoMapping="true">
3030
</resultMap>
3131

32+
<select id="getUserWithPhoneNumber" resultMap="resultWithPhoneNumber">
33+
select * from users where id = #{id}
34+
</select>
35+
36+
<resultMap type="org.apache.ibatis.submitted.automapping.User" id="resultWithPhoneNumber" autoMapping="true">
37+
<result property="phone" column="phone_number"/>
38+
</resultMap>
39+
3240
<sql id="selectUserPetBreeder">
3341
select users.id, users.name,
3442
pets.id as petId, pets.name as petName,

src/test/java/org/apache/ibatis/submitted/automapping/User.java

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class User {
2121

2222
private Integer id;
2323
private String name;
24+
private Long phone; // phone number of Long type
25+
2426
private List<Pet> pets;
2527

2628
public Integer getId() {
@@ -39,6 +41,14 @@ public void setName(String name) {
3941
this.name = name;
4042
}
4143

44+
public Long getPhone() {
45+
return phone;
46+
}
47+
48+
public void setPhone(Long phone) {
49+
this.phone = phone;
50+
}
51+
4252
public List<Pet> getPets() {
4353
return pets;
4454
}

0 commit comments

Comments
 (0)