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

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

Introduction

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

Prototype

public MapSqlParameterSource() 

Source Link

Document

Create an empty MapSqlParameterSource, with values to be added via addValue .

Usage

From source file:com.eu.evaluation.server.dao.eva.UniqueEvaluateItemDAO.java

public UniqueEvaluateItem findTheMatching(ObjectDictionary od, FieldDictionary fd,
        Map<String, Object> otherMap) {
    String jpql = "select t from UniqueEvaluateItem t where t.fieldDictionary.id = :fdID ";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("fdID", fd.getId());
    List<UniqueEvaluateItem> result = this.query(jpql, params);
    if (result.isEmpty()) {
        return null;
    } else if (result.size() == 1) {
        return result.get(0);
    } else {/*from ww w . j  a v a 2  s  .com*/
        throw new RuntimeException("" + fd.getObjectDictionary().getDisplayname() + " , "
                + fd.getDisplayname() + " ??");
    }
}

From source file:com.manning.siia.batch.PaymentWriter.java

@Override
public void write(List<? extends Payment> payments) throws Exception {
    for (Payment payment : payments) {
        MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        parameterSource.addValue("RECIPIENT", payment.getDestinationAccountNo())
                .addValue("PAYEE", payment.getSourceAccountNo()).addValue("AMOUNT", payment.getAmount())
                .addValue("DATE", payment.getDate());
        accountUpdate.update("UPDATE ACCOUNTS SET BALANCE = BALANCE + ? WHERE ID = ?", payment.getAmount(),
                payment.getDestinationAccountNo());
        accountUpdate.update("UPDATE ACCOUNTS SET BALANCE = BALANCE - ? WHERE ID = ?", payment.getAmount(),
                payment.getSourceAccountNo());
        paymentInsert.execute(parameterSource);
        System.out.println("Executing the step " + payment.getDate());
    }/*from  ww w.j  a  va2 s .c o m*/
}

From source file:org.sakuli.services.forwarder.database.dao.DaoTest.java

@Test
public void testCreateSqlSetStringForNamedParameter() throws Throwable {
    testling = new Dao(dataSource) {
    };//from  w w  w .  j  a  v a2s .  c  o  m
    MapSqlParameterSource source = new MapSqlParameterSource().addValue("testling2", "value")
            .addValue("testling", "value");
    Assert.assertEquals("SET testling2=:testling2, testling=:testling ",
            testling.createSqlSetStringForNamedParameter(source.getValues()));
    source.addValue("nullable", null);
    Assert.assertEquals("SET testling2=:testling2, testling=:testling ",
            testling.createSqlSetStringForNamedParameter(source.getValues()));

}

From source file:com.eu.evaluation.server.dao.dictionary.FieldDictionaryDAO.java

