jdbcTemplate.queryForList(sql, object, classType) 的返回类型
我按以下方式使用 jdbcTemplate.queryForList 执行命名查询:
List<Conversation> conversations = jdbcTemplate.queryForList(
SELECT_ALL_CONVERSATIONS_SQL_FULL,
new Object[] {userId, dateFrom, dateTo});
SQL 查询是:
private final String SELECT_ALL_CONVERSATIONS_SQL_FULL =
"select conversation.conversationID, conversation.room, " +
"conversation.isExternal, conversation.startDate, " +
"conversation.lastActivity, conversation.messageCount " +
"from openfire.ofconversation conversation " +
"WHERE conversation.conversationid IN " +
"(SELECT conversation.conversationID " +
"FROM openfire.ofconversation conversation, " +
"openfire.ofconparticipant participant " +
"WHERE conversation.conversationID = participant.conversationID " +
"AND participant.bareJID LIKE ? " +
"AND conversation.startDate between ? AND ?)";
但是当按以下方式提取列表的内容时:
for (Conversation conversation : conversations) {
builder.append(conversation.getId());
builder.append(",");
builder.append(conversation.getRoom());
builder.append(",");
builder.append(conversation.getIsExternal());
builder.append(",");
builder.append(conversation.getStartDate());
builder.append(",");
builder.append(conversation.getEndDate());
builder.append(",");
builder.append(conversation.getMsgCount());
out.write(builder.toString());
}
我收到错误:
java.util.LinkedHashMap cannot be cast to net.org.messagehistory.model.Conversation
如何将此 linkedMap 转换为所需的对象? ?
谢谢
I'm executing a named query using jdbcTemplate.queryForList in the following manner:
List<Conversation> conversations = jdbcTemplate.queryForList(
SELECT_ALL_CONVERSATIONS_SQL_FULL,
new Object[] {userId, dateFrom, dateTo});
The SQL query is:
private final String SELECT_ALL_CONVERSATIONS_SQL_FULL =
"select conversation.conversationID, conversation.room, " +
"conversation.isExternal, conversation.startDate, " +
"conversation.lastActivity, conversation.messageCount " +
"from openfire.ofconversation conversation " +
"WHERE conversation.conversationid IN " +
"(SELECT conversation.conversationID " +
"FROM openfire.ofconversation conversation, " +
"openfire.ofconparticipant participant " +
"WHERE conversation.conversationID = participant.conversationID " +
"AND participant.bareJID LIKE ? " +
"AND conversation.startDate between ? AND ?)";
But when extracting the content of the list in the following manner:
for (Conversation conversation : conversations) {
builder.append(conversation.getId());
builder.append(",");
builder.append(conversation.getRoom());
builder.append(",");
builder.append(conversation.getIsExternal());
builder.append(",");
builder.append(conversation.getStartDate());
builder.append(",");
builder.append(conversation.getEndDate());
builder.append(",");
builder.append(conversation.getMsgCount());
out.write(builder.toString());
}
I get an error:
java.util.LinkedHashMap cannot be cast to net.org.messagehistory.model.Conversation
How do I convert this linkedMap into the desired Object??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了将查询的结果集映射到特定的 Java 类,您可能最好(假设您有兴趣在其他地方使用该对象)使用 RowMapper 将结果集中的列转换为对象实例。
请参阅使用 JDBC 进行数据访问的第 12.2.1.1 节如何使用行映射器。
简而言之,您需要类似的东西:
In order to map a the result set of query to a particular Java class you'll probably be best (assuming you're interested in using the object elsewhere) off with a RowMapper to convert the columns in the result set into an object instance.
See Section 12.2.1.1 of Data access with JDBC on how to use a row mapper.
In short, you'll need something like:
JdbcTemplate、NamedParameterJdbcTemplate 的完整解决方案(带或不带 RowMapper 示例)。
// 创建员工表
============================================== ===========================
//Employee.java
=============================================== ===========================
//EmployeeDao.java
=============================================== ===================
//MyrowMapper.java
=============================================== =============
//MyPreparedStatement.java
================================================= =======================
//Test.java
=============================================== =====================
//applicationContext.xml
=================================================== =================
A complete solution for JdbcTemplate, NamedParameterJdbcTemplate with or without RowMapper Example.
// Create a Employee table
=======================================================================
//Employee.java
=========================================================================
//EmployeeDao.java
================================================================
//MyrowMapper.java
==========================================================
//MyPreparedStatement.java
=====================================================================
//Test.java
==================================================================
//applicationContext.xml
===================================================================
queryForList 返回 LinkedHashMap 对象的列表。
您需要先像这样投射它:
queryForList returns a List of LinkedHashMap objects.
You need to cast it first like this:
认为
sql 查询就像
问号是占位符
它将作为对象数组与 sql 查询一起出现
Suppose
the sql query is like
the question marks are place holders
It will go as an object array along with the sql query