茂盛的小绿芽 2015-05-26 03:39 采纳率: 75%
浏览 6099
已采纳

spring jdbctemplate 插入数据

手动拼装的sql : insert into tableA (id,name) values(:id,:name)

使用BeanPropertySqlParameterSource

执行:jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(vo));
出问题了,
我跟代码进去 发现new BeanPropertySqlParameterSource(vo) 所有集合是空的,但是我的vo对象是有值的

-------------源码-------------
public int insertVo(SuperVo vo) throws Exception {
StringBuffer insert = new StringBuffer(" insert into ");
StringBuffer insert_value = new StringBuffer(" values ( ");
insert.append(vo.getTableName()).append(" ( ");

    // 主键/序列
    String pkFieldName = vo.getPKFieldName();
    String sequence = vo.getSequence();
    Integer seq = null;
    if (pkFieldName != null) {
        if (sequence != null && null == vo.getAttributeValue(vo.getPKFieldName())) {
            seq = this.queryNextSeq(sequence);
            vo.setAttributeValue(pkFieldName, seq);
        } else {
            seq = Util.getIntegerValue(vo.getAttributeValue(vo.getPKFieldName()));
        }
    }

    List<String> list = vo.getAttributeNames();
    int[] types = new int[list.size()];
    Object[] params = new Object[list.size()];
    String split = "";
    for (int i = 0; i < list.size(); i++) {
        String fieldName = list.get(i);
        insert.append(split).append(fieldName);
        insert_value.append(split).append(":").append(fieldName);
        split = ",";
    }
    insert.append(")");
    insert_value.append(")");

    SqlParameterSource paramSource = new BeanPropertySqlParameterSource(vo);
    String sql = insert.append(insert_value).toString();
    jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(vo));


    }
 ![图片说明](https://img-ask.csdn.net/upload/201505/26/1432611494_91042.jpg)

![图片说明](https://img-ask.csdn.net/upload/201505/26/1432611544_196163.jpg)

  • 写回答

1条回答 默认 最新

  • 茂盛的小绿芽 2015-06-21 23:37
    关注

    虽然没有人回答,但是这个问题解决了,就此结贴。将数据缘转成namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
    jdbcTemplate.getDataSource());就ok了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示