首页 > 数据库技术 > 详细

springboot jdbctemplate 常用的语法

时间:2020-11-11 18:14:49      阅读:70      评论:0      收藏:0      [点我收藏+]

新增数据 返回自增主键ID

public int add(Map<String,Object> paramMap) {
    long nowTime = System.currentTimeMillis(); // 当前时间
    String sql = "insert into " + tableName + " (device_no,device_key,device_type,channel_type_id,phone_type,is_attent,serial_no,member_id,sub_member_id,cashier_id,update_time,c_date,state,source_id,ios_type) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplateNotify.update(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement preparedStatement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, ComMapUtils.getString(paramMap,"deviceNo"));
            preparedStatement.setString(2, ComMapUtils.getString(paramMap,"deviceKey"));
            preparedStatement.setString(3, ComMapUtils.getString(paramMap,"deviceType"));
            preparedStatement.setInt(4, ComMapUtils.getInteger(paramMap,"channelTypeId"));
            preparedStatement.setInt(5, ComMapUtils.getInteger(paramMap,"phoneType"));
            preparedStatement.setInt(6, ComMapUtils.getInteger(paramMap,"isAttent"));
            preparedStatement.setInt(7, ComMapUtils.getInteger(paramMap,"serialNo"));
            preparedStatement.setInt(8, ComMapUtils.getInteger(paramMap,"memberId"));
            preparedStatement.setInt(9, ComMapUtils.getInteger(paramMap,"subMemberId"));
            preparedStatement.setInt(10, ComMapUtils.getInteger(paramMap,"cashierId"));
            preparedStatement.setLong(11, nowTime);
            preparedStatement.setLong(12, nowTime);
            preparedStatement.setInt(13, ComMapUtils.getInteger(paramMap,"state"));
            preparedStatement.setInt(14, ComMapUtils.getInteger(paramMap,"sourcId"));
            preparedStatement.setInt(15, ComMapUtils.getInteger(paramMap,"iosType"));
            return preparedStatement;
        }
    }, keyHolder);
    int id = keyHolder.getKey().intValue();
    return id;
}
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

springboot jdbctemplate 常用的语法

原文:https://www.cnblogs.com/gjq1126-web/p/13959029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!