我正在尝试使用MyBatis从包含Integer的表中获取单个列。我想将其提取为列表(尽管在这一点上我可能是错的!)
这是我的mapper.xml
<select id="getPersonIds" resultType="java.util.List">
SELECT PersonId
FROM TestDB.users
</select>
这是我的Mapper界面
package com.paulmjohnson.Mappers;
import java.util.List;
public interface PersonMapper {
List<Integer> getPersonIds();
}
我的DAO:
public List<Integer> getPersonIds() {
SqlSession session = MyBatisUtil.getSqlSessionFactory().openSession();
logger.info("Getting Person IDs");
List<Integer> personIds = session.selectList("com.paulmjohnson.Mappers.PersonMapper.getPersonIds");
session.close();
return personIds;
}
以及我怎么称呼它:
PeopleDAO peopleDAO = new PeopleDAO();
List<Integer> personIds = peopleDAO.getPersonIds();
MyBatis似乎正在创建我期望的SELECT查询:
2019-12-26 09:39:06,677 [main] [DEBUG] c.p.Mappers.PersonMapper.getPersonIds - ==> Preparing: SELECT PersonId FROM TestDB.users
2019-12-26 09:39:06,678 [main] [DEBUG] c.p.Mappers.PersonMapper.getPersonIds - ==> Parameters:
当我运行它时,我得到以下信息:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.UnsupportedOperationException
### The error may exist in person-mapper.xml
### The error may involve com.paulmjohnson.Mappers.PersonMapper.getPersonIds
### The error occurred while handling results
### SQL: SELECT PersonId FROM TestDB.users
### Cause: java.lang.UnsupportedOperationException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:135)
at com.paulmjohnson.DAO.PeopleDAO.getPersonIds(PeopleDAO.java:82)
at com.paulmjohnson.UsingBuckets.main(UsingBuckets.java:105)
Caused by: java.lang.UnsupportedOperationException
at org.apache.ibatis.reflection.wrapper.CollectionWrapper.findProperty(CollectionWrapper.java:48)
at org.apache.ibatis.reflection.MetaObject.findProperty(MetaObject.java:85)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createAutomaticMappings(DefaultResultSetHandler.java:493)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyAutomaticMappings(DefaultResultSetHandler.java:517)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:402)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 4 more
我敢肯定我在这里做的事很傻!
换种写法吧,少年
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。