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