开发者社区> 问答> 正文

使用MyBatis选择整数列

我正在尝试使用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

我敢肯定我在这里做的事很傻!

展开
收起
垚tutu 2019-12-26 18:23:04 923 0
1 条回答
写回答
取消 提交回答
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    换种写法吧,少年

    2020-01-04 23:02:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载