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

MyBatisPostgreSQL大约 957 字

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);
}
阅读 357 · 发布于 2020-04-25

————        END        ————

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

昵称:
随便看看换一批