Edit: I've done a little more testing. It looks like any period in the column name messes it up; spaces have nothing to do with it.
I'm using version 3.2.1. I don't think the ResultHandler is necessary, but it's the scenario I had in my environment where I noticed the bug.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="IncorrectColumnNameTest">
<select id="test" resultType="HashMap">
select 1 as [Space No Period], 2 as [Space. With Period]
</select>
</mapper>
public interface IncorrectColumnNameTest
{
public void test(ResultHandler resultHandler);
}
public Class IncorrectColumnNameTestDao
{
public boolean test(FileWriter fileOutput) throws SQLException
{
final SqlSession session = MyBatisSession.openSession(con);
try
{
final ConsoleResultHandler fileHandler = new ConsoleResultHandler();
final DetailsViewExportMapper mapper = session.getMapper(DetailsViewExportMapper.class);
mapper.test(fileHandler);
} finally {
session.close();
}
public static class ConsoleResultHandler implements ResultHandler
{
public void handleResult(ResultContext rc)
{
final Map<String,Object> row = (Map<String,Object>)rc.getResultObject();
System.out.println( StringUtils.join(row.keySet(), ",") );
System.out.println( StringUtils.join(row.values(), ",") );
}
private String join(Collection<? extends Object> data)
{
String join = "";
for (Object o : data)
{
join += o.toString() + ", "
}
return join;
}
}
}
}
MyBatis output
905 [main] DEBUG IncorrectColumnNameTest.test - ooo Using Connection [com.mpti.reportlogiq.server.db.TraceConnection@16ef71] 905 [main] DEBUG IncorrectColumnNameTest.test - ==> Preparing: select 1 as [Space No Period], 2 as [Space. With Period] 906 [main] DEBUG IncorrectColumnNameTest.test - ==> Parameters: 937 [main] TRACE IncorrectColumnNameTest.test - <== Columns: Space No Period, Space. With Period 937 [main] TRACE com.mpti.reportlogiq.server.db.mappers.IncorrectColumnNameTest.test - <== Row: 1, 2
Expected output
Space No Period, Space. With Period, 1, 2,
Actual output
Space No Period, Space, 1, { With Period=2},
Comment From: harawata
It seems to be the expected behavior. If a map key contains period, MyBatis creates a nested map.
Please see the following test case: org.apache.ibatis.reflection.MetaObjectTest.shouldDemonstrateDeeplyNestedMapProperties() https://github.com/mybatis/mybatis-3/blob/master/src/test/java/org/apache/ibatis/reflection/MetaObjectTest.java#L211
Do you agree to close this issue as invalid?
Comment From: jhsheets
I think I disagree.
autoMappingBehavior is set to PARTIAL by default, which isn't supposed to auto-map nested result mappings.
Comment From: harawata
Well, it is a little bit confusing, but autoMappingBehavior=PARTIAL/FULL is applied only to nested
public class SomeBean {
private String name;
private SomeBean child;
// accessors
}
<select id="selectBean" resultType="SomeBean">
select 'A' as "name", 'B' as "child.name"
</select>
Whether autoMappingBehavior is PARTIAL or FULL, this select maps 'child.name' of the resulting bean. So, I think MyBatis works consistently here.
Having said that, it might be a valid enhancement request: provide an easy way to return non-nested map when a map key contains period. As I don't use resultType="map" very often, I will leave this issue open for others to review.
Comment From: emacarron
+1 Iwao. A dot has meaning, is not just a character.
This works like this by design. Not a bug in my opinion.