mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Add JdbcSpecialtyRepositoryExtImpl implementation
This commit is contained in:
parent
b912cca00f
commit
fa2be4a5ee
1 changed files with 86 additions and 39 deletions
|
@ -1,39 +1,86 @@
|
||||||
package org.springframework.samples.petclinic.repository.jdbc;
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import java.util.Map;
|
||||||
import org.springframework.dao.DataAccessException;
|
|
||||||
import org.springframework.samples.petclinic.model.Specialty;
|
import javax.sql.DataSource;
|
||||||
import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
@Repository
|
import org.springframework.dao.DataAccessException;
|
||||||
@Qualifier("SpecialtyRepositoryExt")
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
public class JdbcSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||||
@Override
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
public Specialty findById(int id) {
|
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||||
// TODO Auto-generated method stub
|
import org.springframework.orm.ObjectRetrievalFailureException;
|
||||||
return null;
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
}
|
import org.springframework.samples.petclinic.repository.SpecialtyRepositoryExt;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
@Override
|
|
||||||
public Collection<Specialty> findAll() throws DataAccessException {
|
@Repository
|
||||||
// TODO Auto-generated method stub
|
@Qualifier("SpecialtyRepositoryExt")
|
||||||
return null;
|
public class JdbcSpecialtyRepositoryExtImpl implements SpecialtyRepositoryExt {
|
||||||
}
|
|
||||||
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
@Override
|
|
||||||
public void save(Specialty specialty) throws DataAccessException {
|
private SimpleJdbcInsert insertSpecialty;
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
@Autowired
|
||||||
}
|
public JdbcSpecialtyRepositoryExtImpl(DataSource dataSource) {
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
@Override
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
public void delete(Specialty specialty) throws DataAccessException {
|
|
||||||
// TODO Auto-generated method stub
|
this.insertSpecialty = new SimpleJdbcInsert(dataSource)
|
||||||
|
.withTableName("specialties")
|
||||||
}
|
.usingGeneratedKeyColumns("id");
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public Specialty findById(int id) {
|
||||||
|
Specialty specialty;
|
||||||
|
try {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", id);
|
||||||
|
specialty = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
|
"SELECT id, name FROM specialties WHERE id= :id",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||||
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
|
throw new ObjectRetrievalFailureException(Specialty.class, id);
|
||||||
|
}
|
||||||
|
return specialty;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Specialty> findAll() throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
return this.namedParameterJdbcTemplate.query(
|
||||||
|
"SELECT id, name FROM specialties",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(Specialty specialty) throws DataAccessException {
|
||||||
|
// TODO not sure - need verify correct insert
|
||||||
|
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(specialty);
|
||||||
|
if (specialty.isNew()) {
|
||||||
|
Number newKey = this.insertSpecialty.executeAndReturnKey(parameterSource);
|
||||||
|
specialty.setId(newKey.intValue());
|
||||||
|
} else {
|
||||||
|
this.namedParameterJdbcTemplate.update("UPDATE specialties SET name=:name WHERE id=:id",
|
||||||
|
parameterSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Specialty specialty) throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", specialty.getId());
|
||||||
|
this.namedParameterJdbcTemplate.update("DELETE FROM specialties WHERE id=:id", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue