Skip to content

Commit 2bc4ce2

Browse files
kazuki43zooh3adache
authored andcommitted
Support constructor-auto-mapping using super type #1240
1 parent d5f024c commit 2bc4ce2

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
import org.apache.ibatis.executor.result.DefaultResultContext;
3030
import org.apache.ibatis.executor.result.DefaultResultHandler;
3131
import org.apache.ibatis.executor.result.ResultMapException;
32+
import org.apache.ibatis.io.Resources;
3233
import org.apache.ibatis.mapping.BoundSql;
3334
import org.apache.ibatis.mapping.Discriminator;
3435
import org.apache.ibatis.mapping.MappedStatement;
@@ -697,15 +698,24 @@ private boolean allowedConstructor(final Constructor<?> constructor, final List<
697698
if (parameterTypes.length != classNames.size()) return false;
698699
for (int i = 0; i < parameterTypes.length; i++) {
699700
final Class<?> parameterType = parameterTypes[i];
700-
if (parameterType.isPrimitive() && !primitiveTypes.getWrapper(parameterType).getName().equals(classNames.get(i))) {
701+
final String className = classNames.get(i);
702+
if (parameterType.isPrimitive() && !primitiveTypes.getWrapper(parameterType).getName().equals(className)) {
701703
return false;
702-
} else if (!parameterType.isPrimitive() && !parameterType.getName().equals(classNames.get(i))) {
704+
} else if (!parameterType.isPrimitive() && !parameterType.isAssignableFrom(toClassForName(className))) {
703705
return false;
704706
}
705707
}
706708
return true;
707709
}
708710

711+
private Class<?> toClassForName(String className) {
712+
try {
713+
return Resources.classForName(className);
714+
} catch (ClassNotFoundException e) {
715+
throw new ExecutorException(e);
716+
}
717+
}
718+
709719
private List<String> typeNames(Class<?>[] parameterTypes) {
710720
List<String> names = new ArrayList<String>();
711721
for (Class<?> type : parameterTypes) {

src/test/java/org/apache/ibatis/autoconstructor/CreateDB.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2017 the original author or authors.
2+
-- Copyright 2009-2018 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -22,10 +22,11 @@ CREATE TABLE subject (
2222
name VARCHAR(20),
2323
age INT NOT NULL,
2424
height INT,
25-
weight INT
25+
weight INT,
26+
dt TIMESTAMP
2627
);
2728

28-
INSERT INTO subject (id, name, age, height, weight) VALUES (1, 'a', 10, 100, 45);
29-
INSERT INTO subject (id, name, age, height, weight) VALUES (2, 'b', 10, NULL, 45);
30-
INSERT INTO subject (id, name, age, height, weight) VALUES (2, 'b', 10, NULL, NULL);
29+
INSERT INTO subject (id, name, age, height, weight, dt) VALUES (1, 'a', 10, 100, 45, CURRENT_TIMESTAMP );
30+
INSERT INTO subject (id, name, age, height, weight, dt) VALUES (2, 'b', 10, NULL, 45, CURRENT_TIMESTAMP);
31+
INSERT INTO subject (id, name, age, height, weight, dt) VALUES (2, 'b', 10, NULL, NULL, CURRENT_TIMESTAMP);
3132

src/test/java/org/apache/ibatis/autoconstructor/PrimitiveSubject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,22 @@
1515
*/
1616
package org.apache.ibatis.autoconstructor;
1717

18+
import java.util.Date;
19+
1820
public class PrimitiveSubject {
1921
private final int id;
2022
private final String name;
2123
private final int age;
2224
private final int height;
2325
private final int weight;
26+
private final Date dt;
2427

25-
public PrimitiveSubject(final int id, final String name, final int age, final int height, final int weight) {
28+
public PrimitiveSubject(final int id, final String name, final int age, final int height, final int weight, final Date dt) {
2629
this.id = id;
2730
this.name = name;
2831
this.age = age;
2932
this.height = height;
3033
this.weight = weight;
34+
this.dt = dt;
3135
}
3236
}

src/test/java/org/apache/ibatis/autoconstructor/WrapperSubject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,22 @@
1515
*/
1616
package org.apache.ibatis.autoconstructor;
1717

18+
import java.sql.Timestamp;
19+
1820
public class WrapperSubject {
1921
private final int id;
2022
private final String name;
2123
private final int age;
2224
private final int height;
2325
private final int weight;
26+
private final Timestamp dt;
2427

25-
public WrapperSubject(final int id, final String name, final int age, final Integer height, final Integer weight) {
28+
public WrapperSubject(final int id, final String name, final int age, final Integer height, final Integer weight, Timestamp dt) {
2629
this.id = id;
2730
this.name = name;
2831
this.age = age;
2932
this.height = height == null ? 0 : height;
3033
this.weight = weight == null ? 0 : weight;
34+
this.dt = dt;
3135
}
3236
}

0 commit comments

Comments
 (0)