Search in sources :

Example 6 with KeyHolder

use of org.springframework.jdbc.support.KeyHolder 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 7 with KeyHolder

use of org.springframework.jdbc.support.KeyHolder 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

GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)7 KeyHolder (org.springframework.jdbc.support.KeyHolder)7 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)6 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)2 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 DataAccessException (org.springframework.dao.DataAccessException)1 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)1 InvalidDataAccessResourceUsageException (org.springframework.dao.InvalidDataAccessResourceUsageException)1