使用 MyBatis 注解接收 PostgreSQL 的 returning 结果

MyBatis PostgreSQL About 958 words

returning

PostgreSQL支持在insert/update/delete语句中添加returning返回一些记录的信息。

如:插入记录后返回主键id

insert into t_test(column1, column2) values(value1, value2) returning id;

MyBatis

使用MyBatis中的@Select@Options两个注解完成接收(没错,增删改也用@Select注解)。

如:以插入留言板信息为例。

@Mapper
public interface MybatisService {
    @Select("insert into message_board(nickname, content, floor, ip_id, ua, os, browser, reply_id, root_id, reply_count, create_ts, update_ts) values(" +
            "#{nickname}," +
            "#{content}," +
            "#{floor}," +
            "(select id from ip_pool where ip=#{ip}::inet)," +
            "#{userAgent}," +
            "#{os}," +
            "#{browser}," +
            "#{reply_id}," +
            "#{root_id}," +
            "#{reply_count}," +
            "#{commentTime}," +
            "#{commentTime}" +
            ") RETURNING id")
    @Options(flushCache = Options.FlushCachePolicy.TRUE)
    Integer insertMessageBoard(MessageBoard messageBoard);
}
Views: 5,875 · Posted: 2020-04-25

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh