Search in sources :

Example 6 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project disconf by knightliao.

the class GenericDao method createAndFetchUpdateRow.

// FIXME
@SuppressWarnings("unchecked")
private int createAndFetchUpdateRow(ENTITY entity, final String sql, final List<Object> params) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    recordLog(sql);
    // 执行操作
    int rowCount = this.jdbcTemplate.update(new PreparedStatementCreator() {

        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql);
            int index = 1;
            for (Object param : params) {
                ps.setObject(index++, param);
            }
            return ps;
        }
    }, keyHolder);
    // 如果插入成功则获取keyHolder中的key
    if (rowCount != 0) {
        Class<KEY> keyClass = orMapping.getKeyClass();
        if (keyClass.equals(Integer.class)) {
            entity.setId((KEY) Integer.valueOf(keyHolder.getKey().intValue()));
        } else if (keyClass.equals(Long.class)) {
            entity.setId((KEY) Long.valueOf(keyHolder.getKey().longValue()));
        }
    }
    return rowCount;
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) SQLException(java.sql.SQLException) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder)

Example 7 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project dal by ctripcorp.

the class DaoOfLoginUser method insertUser.

public int insertUser(final LoginUser user) {
    if (user == null || user.getUserNo() == null) {
        return -1;
    }
    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement("INSERT INTO login_users ( user_no, user_name, user_email, password ) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE user_no = ?", Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, user.getUserNo());
            ps.setString(2, user.getUserName());
            ps.setString(3, user.getUserEmail());
            ps.setString(4, user.getPassword());
            ps.setString(5, user.getUserNo());
            return ps;
        }
    }, holder);
    return holder.getKey().intValue();
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) KeyHolder(org.springframework.jdbc.support.KeyHolder)

Example 8 with PreparedStatementCreator

use of org.springframework.jdbc.core.PreparedStatementCreator in project dal by ctripcorp.

the class DaoOfUserProject method insertUserProject.

public int insertUserProject(final UserProject data) {
    KeyHolder holder = new GeneratedKeyHolder();
    this.jdbcTemplate.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement("INSERT INTO user_project (project_id, user_no ) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, data.getProject_id());
            ps.setString(2, data.getUserNo());
            return ps;
        }
    }, holder);
    return holder.getKey().intValue();
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) KeyHolder(org.springframework.jdbc.support.KeyHolder)

Aggregations

PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)8 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)6 KeyHolder (org.springframework.jdbc.support.KeyHolder)6 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)2 ResultSet (java.sql.ResultSet)2 Map (java.util.Map)2 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 DataAccessException (org.springframework.dao.DataAccessException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 InvalidDataAccessResourceUsageException (org.springframework.dao.InvalidDataAccessResourceUsageException)1 PreparedStatementCreatorFactory (org.springframework.jdbc.core.PreparedStatementCreatorFactory)1 SqlParameter (org.springframework.jdbc.core.SqlParameter)1 ParsedSql (org.springframework.jdbc.core.namedparam.ParsedSql)1 JdbcUtils.closeConnection (org.springframework.jdbc.support.JdbcUtils.closeConnection)1