Mapper.xml配置:
select sid_lv3 as sid3 from table1 where sid_lv3 is not null and sid_lv3 != '';
Mapper类:
public interface TableMapper {
//查询sid_lv3
List selectSidlv3();
}
注意上面XML配置的resultType,如果写成java.util.List的话会出现以下异常:
严重: Servlet.service() for servlet [appServlet] in context with path [/test] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in file [TableMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select sid_lv3 as sid3 from table where sid_lv3 is not null and sid_lv3 != '';
### Cause: java.lang.UnsupportedOperationException] with root cause
java.lang.UnsupportedOperationException
at org.apache.ibatis.reflection.wrapper.CollectionWrapper.findProperty(CollectionWrapper.java:42)
at org.apache.ibatis.reflection.MetaObject.findProperty(MetaObject.java:86)
如果写成java.lang.String的话,则不会出现问题
至于什么原因,有网上说的是MyBatis会默认把查出来的结果按String类型分割,成为List,是不是这样的情况我也不清楚。