public FieldDictionary findByObjectAndProperty(String objectID, String property) {
    String jpql = "select t from FieldDictionary t where t.objectDictionary.id = :oid and t.propertyName = :property";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("oid", objectID);
    params.addValue("property", property);
    try {//from   www  .j a v  a 2s  . c om
        return (FieldDictionary) this.createQuery(jpql, params).getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.stehno.sjdbcx.reflection.DefaultParamMapper.java

@Override
public SqlParameterSource mapByName(final AnnotatedArgument[] annotatedArguments) {
    final MapSqlParameterSource parameterSource = new MapSqlParameterSource();

    if (annotatedArguments != null) {
        for (final AnnotatedArgument arg : annotatedArguments) {
            if (arg.findAnnotation(Ignore.class) == null) {
                final Annotation paramAnno = arg.findAnnotation(Param.class);
                if (paramAnno == null) {
                    addBean(parameterSource, arg.getValue());

                } else {
                    Object value = arg.getValue();
                    if (value != null && value.getClass().isEnum()) {
                        value = ((Enum) value).name();
                    }//w  w  w  .j a v a2s .  co  m

                    parameterSource.addValue(((Param) paramAnno).value(), value);
                }
            }
        }
    }

    return parameterSource;
}

From source file:com.xinferin.dao.DAOActivationImpl.java

@Override
public int getCount(int licenceId) {

    String sql = "SELECT COUNT(*) FROM activation WHERE licence_id = :id";
    MapSqlParameterSource args = new MapSqlParameterSource();
    args = new MapSqlParameterSource();
    args.addValue("id", licenceId);

    int count = jdbcTemplate.queryForObject(sql, args, Integer.class);

    return count;
}

From source file:im.dadoo.cas.server.dao.UserDao.java

public Optional<User> findByName(String name) {
    Preconditions.checkNotNull(name, "name?null");
    MapSqlParameterSource sps = new MapSqlParameterSource();
    sps.addValue("name", name);
    List<User> users = this.jdbcTemplate.query(FIND_BY_NAME_SQL, sps, this.baseRowMapper);
    if (users != null && !users.isEmpty()) {
        return Optional.of(users.get(0));
    } else {//w w  w .j a v a  2s  .  c om
        return Optional.absent();
    }
}

From source file:com.griddynamics.spring.batch.football.internal.JdbcPlayerSummaryDao.java

public void write(List<? extends PlayerSummary> summaries) {

    for (PlayerSummary summary : summaries) {

        MapSqlParameterSource args = new MapSqlParameterSource().addValue("id", summary.getId())
                .addValue("year", summary.getYear()).addValue("completes", summary.getCompletes())
                .addValue("attempts", summary.getAttempts()).addValue("passingYards", summary.getPassingYards())
                .addValue("passingTd", summary.getPassingTd())
                .addValue("interceptions", summary.getInterceptions()).addValue("rushes", summary.getRushes())
                .addValue("rushYards", summary.getRushYards()).addValue("receptions", summary.getReceptions())
                .addValue("receptionYards", summary.getReceptionYards())
                .addValue("totalTd", summary.getTotalTd());

        getSimpleJdbcTemplate().update(INSERT_SUMMARY, args);

    }//from  ww w . j  av a2s. c o m

}

From source file:com.ignite.dao.AccountDao.java

@PreAuthorize("hasRole('ROLE_TELLER')")
public void saveAccount(Account account) {
    logger.info("Saving account: " + account.getOwner().getLogin());
    int count = getSimpleJdbcTemplate().update(
            "insert into account (client,balance) values (:client, :balance)",
            new MapSqlParameterSource().addValue("client", account.getOwner().getLogin()).addValue("balance",
                    account.getBalance()));
    logger.info("Rows affected: " + count);
}

From source file:paillard.florent.springframework.simplejdbcupdate.SimpleJdbcUpdateTestCase.java

@Test
public void testSqlParameterSource() {
    DataSource dataSource = new DriverManagerDataSource("jdbc:hsqldb:mem:testdb");
    SimpleJdbcTemplate createTableTemplate = new SimpleJdbcTemplate(dataSource);
    createTableTemplate.update(//from   w  w w .ja  va 2s  .  c  o m
            "create table dummy_table (key_1 VARCHAR(50), key_2 INT, a_string VARCHAR(50), an_int INTEGER, a_bool BIT) ");

    SimpleJdbcUpdate simpleJdbcUpdate = new SimpleJdbcUpdate(dataSource).withTableName("dummy_table")
            .updatingColumns("a_string", "an_int", "a_bool").restrictingColumns("key_1", "key_2");

    SqlParameterSource mapSqlParameterSource1 = new MapSqlParameterSource().addValue("a_string", "Hello")
            .addValue("an_int", 42).addValue("a_bool", true);

    SqlParameterSource mapSqlParameterSource2 = new MapSqlParameterSource().addValue("key_1", "Pwet")
            .addValue("key_2", 3);

    simpleJdbcUpdate.execute(mapSqlParameterSource1, mapSqlParameterSource2);
}