• springboot jdbctemplate 常用的语法


    1.新增数据 返回自增主键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); 请查看https://www.cnblogs.com/gwq369/p/5438224.html

    2.查询List<
    Map<String, Object>>

    public List<Map<String, Object>> queryDevices(Integer memberId, String deviceType) {
            String sql = "select * from " + tableName + " where member_id=" + memberId + " and device_type='" + deviceType + "' and state=1 order by device_id desc limit 1";
            List<Map<String, Object>> list = jdbcTemplateNotify.queryForList(sql);
            return list;
    }

    查询条件是字符串的话需要加上 ' '

    3.修改

    public int updateTuiSongSet(int tuisongSetId, String tuisongFlag, String tuisongType, int parentMemberId, String parentPayFlag) {
        StringJoiner joiner = new StringJoiner(",", "update " + tableName + " set", " where tuisong_set_id=" + tuisongSetId);
        if (!tuisongFlag.isEmpty()) {
            joiner.add(" tuisong_flag=" + tuisongFlag);
        }
        if (!tuisongType.isEmpty()) {
            joiner.add(" tuisong_type='" + tuisongType + "'");
        }
        if (parentMemberId > 0) {
            joiner.add(" parent_member_id=" + parentMemberId);
        }
        if (!parentPayFlag.isEmpty()) {
            joiner.add(" parent_pay_flag=" + parentPayFlag);
        }
        int update = jdbcTemplateNotify.update(joiner.toString());
        return update;
    }

     使用 StringJoiner 进行拼接查询条件




  • 相关阅读:
    现状和措施
    Nginx http升级到https
    搭建 git 服务器
    Vue + Springboot 开发的简单的用户管理系统
    Vue中的button事件
    Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则
    asp.net mvc 多级目录结构
    Asp.net下使用HttpModule模拟Filter,实现权限控制
    JavaScript事件冒泡简介及应用
    Rhino Mock
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/13959029.html
Copyright © 2020-2023  润新知