Last active
December 7, 2020 17:13
-
-
Save Honatas/8a04b33efe2b5160dc4aa73de305db98 to your computer and use it in GitHub Desktop.
BaseDAO for Spring's JDBC helper classes. Check BaseModel: https://gist.github.com/Honatas/b67a3dd0f6cf0556f1c42c30aaf6acb4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
import java.util.Map; | |
import javax.sql.DataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.dao.EmptyResultDataAccessException; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.RowMapper; | |
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | |
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | |
import org.springframework.jdbc.core.simple.SimpleJdbcCall; | |
import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | |
public class BaseDao { | |
@Autowired | |
protected DataSource dataSource; | |
protected void insert(String tableName, MapSqlParameterSource params) { | |
new SimpleJdbcInsert(dataSource).withTableName(tableName).execute(params); | |
} | |
protected Integer insertGenerateKey(String tableName, String primaryKeyName, MapSqlParameterSource params) { | |
return new SimpleJdbcInsert(dataSource).withTableName(tableName).usingGeneratedKeyColumns(primaryKeyName).executeAndReturnKey(params).intValue(); | |
} | |
protected Integer insertGenerateKey(String tableName, MapSqlParameterSource params) { | |
return this.insertGenerateKey(tableName, "id_" + tableName, params); | |
} | |
protected void delete(String tableName, String primaryKeyName, Object primaryKey) { | |
new JdbcTemplate(dataSource).update("delete from " + tableName + " where " + primaryKeyName + " = ?", primaryKey); | |
} | |
protected void delete(String tableName, Object primaryKey) { | |
this.delete(tableName, "id_" + tableName, primaryKey); | |
} | |
protected int update(String sql, MapSqlParameterSource params) { | |
return new NamedParameterJdbcTemplate(dataSource).update(sql, params); | |
} | |
protected <T extends BaseModel<T>> T getOne(String sql, MapSqlParameterSource params, Class<T> clazz) { | |
try { | |
return new NamedParameterJdbcTemplate(dataSource).queryForObject(sql, params, this.getRowMapper(clazz)); | |
} catch (EmptyResultDataAccessException e) { | |
return null; | |
} | |
} | |
protected <T extends BaseModel<T>> List<T> getMany(String sql, MapSqlParameterSource params, Class<T> clazz) { | |
return new NamedParameterJdbcTemplate(dataSource).query(sql, params, this.getRowMapper(clazz)); | |
} | |
private <T extends BaseModel<T>> RowMapper<T> getRowMapper(Class<T> clazz) { | |
try { | |
return clazz.getConstructor().newInstance().getRowMapper(); | |
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | InstantiationException e) { | |
throw new UnsupportedOperationException("Model Bean did not implement RowMapper: " + clazz.getName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment