use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project bamboobsc by billchen198318.
the class ManualJdbcTemplateFactory method getManualJdbcTemplate.
public static NamedParameterJdbcTemplate getManualJdbcTemplate(String confOid) throws ServiceException, Exception {
if (jdbcTemplateTreadLocal.get() != null && jdbcTemplateTreadLocal.get().getDataSourceConf().getOid().equals(confOid)) {
return jdbcTemplateTreadLocal.get().getJdbcTemplate();
}
DataSourceConfVO conf = new DataSourceConfVO();
conf.setOid(confOid);
DefaultResult<DataSourceConfVO> confResult = dataSourceConfService.findObjectByOid(conf);
if (confResult.getValue() == null) {
throw new ServiceException(confResult.getSystemMessage().getValue());
}
conf = confResult.getValue();
DataSourceDriverVO driver = new DataSourceDriverVO();
driver.setId(conf.getDriverId());
DefaultResult<DataSourceDriverVO> driverResult = dataSourceDriverService.findByUK(driver);
if (driverResult.getValue() == null) {
throw new ServiceException(driverResult.getSystemMessage().getValue());
}
driver = driverResult.getValue();
NamedParameterJdbcTemplate jdbcTemplate = DataUtils.getManualJdbcTemplate(Class.forName(driver.getClassName()), conf.getJdbcUrl(), conf.getDbAccount(), conf.getDbPassword());
DataProperty dataProperty = new DataProperty();
dataProperty.setDataSourceConf(conf);
dataProperty.setJdbcTemplate(jdbcTemplate);
jdbcTemplateTreadLocal.set(dataProperty);
return jdbcTemplate;
}
use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project lavagna by digitalfondue.
the class PersistenceAndServiceConfig method simpleJdbcTemplate.
@Bean
public NamedParameterJdbcTemplate simpleJdbcTemplate(LavagnaEnvironment env, DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
// mysql does not support check constraints
if ("MYSQL".equals(env.getProperty("datasource.dialect"))) {
CustomSQLErrorCodesTranslator tr = new CustomSQLErrorCodesTranslator();
tr.setDataSource(dataSource);
jdbcTemplate.setExceptionTranslator(tr);
}
return new NamedParameterJdbcTemplate(jdbcTemplate);
}
use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.
the class UserDaoImpl method getUserByUsername.
@Override
public User getUserByUsername(String username) throws DataAccessException {
Map<String, String> namedParams = new HashMap<String, String>();
namedParams.put("user_name", username);
String sql = sqlDictionary.getSQLQuery("read-user-by-name");
NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
List<User> users = template.query(sql, namedParams, new UsersExtractor());
if (users.isEmpty())
throw new EmptyResultDataAccessException(1);
return users.get(0);
}
use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.
the class UserDaoImpl method updateUser.
@Override
public void updateUser(User src) {
final String UPDATE_SQL = sqlDictionary.getSQLQuery("update-user");
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
// key to identify application to be updated
namedParameters.addValue("user_id", src.getId());
// values to update
namedParameters.addValue("user_name", src.getUsername());
namedParameters.addValue("first_name", src.getFirstName());
namedParameters.addValue("last_name", src.getLastName());
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
int affectedRows = jdbcTemplate.update(UPDATE_SQL, namedParameters);
if (affectedRows != 1) {
String msg = "oops something wrong occurred" + affectedRows + " rows were affected instead of only 1.";
Logger.error(msg);
throw new NextProtException(msg);
}
if (src.getAuthorities() != null && !src.getAuthorities().isEmpty()) {
// 1. delete all roles for this user if roles exist in user_roles table of src
deleteUserRoles(src.getId());
// 2. insert roles with insertUserRoles(src.getKey(), src.getRoles())
insertUserAuthorities(src.getId(), src.getAuthorities());
}
}
use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.
the class UserDaoImpl method deleteUserRoles.
private void deleteUserRoles(final long userId) {
final String DELETE_SQL = sqlDictionary.getSQLQuery("delete-user-roles");
Map<String, Object> params = new HashMap<String, Object>();
params.put("user_id", userId);
new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).update(DELETE_SQL, params);
}
Aggregations