mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 21:15:50 +00:00
Improve findById() in JdbcVetRepository and format code
This commit is contained in:
parent
3e41bdf347
commit
50da6d65fb
1 changed files with 75 additions and 30 deletions
|
@ -1,6 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2016 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.repository.jdbc;
|
package org.springframework.samples.petclinic.repository.jdbc;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
@ -15,61 +36,85 @@ 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.Owner;
|
||||||
|
import org.springframework.samples.petclinic.model.PetType;
|
||||||
|
import org.springframework.samples.petclinic.model.Specialty;
|
||||||
import org.springframework.samples.petclinic.model.Vet;
|
import org.springframework.samples.petclinic.model.Vet;
|
||||||
import org.springframework.samples.petclinic.repository.VetRepositoryExt;
|
import org.springframework.samples.petclinic.repository.VetRepositoryExt;
|
||||||
|
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vitaliy Fedoriv
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
@Qualifier("VetRepositoryExt")
|
@Qualifier("VetRepositoryExt")
|
||||||
public class JdbcVetRepositoryExtImpl extends JdbcVetRepositoryImpl implements VetRepositoryExt {
|
public class JdbcVetRepositoryExtImpl extends JdbcVetRepositoryImpl implements VetRepositoryExt {
|
||||||
|
|
||||||
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
|
||||||
|
|
||||||
private SimpleJdbcInsert insertVet;
|
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
|
||||||
|
|
||||||
|
private SimpleJdbcInsert insertVet;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public JdbcVetRepositoryExtImpl(DataSource dataSource, JdbcTemplate jdbcTemplate) {
|
public JdbcVetRepositoryExtImpl(DataSource dataSource, JdbcTemplate jdbcTemplate) {
|
||||||
super(jdbcTemplate);
|
super(jdbcTemplate);
|
||||||
this.insertVet = new SimpleJdbcInsert(dataSource)
|
this.insertVet = new SimpleJdbcInsert(dataSource).withTableName("vets").usingGeneratedKeyColumns("id");
|
||||||
.withTableName("vets")
|
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||||
.usingGeneratedKeyColumns("id");
|
|
||||||
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Vet findById(int id) throws DataAccessException {
|
public Vet findById(int id) throws DataAccessException {
|
||||||
Vet vet;
|
Vet vet;
|
||||||
try {
|
try {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> vet_params = new HashMap<>();
|
||||||
params.put("id", id);
|
vet_params.put("id", id);
|
||||||
vet = this.namedParameterJdbcTemplate.queryForObject(
|
vet = this.namedParameterJdbcTemplate.queryForObject(
|
||||||
"SELECT id, first_name, last_name FROM vets WHERE id= :id",
|
"SELECT id, first_name, last_name FROM vets WHERE id= :id",
|
||||||
params,
|
vet_params,
|
||||||
BeanPropertyRowMapper.newInstance(Vet.class));
|
BeanPropertyRowMapper.newInstance(Vet.class));
|
||||||
} catch (EmptyResultDataAccessException ex) {
|
|
||||||
throw new ObjectRetrievalFailureException(Vet.class, id);
|
final List<Specialty> specialties = this.namedParameterJdbcTemplate.query(
|
||||||
}
|
"SELECT id, name FROM specialties", vet_params, BeanPropertyRowMapper.newInstance(Specialty.class));
|
||||||
return vet;
|
|
||||||
|
final List<Integer> vetSpecialtiesIds = this.namedParameterJdbcTemplate.query(
|
||||||
|
"SELECT specialty_id FROM vet_specialties WHERE vet_id=:id",
|
||||||
|
vet_params,
|
||||||
|
new BeanPropertyRowMapper<Integer>() {
|
||||||
|
@Override
|
||||||
|
public Integer mapRow(ResultSet rs, int row) throws SQLException {
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (int specialtyId : vetSpecialtiesIds) {
|
||||||
|
Specialty specialty = EntityUtils.getById(specialties, Specialty.class, specialtyId);
|
||||||
|
vet.addSpecialty(specialty);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (EmptyResultDataAccessException ex) {
|
||||||
|
throw new ObjectRetrievalFailureException(Vet.class, id);
|
||||||
|
}
|
||||||
|
return vet;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save(Vet vet) throws DataAccessException {
|
public void save(Vet vet) throws DataAccessException {
|
||||||
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(vet);
|
BeanPropertySqlParameterSource parameterSource = new BeanPropertySqlParameterSource(vet);
|
||||||
if (vet.isNew()) {
|
if (vet.isNew()) {
|
||||||
Number newKey = this.insertVet.executeAndReturnKey(parameterSource);
|
Number newKey = this.insertVet.executeAndReturnKey(parameterSource);
|
||||||
vet.setId(newKey.intValue());
|
vet.setId(newKey.intValue());
|
||||||
} else {
|
} else {
|
||||||
this.namedParameterJdbcTemplate.update(
|
this.namedParameterJdbcTemplate
|
||||||
"UPDATE vets SET first_name=:firstName, last_name=:lastName WHERE id=:id",
|
.update("UPDATE vets SET first_name=:firstName, last_name=:lastName WHERE id=:id", parameterSource);
|
||||||
parameterSource);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(Vet vet) throws DataAccessException {
|
public void delete(Vet vet) throws DataAccessException {
|
||||||
Map<String, Object> params = new HashMap<>();
|
Map<String, Object> params = new HashMap<>();
|
||||||
params.put("id", vet.getId());
|
params.put("id", vet.getId());
|
||||||
this.namedParameterJdbcTemplate.update("DELETE FROM vets WHERE id=:id", params);
|
this.namedParameterJdbcTemplate.update("DELETE FROM vets WHERE id=:id", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue