2 u013890437 u013890437 于 2015.05.26 11:39 提问

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));


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

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

1个回答

u013890437
u013890437   2015.06.22 07:37
已采纳

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

Csdn user default icon
上传中...
上传图片
插入图片