petclinic-aplcache
Changes
src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java 54(+31 -23)
src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java 27(+16 -11)
Details
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
index 6a3f558..90f9dd1 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcOwnerRepositoryImpl.java
@@ -1,14 +1,15 @@
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.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
-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.ParameterizedBeanPropertyRowMapper;
@@ -19,7 +20,6 @@ import org.springframework.samples.petclinic.Pet;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Visit;
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.util.EntityUtils;
import org.springframework.stereotype.Service;
@@ -39,14 +39,8 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class JdbcOwnerRepositoryImpl implements OwnerRepository {
- @Autowired
- private PetRepository petRepository;
-
- @Autowired
private VisitRepository visitRepository;
- @Autowired
- private JdbcTemplate jdbcTemplate;
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@@ -54,11 +48,16 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
private SimpleJdbcInsert insertOwner;
@Autowired
- public void init(DataSource dataSource) {
+ public JdbcOwnerRepositoryImpl(DataSource dataSource, NamedParameterJdbcTemplate namedParameterJdbcTemplate,
+ VisitRepository visitRepository) {
this.insertOwner = new SimpleJdbcInsert(dataSource)
.withTableName("owners")
.usingGeneratedKeyColumns("id");
+
+ this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
+
+ this.visitRepository = visitRepository;
}
@@ -72,10 +71,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
*/
@Transactional(readOnly = true)
public Collection<Owner> findByLastName(String lastName) throws DataAccessException {
- List<Owner> owners = this.jdbcTemplate.query(
- "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like ?",
- ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),
- lastName + "%");
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("lastName", lastName + "%");
+ List<Owner> owners = this.namedParameterJdbcTemplate.query(
+ "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE last_name like :lastName",
+ params,
+ ParameterizedBeanPropertyRowMapper.newInstance(Owner.class)
+ );
loadOwnersPetsAndVisits(owners);
return owners;
}
@@ -89,10 +91,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
public Owner findById(int id) throws DataAccessException {
Owner owner;
try {
- owner = this.jdbcTemplate.queryForObject(
- "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id=?",
- ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),
- id);
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("id", id);
+ owner = this.namedParameterJdbcTemplate.queryForObject(
+ "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id= :id",
+ params,
+ ParameterizedBeanPropertyRowMapper.newInstance(Owner.class)
+ );
}
catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Owner.class, new Integer(id));
@@ -102,10 +107,13 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
}
public void loadPetsAndVisits(final Owner owner) {
- final List<JdbcPet> pets = this.jdbcTemplate.query(
- "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=?",
- new JdbcPetRowMapper(),
- owner.getId().intValue());
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("id", owner.getId().intValue());
+ final List<JdbcPet> pets = this.namedParameterJdbcTemplate.query(
+ "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE owner_id=:id",
+ params,
+ new JdbcPetRowMapper()
+ );
for (JdbcPet pet : pets) {
owner.addPet(pet);
pet.setType(EntityUtils.getById(getPetTypes(), PetType.class, pet.getTypeId()));
@@ -139,8 +147,8 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
@Transactional(readOnly = true)
public Collection<PetType> getPetTypes() throws DataAccessException {
- return this.jdbcTemplate.query(
- "SELECT id, name FROM types ORDER BY name",
+ return this.namedParameterJdbcTemplate.query(
+ "SELECT id, name FROM types ORDER BY name", new HashMap<String,Object>(),
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
}
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
index a2a0045..5c92c25 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcPetRepositoryImpl.java
@@ -1,13 +1,14 @@
package org.springframework.samples.petclinic.repository.jdbc;
+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.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
@@ -35,40 +36,44 @@ import org.springframework.stereotype.Repository;
@Repository
public class JdbcPetRepositoryImpl implements PetRepository {
- private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private SimpleJdbcInsert insertPet;
- @Autowired
private OwnerRepository ownerRepository;
- @Autowired
private VisitRepository visitRepository;
+
@Autowired
- public void init(DataSource dataSource) {
- this.jdbcTemplate = new JdbcTemplate(dataSource);
+ public JdbcPetRepositoryImpl(DataSource dataSource, OwnerRepository ownerRepository, VisitRepository visitRepository) {
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
this.insertPet = new SimpleJdbcInsert(dataSource)
.withTableName("pets")
.usingGeneratedKeyColumns("id");
+
+ this.ownerRepository = ownerRepository;
+ this.visitRepository = visitRepository;
}
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",
+ params,
ParameterizedBeanPropertyRowMapper.newInstance(PetType.class));
}
public Pet findById(int id) throws DataAccessException {
JdbcPet pet;
try {
- pet = this.jdbcTemplate.queryForObject(
- "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=?",
- new JdbcPetRowMapper(),
- id);
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put("id", id);
+ pet = this.namedParameterJdbcTemplate.queryForObject(
+ "SELECT id, name, birth_date, type_id, owner_id FROM pets WHERE id=:id",
+ params,
+ new JdbcPetRowMapper());
}
catch (EmptyResultDataAccessException ex) {
throw new ObjectRetrievalFailureException(Pet.class, new Integer(id));
diff --git a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
index c54eb10..52519ee 100644
--- a/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
+++ b/src/main/java/org/springframework/samples/petclinic/repository/jdbc/JdbcVisitRepositoryImpl.java
@@ -39,7 +39,7 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
private SimpleJdbcInsert insertVisit;
@Autowired
- public void init(DataSource dataSource) {
+ public JdbcVisitRepositoryImpl(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.insertVisit = new SimpleJdbcInsert(dataSource)
diff --git a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java
index a7f760a..77bf6b5 100644
--- a/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java
+++ b/src/main/java/org/springframework/samples/petclinic/web/OwnerController.java
@@ -66,8 +66,7 @@ public class OwnerController {
@RequestMapping(value = "/owners/find", method = RequestMethod.GET)
public String initFindForm(Model model) {
- model.addAttribute("owner", new Owner());
- return "owners/findOwners";
+ throw new RuntimeException("aaaaaaa");
}
@RequestMapping(value = "/owners", method = RequestMethod.GET)
diff --git a/src/main/webapp/WEB-INF/petclinic-servlet.xml b/src/main/webapp/WEB-INF/petclinic-servlet.xml
index 299409a..dbb255a 100644
--- a/src/main/webapp/WEB-INF/petclinic-servlet.xml
+++ b/src/main/webapp/WEB-INF/petclinic-servlet.xml
@@ -94,6 +94,7 @@
-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="exception"/>
+ <property name="warnLogCategory" value="warn"/>
</bean>