我创建了一个实现映射的类。这将将所有键转换为上限,并防止重复
public class UCaseMap<T> implements Map<String, T> { }
现在,每当我在mybatis中使用它时,它都会将班级视为pojo
<select id="selectQuestions" parameterType="map" resultType="com.quiz.utils.UCaseMap">
这是错误消息
### Error querying database. Cause: java.lang.IllegalArgumentException: No obj found for 'QUESTION_ID' available keys []
### The error may exist in file [D:DevelopmentWorkspaceQuizCreatortargetclassescomquizmapperimplQuizMapper.xml]
### The error may involve com.quiz.mapper.QuizMapper.selectQuestions
### The error occurred while handling results
### SQL: SELECT QUESTION_ID, QUESTION, ANSWER_EXPLANATION, CATEGORY, SUB_CATEGORY FROM M_QUESTION WHERE DELETEFLAG = 0 AND CATEGORY = ? ORDER BY RANDOM() LIMIT ?
### Cause: java.lang.IllegalArgumentException: No obj found for 'QUESTION_ID' available keys [], mergedContextConfiguration = [MergedContextConfiguration@91161c7 testClass = TestQuizService, locations = '{classpath:dispatcher-servlet.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].
您知道Mybatis如何将该类视为普通地图?像这样:
<select id="selectQuestions" parameterType="map" resultType="map">
im使用:
- mybatis 3.4.1
- mybatis-spring 1.3.0
- Postgres 9.1-901-1.jdbc4
- 春季4.3.2
- Java 8和Tomcat 8
它应该起作用,我尝试了以下自定义映射实现:
public class CustomMap<T> extends HashMap<String, T>{
@Override
public boolean containsKey(Object key) {
String uKey = key.toString().toUpperCase();
return super.containsKey(uKey);
}
@Override
public T put(String key, T value) {
String uKey = key.toUpperCase();
if ( containsKey(uKey)){
return super.get(uKey);
}else{
return super.put(uKey, value);
}
}
}
和以下select
<select id="getCountriesAsCustomMap" resultType="info.sanaulla.model.CustomMap">
SELECT
c.code,
c.name,
c.continent,
c.region
FROM country c
</select>
我有一个Junit测试:
@Test
public void test_getCountriesAsCustomMap(){
List<Map<String, Object>> countries = countryService.getCountriesAsCustomMap();
System.out.println(countries);
}
给我输出:
{
CODE=AFG, CONTINENT=clob2: 'Asia',
NAME=clob1: 'Afghanistan', REGION=clob3: 'Southern and Central Asia'
}
因此自定义地图应起作用。