gpt4 book ai didi

java - 具有 SQL SELECT FROM INSERT 的 JdbcDaoSupport

转载 作者:行者123 更新时间:2023-12-01 04:32:13 25 4
gpt4 key购买 nike

我正在尝试在 Spring JdbcDaoSupport 类中创建“从插入中选择”,但无法弄清楚如何从 select 语句中获取数据并返回它。

我的EventJdbcTemplate(我的DaoImpl):

@Service
public class EventJdbcTemplate extends JdbcDaoSupport implements EventDao {

private static final Logger LOGGER = Logger.getLogger(EventJdbcTemplate.class);

private static final String SQL_INSERT_EVENT = "SELECT EVENT_ID FROM FINAL TABLE " +
"(INSERT INTO EBT10DBB.SB0401T0 (EVENT_NAME, HOST_NAME, USER_ID) " +
"VALUES(?, ?, \'EMP0321\'))";

@Autowired
public EventJdbcTemplate(DataSource pDataSource) {
super.setDataSource(pDataSource);
}


@Override
public Integer createEvent(EventBean pEventBean) { //(Integer id, String eventName)
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Entering create(Event event) of EventJDBCTemplate.");
}
// This SQL works, but is for an INSERT only.
/*this.getJdbcTemplate().query(SQL_INSERT_EVENT, new Object[]{
pEventBean.getEventName(),
pEventBean.getHostName()
});*/

final List eventList = this.getJdbcTemplate().query(SQL_INSERT_EVENT, new Object[]{
pEventBean.getEventName(),
pEventBean.getHostName()
}, new EventRowMapper()
);

Event event = null;
for (int i = 0; i < eventList.size(); i++) {
event = (Event)eventList.get(i);
}

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Exiting create(Event event) of EventJDBCTemplate.");
}
//return statement -- should return either the entire "pEventBean", or
//just the unique key, "EVENT_ID".
return event.getId();
}

EventRowMapper 类(不确定我是否需要这个来进行选择):

public class EventRowMapper implements RowMapper<Event> {

@Override
public Event mapRow(ResultSet rs, int rowNum) throws SQLException {
final EventBuilder event = new EventImpl.EventBuilder();
event.setId(rs.getInt("EVENT_ID"));
event.setEventName("EVENT_NAME");
event.setHostName("HOST_NAME");
return event.build();
}

}

所以我的目标是返回一个整数值,该值将是从 INSERT SQL 创建的唯一键 (EVENT_ID)。

最佳答案

您可以使用Spring提供的SimpleJdbcInsert来取回生成的 key ,请参阅Spring提供的以下文档部分

13.5.2 Retrieving auto-generated keys using SimpleJdbcInsert

这是Link

关于java - 具有 SQL SELECT FROM INSERT 的 JdbcDaoSupport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17859356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com