不支持的操作异常mybatis

时间:2014-09-15 07:48:38

标签: sql exception-handling mybatis

所以在这段代码中我想要的是来自外部的随机SQL查询,它将被加载到属性文件中。到目前为止,我已经获得了带有查询的属性文件来测试它。所以我想要一些数据,标题和下面的所有数据。基本上只是开始的数据,因为这个测试应该做。但是我收到了以下链接的错误消息。我不能为我的生活找出问题所在。请帮忙! :)

我已获得以下代码;

DataHandler类。

public class DataHandler{

DataService dataService = new DataService();

public String getPropertyValue() throws IOException {
    Properties prop = new Properties();
    String propFileName = "randomSqlQuery.properties";

    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
    prop.load(inputStream);
    if (inputStream == null) {
        throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
    }
    String result = prop.getProperty("sqlQuery");
    return result;
}

public Data getKeysAndValues() throws IOException {
    String query = getPropertyValue();
    List<List<Object>> randomSqlQuery = dataService.getRandomSqlQuery(query);
    List<List<Object>> recordList = new ArrayList<>();
    List<String> headline = new ArrayList();

    if (randomSqlQuery != null && randomSqlQuery.size() > 0) {
        {
            List<Object> record = randomSqlQuery.get(0);
            getHeadlines(record, headline);
        }
        for (int i = 1; i < randomSqlQuery.size(); i++) {
            List<Object> singleRecord = randomSqlQuery.get(i);
            recordList.add(singleRecord);
            System.out.println(recordList);
        }
    }
    return new DataImpl(headline, recordList);
}

private void getHeadlines(List<Object> record, List<String> headline) {
    for (Object headlineName : record) {
        headline.add((String) headlineName);
        System.out.println(headlineName);
    }
}
}  

DataMapper类

public interface DataMapper {
public List<List<Object>> getRandomSqlQuery(@Param("query") String query);

}

DataMapper XML

<?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="nd.mappers.DataMapper">

<select id="getRandomSqlQuery" resultType="java.util.List">
    ${query}
</select>
</mapper>

DataImpl类,它有一个连接到它的接口

public class DataImpl implements Serializable, Data {

private final List<String> headers;
private final List<List<Object>> records;

public DataImpl(List<String> headers, List<List<Object>> records) {
    this.headers = Collections.unmodifiableList(headers);
    this.records = Collections.unmodifiableList(records);
}

@Override
public List<String> getHeaders() {
    return this.headers;
}

@Override
public List<List<Object>> getRecords() {
    return this.records;
}

}

和DataService类

public class DataService implements DataMapper {

@Override
public List<List<Object>> getRandomSqlQuery(String query) {
    SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
    try {
        DataMapper dataMapper = sqlSession.getMapper(DataMapper.class);
        return dataMapper.getRandomSqlQuery(query);
    } finally {
        sqlSession.close();
    }
}
}

最后的测试

//dataHandler instantiated in top
    @Test
public void getKeysAndValues() throws IOException {
    dataHandler.getKeysAndValues();
}

这是我的错误!

 ### Error querying database.  Cause: java.lang.UnsupportedOperationException 
 ### The error may exist in nd/mappers/DataMapper.xml 
  ### The error may involve defaultParameterMap 
 ### The error    occurred while setting parameters 
 ### SQL: SELECT * FROM PERSON ### Cause:    java.lang.UnsupportedOperationException
org.apache.ibatis.exceptions.PersistenceException
### Error querying database.  Cause: java.lang.UnsupportedOperationException
### The error may exist in nd/mappers/DataMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * FROM PERSON
### Cause: java.lang.UnsupportedOperationException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:111)

不知道该怎么做。 SQL来自属性文件。对不起大文。

1 个答案:

答案 0 :(得分:0)

我刚刚解决了相同的错误消息。问题出在这里:

<select id="getRandomSqlQuery" resultType="Person">
    ${query}
</select>

结果类型不应该是集合,而是类型 集合包含。在你的情况下它应该是一些Person POJO。然后必须在(比如mybatis-config.xml)配置中定义此类型 文件:

<typeAliases>
    <typeAlias alias="Person" type="com.example.bean.Book"/>  
</typeAliases>  

此外,您还需要在此处检查null

try {
     DataMapper dataMapper = qlSession.getMapper(DataMapper.class);
     return dataMapper.getRandomSqlQuery(query);
} finally {
     if (sqlSession != null) {
         sqlSession.close();
     }
}