mybatis怎么把一串sql当作字符串返回

如题所述

第1个回答  推荐于2016-06-12
<select id="DAO接口方法名称" parameterType="参数类型" resultType="返回结果类型">
select * from 表 where 。。。
</select>
resultType 可以是任意Object对象,如果多条数据,这这个方法返回的是List<Object?>,
如果确认是单条数据,可以直接 Object? ***(**); 。

没有封装成对象时,默认返回的是List<Map<字段名称String,列值Object>>这样的数据。
Dao接口:
List<Map<String,Object>> list(Integer id);
SQL:
<select id="list" parameterType="Integer" resultType="Map">
select * from aaa
<where>
<if test="null!=id">
id >#{id}
</if>
</where>
</select>
以上示例中表示查询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。本回答被提问者和网友采纳
相似回答