mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Add JDBC extended repositories implementation
This commit is contained in:
parent
dafa2772de
commit
8be14d6b75
4 changed files with 249 additions and 0 deletions
|
@ -0,0 +1,51 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
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.dao.DataAccessException;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
|
import org.springframework.samples.petclinic.model.Owner;
|
||||||
|
import org.springframework.samples.petclinic.repository.OwnerRepositoryExt;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Qualifier("OwnerRepositoryExt")
|
||||||
|
public class JdbcOwnerRepositoryImplExt extends JdbcOwnerRepositoryImpl implements OwnerRepositoryExt {
|
||||||
|
|
||||||
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JdbcOwnerRepositoryImplExt(DataSource dataSource) {
|
||||||
|
super(dataSource);
|
||||||
|
// TODO super() ?
|
||||||
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Owner> findAll() throws DataAccessException {
|
||||||
|
List<Owner> owners = this.namedParameterJdbcTemplate.query(
|
||||||
|
"SELECT id, first_name, last_name, address, city, telephone FROM owners",
|
||||||
|
new HashMap<String, Object>(),
|
||||||
|
BeanPropertyRowMapper.newInstance(Owner.class));
|
||||||
|
for (Owner owner : owners) {
|
||||||
|
loadPetsAndVisits(owner);
|
||||||
|
}
|
||||||
|
return owners;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Owner owner) throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", owner.getId());
|
||||||
|
this.namedParameterJdbcTemplate.update("DELETE FROM owners WHERE id=:id", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
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.dao.DataAccessException;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
|
import org.springframework.samples.petclinic.model.Pet;
|
||||||
|
import org.springframework.samples.petclinic.repository.OwnerRepositoryExt;
|
||||||
|
import org.springframework.samples.petclinic.repository.PetRepositoryExt;
|
||||||
|
import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Qualifier("PetRepositoryExt")
|
||||||
|
public class JdbcPetRepositoryImplExt extends JdbcPetRepositoryImpl implements PetRepositoryExt {
|
||||||
|
|
||||||
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JdbcPetRepositoryImplExt(DataSource dataSource,
|
||||||
|
@Qualifier("OwnerRepositoryExt") OwnerRepositoryExt ownerRepository,
|
||||||
|
@Qualifier("VisitRepositoryExt") VisitRepositoryExt visitRepository) {
|
||||||
|
super(dataSource, ownerRepository, visitRepository);
|
||||||
|
// TODO super () ?
|
||||||
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Pet> findAll() throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
return this.namedParameterJdbcTemplate.query(
|
||||||
|
"SELECT id, name, birth_date, type_id, owner_id FROM pets",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Pet.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Pet pet) throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", pet.getId());
|
||||||
|
this.namedParameterJdbcTemplate.update("DELETE FROM pets WHERE id=:id", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
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.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
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.Owner;
|
||||||
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
|
import org.springframework.samples.petclinic.repository.VetRepositoryExt;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Qualifier("VetRepositoryExt")
|
||||||
|
public class JdbcVetRepositoryImplExt extends JdbcVetRepositoryImpl implements VetRepositoryExt {
|
||||||
|
|
||||||
|
//private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
|
private SimpleJdbcInsert insertVet;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JdbcVetRepositoryImplExt(DataSource dataSource, JdbcTemplate jdbcTemplate) {
|
||||||
|
super(jdbcTemplate);
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
//this.jdbcTemplate = jdbcTemplate;
|
||||||
|
|
||||||
|
this.insertVet = new SimpleJdbcInsert(dataSource)
|
||||||
|
.withTableName("vets")
|
||||||
|
.usingGeneratedKeyColumns("id");
|
||||||
|
|
||||||
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Vet findById(int id) throws DataAccessException {
|
||||||
|
Vet vet;
|
||||||
|
try {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", id);
|
||||||
|
vet = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
|
"SELECT id, first_name, last_name FROM vets WHERE id= :id",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Vet.class));
|
||||||
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
|
throw new ObjectRetrievalFailureException(Vet.class, id);
|
||||||
|
}
|
||||||
|
return vet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(Vet vet) throws DataAccessException {
|
||||||
|
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(vet);
|
||||||
|
if (vet.isNew()) {
|
||||||
|
Number newKey = this.insertVet.executeAndReturnKey(parameterSource);
|
||||||
|
vet.setId(newKey.intValue());
|
||||||
|
} else {
|
||||||
|
this.namedParameterJdbcTemplate.update(
|
||||||
|
"UPDATE vets SET first_name=:firstName, last_name=:lastName WHERE id=:id",
|
||||||
|
parameterSource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Vet vet) throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", vet.getId());
|
||||||
|
this.namedParameterJdbcTemplate.update("DELETE FROM vets WHERE id=:id", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
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.dao.DataAccessException;
|
||||||
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||||
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
|
import org.springframework.orm.ObjectRetrievalFailureException;
|
||||||
|
import org.springframework.samples.petclinic.model.Visit;
|
||||||
|
import org.springframework.samples.petclinic.repository.VisitRepositoryExt;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@Qualifier("VisitRepositoryExt")
|
||||||
|
public class JdbcVisitRepositoryImplExt extends JdbcVisitRepositoryImpl implements VisitRepositoryExt {
|
||||||
|
|
||||||
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public JdbcVisitRepositoryImplExt(DataSource dataSource) {
|
||||||
|
super(dataSource);
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Visit findById(int id) throws DataAccessException {
|
||||||
|
Visit visit;
|
||||||
|
try {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", id);
|
||||||
|
visit = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
|
"SELECT id, pet_id, visit_date, description FROM visits WHERE id= :id",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Visit.class));
|
||||||
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
|
throw new ObjectRetrievalFailureException(Visit.class, id);
|
||||||
|
}
|
||||||
|
return visit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Visit> findAll() throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
return this.namedParameterJdbcTemplate.query(
|
||||||
|
"SELECT id, pet_id, visit_date, description FROM visits",
|
||||||
|
params,
|
||||||
|
BeanPropertyRowMapper.newInstance(Visit.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(Visit visit) throws DataAccessException {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("id", visit.getId());
|
||||||
|
this.namedParameterJdbcTemplate.update("DELETE FROM visits WHERE id=:id", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue