Package org.springframework.jdbc.core.namedparam

Examples of org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource


  /**
   * 适用sqlserver,mysql 自动生成主键
   */
  protected void insertWithGeneratedKey(Object entity, String insertSql) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getNamedParameterJdbcTemplate().update(insertSql, new BeanPropertySqlParameterSource(entity) , keyHolder);
    setIdentifierProperty(entity, keyHolder.getKey().longValue());
  }
View Full Code Here


  }

  protected void insertWithSequence(Object entity,AbstractSequenceMaxValueIncrementer sequenceIncrementer,String insertSql) {
    Long id = sequenceIncrementer.nextLongValue();
    setIdentifierProperty(entity, id);
    getNamedParameterJdbcTemplate().update(insertSql, new BeanPropertySqlParameterSource(entity));
  }
View Full Code Here

  }
 
  protected void insertWithUUID(Object entity,String insertSql) {
    String uuid = UUID.randomUUID().toString().replace("-", "");
    setIdentifierProperty(entity, uuid);
    getNamedParameterJdbcTemplate().update(insertSql, new BeanPropertySqlParameterSource(entity));
  }
View Full Code Here

   * 手工分配ID插入
   * @param entity
   * @param insertSql
   */
  protected void insertWithAssigned(Object entity,String insertSql) {
    getNamedParameterJdbcTemplate().update(insertSql, new BeanPropertySqlParameterSource(entity));
  }
View Full Code Here

  }
 
  public E getById(PK id) {
    List list = null;
    if(getSqlGenerator().getTable().getPrimaryKeyCount() > 1) {
      list = getNamedParameterJdbcTemplate().query(getSqlGenerator().getSelectByPkSql(), new BeanPropertySqlParameterSource(id), new BeanPropertyRowMapper(getEntityClass()));
    }else if(getSqlGenerator().getTable().getPrimaryKeyCount() == 1){
      list = getSimpleJdbcTemplate().query(getSqlGenerator().getSelectByPkSql(), ParameterizedBeanPropertyRowMapper.newInstance(getEntityClass()), id);
    }else {
      throw new IllegalStateException("not found primary key on table:"+getSqlGenerator().getTable().getTableName());
    }
View Full Code Here

    return (E)CollectionHelper.findSingleObject(list);
  }

  public void deleteById(PK id) {
    if(getSqlGenerator().getTable().getPrimaryKeyCount() > 1) {
      getNamedParameterJdbcTemplate().update(getSqlGenerator().getDeleteByPkSql(),new BeanPropertySqlParameterSource(id));
    }else if(getSqlGenerator().getTable().getPrimaryKeyCount() == 1){
      getSimpleJdbcTemplate().update(getSqlGenerator().getDeleteByPkSql(), id);
    }else {
      throw new IllegalStateException("not found primary key on table:"+getSqlGenerator().getTable().getTableName());
    }
View Full Code Here

      update(entity);
    }
  }
  public void update(E entity) {
    String sql = getSqlGenerator().getUpdateByPkSql();
    getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
  }
View Full Code Here

  protected ParameterizedBeanPropertyRowMapper resultBeanMapper(Class clazz) {
    return ParameterizedBeanPropertyRowMapper.newInstance(clazz);
  }
 
  protected BeanPropertySqlParameterSource paramBeanMapper(Object object) {
    return new BeanPropertySqlParameterSource(object);
  }
View Full Code Here

  public int updateObject(String sql, Object object) throws DataAccessException {
    if (debug) {
      logger.info("sql:" + sql);
      logger.info("object:" + object);
    }
    SqlParameterSource namedParam = new BeanPropertySqlParameterSource(object);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int ret = getNameParameterJdbcTemplate().update(sql, namedParam, keyHolder);
    if (ret <= 0)
      return ret;
    try {
View Full Code Here

      if (arg instanceof Map) {
        msp.addValues((Map)arg);
        continue;
      }
     
      BeanPropertySqlParameterSource sps = new BeanPropertySqlParameterSource(arg);
      List<Field> fields = new ArrayList<Field>();
      for (Class<?> c = arg.getClass(); c != null; c = c.getSuperclass()) {
        if (c.equals(Object.class))
          continue;
        fields.addAll(Arrays.asList(c.getDeclaredFields()));
      }
      for (Field f : fields) {
        try {
          String fn = f.getName();
          if (msp.hasValue(fn))
            sqlLogger.warn(String
                .format("Field with name=%s has "
                    + "been already mapped by another arg bean. Overriding!",
                    fn));
          if (Enum.class.isAssignableFrom(f.getType())) {
            sps.registerSqlType(f.getName(), Types.VARCHAR);
          }

          msp.addValue(fn, sps.getValue(fn), sps.getSqlType(fn),
              sps.getTypeName(fn));
          logger.debug(String.format("prepared sql arg: name=%s, value=%s, type=%s",
              fn, sps.getValue(fn), sps.getTypeName(fn)));
        } catch (Exception e) {
          Throwables.propagate(e);
        }
      }
    }
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.