mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Merge 965ef801de
into a07cf69292
This commit is contained in:
commit
55fffd3529
16 changed files with 35 additions and 68 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,3 +5,4 @@ target/*
|
|||
.idea
|
||||
*.iml
|
||||
/target
|
||||
/build/
|
||||
|
|
22
pom.xml
22
pom.xml
|
@ -244,13 +244,19 @@
|
|||
</dependency>
|
||||
|
||||
<!-- Databases - Uses HSQL by default -->
|
||||
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<!--
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>${hsqldb.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
-->
|
||||
|
||||
<!-- For MySql only -->
|
||||
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> -->
|
||||
|
@ -344,20 +350,6 @@
|
|||
</testResource>
|
||||
</testResources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.0</version>
|
||||
<configuration>
|
||||
<compilerArguments>
|
||||
<Xlint />
|
||||
</compilerArguments>
|
||||
<verbose>true</verbose>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<showWarnings>true</showWarnings>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
|
|
|
@ -71,7 +71,6 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
* the given name; also loads the {@link Pet Pets} and {@link Visit Visits} for the corresponding owners, if not
|
||||
* already loaded.
|
||||
*/
|
||||
@Override
|
||||
public Collection<Owner> findByLastName(String lastName) throws DataAccessException {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
params.put("lastName", lastName + "%");
|
||||
|
@ -88,7 +87,6 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
* Loads the {@link Owner} with the supplied <code>id</code>; also loads the {@link Pet Pets} and {@link Visit Visits}
|
||||
* for the corresponding owner, if not already loaded.
|
||||
*/
|
||||
@Override
|
||||
public Owner findById(int id) throws DataAccessException {
|
||||
Owner owner;
|
||||
try {
|
||||
|
@ -119,7 +117,6 @@ public class JdbcOwnerRepositoryImpl implements OwnerRepository {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Owner owner) throws DataAccessException {
|
||||
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(owner);
|
||||
if (owner.isNew()) {
|
||||
|
|
|
@ -71,7 +71,6 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
this.visitRepository = visitRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PetType> findPetTypes() throws DataAccessException {
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
return this.namedParameterJdbcTemplate.query(
|
||||
|
@ -80,7 +79,6 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
BeanPropertyRowMapper.newInstance(PetType.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pet findById(int id) throws DataAccessException {
|
||||
JdbcPet pet;
|
||||
try {
|
||||
|
@ -104,7 +102,6 @@ public class JdbcPetRepositoryImpl implements PetRepository {
|
|||
return pet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Pet pet) throws DataAccessException {
|
||||
if (pet.isNew()) {
|
||||
Number newKey = this.insertPet.executeAndReturnKey(
|
||||
|
|
|
@ -58,7 +58,6 @@ public class JdbcVetRepositoryImpl implements VetRepository {
|
|||
*
|
||||
* @see org.springframework.samples.petclinic.model.service.ClinicService#shouldFindVets()
|
||||
*/
|
||||
@Override
|
||||
public Collection<Vet> findAll() throws DataAccessException {
|
||||
List<Vet> vets = new ArrayList<Vet>();
|
||||
// Retrieve the list of all vets.
|
||||
|
|
|
@ -55,7 +55,6 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Visit visit) throws DataAccessException {
|
||||
if (visit.isNew()) {
|
||||
Number newKey = this.insertVisit.executeAndReturnKey(
|
||||
|
@ -78,7 +77,6 @@ public class JdbcVisitRepositoryImpl implements VisitRepository {
|
|||
.addValue("pet_id", visit.getPet().getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Visit> findByPetId(Integer petId) {
|
||||
return this.jdbcTemplate.query(
|
||||
"SELECT id as visit_id, visit_date, description FROM visits WHERE pet_id=?",
|
||||
|
|
|
@ -58,7 +58,6 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
|||
return query.getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Owner findById(int id) {
|
||||
// using 'join fetch' because a single query should load both owners and pets
|
||||
// using 'left join fetch' because it might happen that an owner does not have pets yet
|
||||
|
@ -68,7 +67,6 @@ public class JpaOwnerRepositoryImpl implements OwnerRepository {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Owner owner) {
|
||||
if (owner.getId() == null) {
|
||||
this.em.persist(owner);
|
||||
|
|
|
@ -40,18 +40,15 @@ public class JpaPetRepositoryImpl implements PetRepository {
|
|||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<PetType> findPetTypes() {
|
||||
return this.em.createQuery("SELECT ptype FROM PetType ptype ORDER BY ptype.name").getResultList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pet findById(int id) {
|
||||
return this.em.find(Pet.class, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Pet pet) {
|
||||
if (pet.getId() == null) {
|
||||
this.em.persist(pet);
|
||||
|
|
|
@ -41,7 +41,6 @@ public class JpaVetRepositoryImpl implements VetRepository {
|
|||
private EntityManager em;
|
||||
|
||||
|
||||
@Override
|
||||
@Cacheable(value = "vets")
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<Vet> findAll() {
|
||||
|
|
|
@ -43,7 +43,6 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
|
|||
private EntityManager em;
|
||||
|
||||
|
||||
@Override
|
||||
public void save(Visit visit) {
|
||||
if (visit.getId() == null) {
|
||||
this.em.persist(visit);
|
||||
|
@ -54,7 +53,6 @@ public class JpaVisitRepositoryImpl implements VisitRepository {
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Visit> findByPetId(Integer petId) {
|
||||
Query query = this.em.createQuery("SELECT visit FROM Visit v where v.pets.id= :id");
|
||||
|
|
|
@ -31,11 +31,9 @@ import org.springframework.samples.petclinic.repository.OwnerRepository;
|
|||
*/
|
||||
public interface SpringDataOwnerRepository extends OwnerRepository, Repository<Owner, Integer> {
|
||||
|
||||
@Override
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||
@Query("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName%")
|
||||
public Collection<Owner> findByLastName(@Param("lastName") String lastName);
|
||||
|
||||
@Override
|
||||
@Query("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id")
|
||||
public Owner findById(@Param("id") int id);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,6 @@ import org.springframework.samples.petclinic.repository.PetRepository;
|
|||
*/
|
||||
public interface SpringDataPetRepository extends PetRepository, Repository<Pet, Integer> {
|
||||
|
||||
@Override
|
||||
@Query("SELECT ptype FROM PetType ptype ORDER BY ptype.name")
|
||||
List<PetType> findPetTypes() throws DataAccessException;
|
||||
}
|
||||
|
|
|
@ -54,51 +54,43 @@ public class ClinicServiceImpl implements ClinicService {
|
|||
this.visitRepository = visitRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Collection<PetType> findPetTypes() throws DataAccessException {
|
||||
return petRepository.findPetTypes();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Owner findOwnerById(int id) throws DataAccessException {
|
||||
return ownerRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Collection<Owner> findOwnerByLastName(String lastName) throws DataAccessException {
|
||||
return ownerRepository.findByLastName(lastName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveOwner(Owner owner) throws DataAccessException {
|
||||
ownerRepository.save(owner);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void saveVisit(Visit visit) throws DataAccessException {
|
||||
visitRepository.save(visit);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Pet findPetById(int id) throws DataAccessException {
|
||||
return petRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void savePet(Pet pet) throws DataAccessException {
|
||||
petRepository.save(pet);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable(value = "vets")
|
||||
public Collection<Vet> findVets() throws DataAccessException {
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.springframework.web.bind.annotation.InitBinder;
|
|||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
@ -96,13 +97,11 @@ public class OwnerController {
|
|||
// no owners found
|
||||
result.rejectValue("lastName", "notFound", "not found");
|
||||
return "owners/findOwners";
|
||||
}
|
||||
else if (results.size() == 1) {
|
||||
// 1 owner found
|
||||
owner = results.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
}
|
||||
else {
|
||||
} else if (results.size() == 1) {
|
||||
// 1 owner found
|
||||
owner = results.iterator().next();
|
||||
return "redirect:/owners/" + owner.getId();
|
||||
} else {
|
||||
// multiple owners found
|
||||
model.put("selections", results);
|
||||
return "owners/ownersList";
|
||||
|
@ -140,4 +139,9 @@ public class OwnerController {
|
|||
return mav;
|
||||
}
|
||||
|
||||
@RequestMapping("/owners/print")
|
||||
@ResponseBody
|
||||
public String printOwner() {
|
||||
return "json";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,12 +47,10 @@ public class PetTypeFormatter implements Formatter<PetType> {
|
|||
this.clinicService = clinicService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String print(PetType petType, Locale locale) {
|
||||
return petType.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PetType parse(String text, Locale locale) throws ParseException {
|
||||
Collection<PetType> findPetTypes = this.clinicService.findPetTypes();
|
||||
for (PetType type : findPetTypes) {
|
||||
|
|
|
@ -7,35 +7,35 @@
|
|||
#-------------------------------------------------------------------------------
|
||||
# HSQL Settings
|
||||
|
||||
jdbc.driverClassName=org.hsqldb.jdbcDriver
|
||||
jdbc.url=jdbc:hsqldb:mem:petclinic
|
||||
jdbc.username=sa
|
||||
jdbc.password=
|
||||
#jdbc.driverClassName=org.hsqldb.jdbcDriver
|
||||
#jdbc.url=jdbc:hsqldb:mem:petclinic
|
||||
#jdbc.username=sa
|
||||
#jdbc.password=
|
||||
|
||||
# Properties that control the population of schema and data for a new data source
|
||||
jdbc.initLocation=classpath:db/hsqldb/initDB.sql
|
||||
jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql
|
||||
#jdbc.initLocation=classpath:db/hsqldb/initDB.sql
|
||||
#jdbc.dataLocation=classpath:db/hsqldb/populateDB.sql
|
||||
|
||||
# Property that determines which database to use with an AbstractJpaVendorAdapter
|
||||
jpa.database=HSQL
|
||||
#jpa.database=HSQL
|
||||
|
||||
jpa.showSql=true
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
# MySQL Settings
|
||||
|
||||
#jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
#jdbc.url=jdbc:mysql://localhost:3306/petclinic
|
||||
#jdbc.username=root
|
||||
#jdbc.password=
|
||||
jdbc.driverClassName=com.mysql.jdbc.Driver
|
||||
jdbc.url=jdbc:mysql://115.29.227.156:3306/petclinic
|
||||
jdbc.username=root
|
||||
jdbc.password=Yang
|
||||
|
||||
# Properties that control the population of schema and data for a new data source
|
||||
#jdbc.initLocation=classpath:db/mysql/initDB.sql
|
||||
#jdbc.dataLocation=classpath:db/mysql/populateDB.sql
|
||||
jdbc.initLocation=classpath:db/mysql/initDB.sql
|
||||
jdbc.dataLocation=classpath:db/mysql/populateDB.sql
|
||||
|
||||
# Property that determines which Hibernate dialect to use
|
||||
# (only applied with "applicationContext-hibernate.xml")
|
||||
#hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||
hibernate.dialect=org.hibernate.dialect.MySQLDialect
|
||||
|
||||
# Property that determines which database to use with an AbstractJpaVendorAdapter
|
||||
#jpa.database=MYSQL
|
||||
jpa.database=MYSQL
|
||||
|
|
Loading…
Reference in a new issue