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

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 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退