【问题标题】:Unsupported Operation Exception mybatismybatis 不支持的操作异常
【发布时间】:2014-09-15 07:48:38
【问题描述】:

因此,在这段代码中,我想要的是来自外部的随机 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 来自属性文件。抱歉,有大量文字。

【问题讨论】:

  • 就我使用MyBatis而言,传递参数的语法是#{query}而不是${query}。请检查错误是否与此更改不同。
  • 要传递像@Param("query")这样的参数,我需要使用${query},如果没有,我会收到这样的错误:MySQLSyntaxErrorException,它再也找不到我的sqlquery两者都不。可惜不是这样=(
  • OK.. 我刚刚检查了 DefaultSqlSession.java 的 selectList 方法,它期望 List 作为返回类型。 List> 可能不受支持。尝试使用 List 然后相应地处理 Object 。现在看看有没有错误。
  • 感谢您的帮助,我们刚刚从另一个站点获得了一些帮助,试图使其作为哈希图返回,然后转换为列表。

标签: sql exception-handling mybatis


【解决方案1】:

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

<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();
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    • 2012-03-08
    • 2021-11-21
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多