mybatis 查询数据库返回值某字段是 List 该怎么搞

2025-02-12 12:07:51
推荐回答(2个)
回答1:


resultType 可以是任意Object对象,如果多条数据,这这个方法返回的是List
如果确认是单条数据,可以直接 Object? ***(**); 。

没有封装成对象时,默认返回的是List>这样的数据。
Dao接口:
List> list(Integer id);
SQL:

以上示例中表示查询id>某个数值的所有结果,返回类型为MAP

执行脚本后没有返回结果的吧,看ScriptRunner源码,没有提供任何返回结果的。
private void executeStatement(String command) throws SQLException, UnsupportedEncodingException {
boolean hasResults = false;
Statement statement = connection.createStatement();
statement.setEscapeProcessing(escapeProcessing);
String sql = command;
if (removeCRs)
sql = sql.replaceAll("\r\n", "\n");
if (stopOnError) {
hasResults = statement.execute(sql);
} else {
try {
hasResults = statement.execute(sql);
} catch (SQLException e) {
String message = "Error executing: " + command + ". Cause: " + e;
printlnError(message);
}
}
printResults(statement, hasResults);
try {
statement.close();
} catch (Exception e) {
// Ignore to workaround a bug in some connection pools
}
}

...

有结果时,最后调用了这个方法打印出来而已。
private void print(Object o) {
if (logWriter != null) {
logWriter.print(o);
logWriter.flush();
}
}

你可以调用
public void setLogWriter(PrintWriter logWriter) {
this.logWriter = logWriter;
}
传入你自己的Writer。

回答2:

解决方法:如果确认是单条数据,可以直接 Object? ***(**); 。没有封装成对象时,默认返回的是List>这样的数据。Dao接口:
List> list(Integer id);
SQL: