Example usage for org.springframework.jdbc.core.namedparam BeanPropertySqlParameterSource BeanPropertySqlParameterSource

List of usage examples for org.springframework.jdbc.core.namedparam BeanPropertySqlParameterSource BeanPropertySqlParameterSource

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam BeanPropertySqlParameterSource BeanPropertySqlParameterSource.

Prototype

public BeanPropertySqlParameterSource(Object object) 

Source Link

Document

Create a new BeanPropertySqlParameterSource for the given bean.

Usage

From source file:net.turnbig.jdbcx.JdbcxDaoSupport.java

public Map<String, Object> queryForMap(String sql, Object beanParamSource) {
    return getNamedParameterJdbcTemplate().queryForMap(sql,
            new BeanPropertySqlParameterSource(beanParamSource));
}

From source file:net.turnbig.jdbcx.JdbcxDaoSupport.java

public List<Map<String, Object>> queryForListMap(String sql, Object beanParamSource) {
    return getNamedParameterJdbcTemplate().queryForList(sql,
            new BeanPropertySqlParameterSource(beanParamSource));
}

From source file:com.gopivotal.spring.xd.module.jdbc.JdbcTasklet.java

private String runCommand(StepContext steoContext, String sqlCommand) throws Exception {

    String msg = "";
    if (sqlCommand.trim().toUpperCase().startsWith("SELECT")) {
        logger.debug("Executing: " + sqlCommand);
        List<Map<String, Object>> result = jdbcTemplate.queryForList(sqlCommand,
                new BeanPropertySqlParameterSource(steoContext));
        msg = "Result: " + result;
        logger.debug(msg);/* ww  w  .ja v a  2s. c  o m*/
    } else {
        logger.debug("Updating : " + sqlCommand);
        int updated = jdbcTemplate.update(sqlCommand, new BeanPropertySqlParameterSource(steoContext));
        msg = "Updated: " + updated + " rows";
        logger.debug(msg);
    }

    return msg;
}

From source file:com.clican.pluto.dataprocess.engine.processes.JdbcProcessor.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public void process(ProcessorContext context) throws DataProcessException {
    // ????jdbcExecBean
    for (JdbcExecBean jdbcExecBean : jdbcExecBeanList) {
        String sql = jdbcExecBean.getSql().trim();
        // sql???
        if (StringUtils.isEmpty(sql)) {
            log.warn("The sql is null, we ignore it");
            return;
        }//from   w  w  w. j  a  v a 2s .  c  om
        boolean singleRow = jdbcExecBean.isSingleRow();
        Class<?> clazz = jdbcExecBean.getClazz();
        // select,insert,update,delete
        String command = sql.toLowerCase().substring(0, 6);
        // ?
        Object temp = jdbcExecBean.getParam(context);
        Map<String, Object> param;
        if (temp instanceof Map) {
            param = (Map<String, Object>) temp;
        } else if (StringUtils.isNotEmpty(jdbcExecBean.getParamName())) {
            param = Collections.singletonMap(jdbcExecBean.getParamName(), temp);
        } else {
            throw new DataProcessException("?");
        }
        if (command.equals("select")) {
            List<?> list;
            if (clazz == null) {
                list = jdbcTemplate.queryForList(sql, new MapAndNestedPropertySqlParameterSource(param));
            } else {
                if (clazz.isAssignableFrom(Date.class) || clazz.equals(Long.class)
                        || clazz.equals(Integer.class) || clazz.equals(Float.class)
                        || clazz.equals(Double.class) || clazz.equals(String.class)) {
                    list = jdbcTemplate.query(sql, new MapAndNestedPropertySqlParameterSource(param),
                            new SingleColumnRowMapper(clazz) {

                                protected Object getColumnValue(ResultSet rs, int index, Class requiredType)
                                        throws SQLException {
                                    Object obj = super.getColumnValue(rs, index, requiredType);
                                    if (obj instanceof Date) {
                                        return new Date(((Date) obj).getTime());
                                    } else {
                                        return obj;
                                    }
                                }

                                protected Object getColumnValue(ResultSet rs, int index) throws SQLException {
                                    Object obj = super.getColumnValue(rs, index);
                                    if (obj instanceof Date) {
                                        return new Date(((Date) obj).getTime());
                                    } else {
                                        return obj;
                                    }
                                }

                            });
                } else {
                    list = jdbcTemplate.query(sql, new MapAndNestedPropertySqlParameterSource(param),
                            new BeanPropertyRowMapper(clazz));
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("jdbc set attribute[" + jdbcExecBean.getResultName() + "],size=" + list.size());
            }
            if (singleRow) {
                if (list.size() == 0) {
                    context.setAttribute(jdbcExecBean.getResultName(), null);
                } else if (list.size() == 1) {
                    context.setAttribute(jdbcExecBean.getResultName(), list.get(0));
                } else {
                    throw new DataProcessException("???");
                }
            } else {
                context.setAttribute(jdbcExecBean.getResultName(), list);
            }
        } else if (command.equals("insert") || command.equals("update") || command.equals("delete")
                || command.equals("trunca")) {
            if (sql.contains(";")) {
                String updateSql = sql.split(";")[0];
                String insertSql = sql.split(";")[1];
                int row = jdbcTemplate.update(updateSql, param);
                if (row == 0) {
                    row = jdbcTemplate.update(insertSql, param);
                }
            } else {
                if (jdbcExecBean.isBatch() && temp instanceof List) {
                    List tempList = (List) temp;
                    SqlParameterSource[] sources = new SqlParameterSource[tempList.size()];
                    for (int i = 0; i < tempList.size(); i++) {
                        Object obj = tempList.get(i);
                        SqlParameterSource namedParameters;
                        if (obj instanceof Map) {
                            namedParameters = new MapAndNestedPropertySqlParameterSource((Map) obj);
                        } else {
                            namedParameters = new BeanPropertySqlParameterSource(obj);
                        }
                        sources[i] = namedParameters;
                    }
                    new SimpleJdbcTemplate(jdbcTemplate.getJdbcOperations()).batchUpdate(sql, sources);
                } else {
                    SqlParameterSource namedParameters;
                    if (param instanceof Map) {
                        namedParameters = new MapAndNestedPropertySqlParameterSource((Map) param);
                    } else {
                        namedParameters = new BeanPropertySqlParameterSource(param);
                    }
                    int row = jdbcTemplate.update(sql, namedParameters);
                    if (log.isDebugEnabled()) {
                        log.debug(command + "=" + row + " row");
                    }
                }
            }
        } else {
            throw new DataProcessException("sql");
        }
    }
}

From source file:com.fmguler.ven.Ven.java

/**
 * Save the object. If it has a non null (or non zero) "id" property it will
 * be updated.//from w  ww  .  j a v a2  s  . c  o  m
 * It will be inserted otherwise.
 * <p>
 * The object will be saved to a table with the same name as the object,
 * The fields of object will be mapped to the table fields.
 *
 * @param object the object to be saved
 */
public void save(Object object) {
    String query = null;

    if (isObjectNew(object)) {
        //if this is a new object assign a new id first
        generateId(object);
        query = generator.generateInsertQuery(object);
    } else {
        query = generator.generateUpdateQuery(object);
    }

    //execute the insert/update query
    SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(object);
    template.update(query, parameterSource);
}

From source file:net.turnbig.jdbcx.JdbcxDaoSupport.java

public <T> T queryForObject(String sql, Object beanParamSource, Class<T> requiredType) {
    return getNamedParameterJdbcTemplate().queryForObject(sql,
            new BeanPropertySqlParameterSource(beanParamSource), requiredType);
}

From source file:net.turnbig.jdbcx.JdbcxDaoSupport.java

public <T> List<T> queryForList(String sql, Object beanParamSource, Class<T> elementType)
        throws DataAccessException {
    return getNamedParameterJdbcTemplate().queryForList(sql,
            new BeanPropertySqlParameterSource(beanParamSource), elementType);
}

From source file:com.wandrell.pattern.repository.spring.SpringJdbcRepository.java

/**
 * Adds an entity to the repository, or updates it if it already exists.
 * <p>/*from w  w w .j a va 2 s  .c  o  m*/
 * Note that both the {@code add} and the {@link #update(PersistenceEntity)
 * update} methods work the same. If the entity does not exist it will be
 * added, but if it already exists then it will be updated.
 * <p>
 * If the entity is to be updated, then the update query received on the
 * constructor will be used.
 * <p>
 * If it is inserted, a query generated from the data received by the
 * constructor will be used.
 *
 * @param entity
 *            the entity to add
 */
@Override
public final void add(final V entity) {
    final SqlParameterSource parameterSource; // Parameters source
    final Number newKey; // Key assigned to the new
                         // entity

    checkNotNull(entity, "Received a null pointer as the entity");

    parameterSource = new BeanPropertySqlParameterSource(entity);

    if ((entity.getId() == null) || (entity.getId() < 0)) {
        // No ID has been assigned
        // It is a new entity
        newKey = getInsertHandler().executeAndReturnKey(parameterSource);

        entity.setId(newKey.intValue());
    } else {
        // ID already assigned
        // It is an existing entity
        getTemplate().update(getUpdateQueryTemplate(), parameterSource);
    }
}

From source file:com.wandrell.pattern.repository.spring.SpringJdbcRepository.java

/**
 * Removes an entity from the repository.
 * <p>// w  w  w. j a va 2s  . c om
 * For this operation the delete query received on the constructor will be
 * used.
 *
 * @param entity
 *            the entity to remove
 */
@Override
public final void remove(final V entity) {
    final SqlParameterSource parameterSource; // Parameters source

    parameterSource = new BeanPropertySqlParameterSource(entity);

    getTemplate().update(getDeleteQueryTemplate(), parameterSource);
}

From source file:org.pssframework.dao.BaseSpringJdbcDao.java

private int queryTotalCount(String countQuery, Object filtersObject) {
    XsqlFilterResult countQueryXsqlResult = getXsqlBuilder().generateHql(countQuery, filtersObject);
    String removedOrderByQuery = SqlRemoveUtils.removeOrders(countQueryXsqlResult.getXsql());
    final int totalCount = getNamedParameterJdbcTemplate().queryForInt(removedOrderByQuery,
            new BeanPropertySqlParameterSource(filtersObject));
    return totalCount;
}