mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-05-29 22:59:38 +00:00
removing JdbcTemplate so we use NamedParameterJdbcTemplate solely when possible
This commit is contained in:
parent
f44e383462
commit
74eb3e72fe
5 changed files with 50 additions and 37 deletions
|
@ -1,14 +1,15 @@
|
||||||
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.List;
|
import java.util.List;
|
||||||
|
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.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.EmptyResultDataAccessException;
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
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.ParameterizedBeanPropertyRowMapper;
|
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
|
||||||
|
@ -19,7 +20,6 @@ import org.springframework.samples.petclinic.Pet;
|
||||||
import org.springframework.samples.petclinic.PetType;
|
import org.springframework.samples.petclinic.PetType;
|
||||||
import org.springframework.samples.petclinic.Visit;
|
import org.springframework.samples.petclinic.Visit;
|
||||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
|
||||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -39,14 +39,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
@Service
|
@Service
|
||||||
public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private PetRepository petRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VisitRepository visitRepository;
|
private VisitRepository visitRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
@ -54,11 +48,16 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
private SimpleJdbcInsert insertOwner;
|
private SimpleJdbcInsert insertOwner;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void init(DataSource dataSource) {
|
public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate,
|
||||||
|
VisitRepository visitRepository) {
|
||||||
|
|
||||||
this.insertOwner = new SimpleJdbcInsert(dataSource)
|
this.insertOwner = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("owners")
|
.withTableName("owners")
|
||||||
.usingGeneratedKeyColumns("id");
|
.usingGeneratedKeyColumns("id");
|
||||||
|
|
||||||
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
|
||||||
|
this.visitRepository = visitRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,10 +71,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
*/
|
*/
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Collection<Owner> findByLastName(String lastName) throws DataAccessException {
|
public Collection<Owner> findByLastName(String lastName) throws DataAccessException {
|
||||||
List<Owner> owners = this.jdbcTemplate.query(
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like ?",
|
params.put("lastName", lastName + "%");
|
||||||
ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),
|
List<Owner> owners = this.namedParameterJdbcTemplate.query(
|
||||||
lastName + "%");
|
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
|
||||||
|
params,
|
||||||
|
ParameterizedBeanPropertyRowMapper.newInstance(Owner.class)
|
||||||
|
);
|
||||||
loadOwnersPetsAndVisits(owners);
|
loadOwnersPetsAndVisits(owners);
|
||||||
return owners;
|
return owners;
|
||||||
}
|
}
|
||||||
|
@ -89,10 +91,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
public Owner findById(int id) throws DataAccessException {
|
public Owner findById(int id) throws DataAccessException {
|
||||||
Owner owner;
|
Owner owner;
|
||||||
try {
|
try {
|
||||||
owner = this.jdbcTemplate.queryForObject(
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id=?",
|
params.put("id", id);
|
||||||
ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),
|
owner = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
id);
|
"SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
|
||||||
|
params,
|
||||||
|
ParameterizedBeanPropertyRowMapper.newInstance(Owner.class)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
catch (EmptyResultDataAccessException ex) {
|
catch (EmptyResultDataAccessException ex) {
|
||||||
throw new ObjectRetrievalFailureException(Owner.class, new Integer(id));
|
throw new ObjectRetrievalFailureException(Owner.class, new Integer(id));
|
||||||
|
@ -102,10 +107,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPetsAndVisits(final Owner owner) {
|
public void loadPetsAndVisits(final Owner owner) {
|
||||||
final List<JdbcPet> pets = this.jdbcTemplate.query(
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=?",
|
params.put("id", owner.getId().intValue());
|
||||||
new JdbcPetRowMapper(),
|
final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
|
||||||
owner.getId().intValue());
|
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
|
||||||
|
params,
|
||||||
|
new JdbcPetRowMapper()
|
||||||
|
);
|
||||||
for (JdbcPet pet : pets) {
|
for (JdbcPet pet : pets) {
|
||||||
owner.addPet(pet);
|
owner.addPet(pet);
|
||||||
pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
|
pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
|
||||||
|
@ -139,8 +147,8 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Collection<PetType> getPetTypes() throws DataAccessException {
|
public Collection<PetType> getPetTypes() throws DataAccessException {
|
||||||
return this.jdbcTemplate.query(
|
return this.namedParameterJdbcTemplate.query(
|
||||||
"SELECT id, name FROM types ORDER BY name",
|
"SELECT id, name FROM types ORDER BY name", new HashMap<String,Object>(),
|
||||||
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
|
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package org.springframework.samples.petclinic.repository.jdbc;
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
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.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.EmptyResultDataAccessException;
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
|
||||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||||
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
|
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
|
||||||
|
@ -35,40 +36,44 @@ import org.springframework.stereotype.Repository;
|
||||||
@Repository
|
@Repository
|
||||||
public class JdbcPetRepositoryImpl implements PetRepository {
|
public class JdbcPetRepositoryImpl implements PetRepository {
|
||||||
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
|
||||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
private SimpleJdbcInsert insertPet;
|
private SimpleJdbcInsert insertPet;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private OwnerRepository ownerRepository;
|
private OwnerRepository ownerRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private VisitRepository visitRepository;
|
private VisitRepository visitRepository;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void init(DataSource dataSource) {
|
public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
|
||||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
|
|
||||||
this.insertPet = new SimpleJdbcInsert(dataSource)
|
this.insertPet = new SimpleJdbcInsert(dataSource)
|
||||||
.withTableName("pets")
|
.withTableName("pets")
|
||||||
.usingGeneratedKeyColumns("id");
|
.usingGeneratedKeyColumns("id");
|
||||||
|
|
||||||
|
this.ownerRepository = ownerRepository;
|
||||||
|
this.visitRepository = visitRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PetType> findPetTypes() throws DataAccessException {
|
public List<PetType> findPetTypes() throws DataAccessException {
|
||||||
return this.jdbcTemplate.query(
|
Map<String, Object> params = new HashMap<String,Object>();
|
||||||
|
return this.namedParameterJdbcTemplate.query(
|
||||||
"SELECT id, name FROM types ORDER BY name",
|
"SELECT id, name FROM types ORDER BY name",
|
||||||
|
params,
|
||||||
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
|
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pet findById(int id) throws DataAccessException {
|
public Pet findById(int id) throws DataAccessException {
|
||||||
JdbcPet pet;
|
JdbcPet pet;
|
||||||
try {
|
try {
|
||||||
pet = this.jdbcTemplate.queryForObject(
|
Map<String, Object> params = new HashMap<String, Object>();
|
||||||
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=?",
|
params.put("id", id);
|
||||||
new JdbcPetRowMapper(),
|
pet = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
id);
|
"SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
|
||||||
|
params,
|
||||||
|
new JdbcPetRowMapper());
|
||||||
}
|
}
|
||||||
catch (EmptyResultDataAccessException ex) {
|
catch (EmptyResultDataAccessException ex) {
|
||||||
throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
|
throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
|
||||||
|
|
|
@ -39,7 +39,7 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
||||||
private SimpleJdbcInsert insertVisit;
|
private SimpleJdbcInsert insertVisit;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void init(DataSource dataSource) {
|
public JdbcVisitRepositoryImpl(DataSource dataSource) {
|
||||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
|
||||||
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
this.insertVisit = new SimpleJdbcInsert(dataSource)
|
||||||
|
|
|
@ -66,8 +66,7 @@ public class OwnerController {
|
||||||
|
|
||||||
@RequestMapping(value = "/owners/find", method = RequestMethod.GET)
|
@RequestMapping(value = "/owners/find", method = RequestMethod.GET)
|
||||||
public String initFindForm(Model model) {
|
public String initFindForm(Model model) {
|
||||||
model.addAttribute("owner", new Owner());
|
throw new RuntimeException("aaaaaaa");
|
||||||
return "owners/findOwners";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/owners", method = RequestMethod.GET)
|
@RequestMapping(value = "/owners", method = RequestMethod.GET)
|
||||||
|
|
|
@ -94,6 +94,7 @@
|
||||||
-->
|
-->
|
||||||
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
|
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
|
||||||
<property name="defaultErrorView" value="exception"/>
|
<property name="defaultErrorView" value="exception"/>
|
||||||
|
<property name="warnLogCategory" value="warn"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue