return namedParamJdbcTemplate.query("SELECT " + " id, name, countrycode country_code, district, population " + " FROM city WHERE countrycode = :code" + " ORDER BY Population DESC" + ((pageNo != null) ? " LIMIT :offset , :size " : ""), params, new CityRowMapper()); }
We are using a paginated query to get a list of cities for a country. We will also need another overloaded version of this method where we return all the cities of a country and we will use this query to fetch all the cities while editing the country to select its capital. The overloaded version is as follows:
public List<City> getCities(String countryCode){ return getCities(countryCode, null); }
Next is to implement the method to get the city details, as shown in the following code:
public City getCityDetail(Long cityId) { Map<String, Object> params = new HashMap<String, Object>(); params.put("id", cityId); return namedParamJdbcTemplate.queryForObject("SELECT id, " + " name, countrycode country_code, " + " district, population " + " FROM city WHERE id = :id", params, new CityRowMapper()); }
Then we implement the method to add a city as follows:
public Long addCity(String countryCode, City city) {
An important thing to notice inaddCityis the use ofKeyHolderandGeneratedKeyHolderto return the generated (due to auto increment) primary key that is thecityId, as follows:
KeyHolder keyHolder = new GeneratedKeyHolder(); //other code return keyHolder.getKey().longValue();
And finally, we implement the method to delete a city from the country as shown in the following code:
public void deleteCity(Long cityId) { Map<String, Object> params = new HashMap<String, Object>(); params.put("id", cityId); namedParamJdbcTemplate.update("DELETE FROM city WHERE id = :id", params); }
Now let's add a test for CityDAO. Add the CityDAOTest class in com.nilangpatel.worldgdp.test.daopackage under src/test/java folder as follows:
@RunWith(SpringRunner.class) @SpringJUnitConfig( classes = { TestDBConfiguration.class, CityDAO.class}) public class CityDAOTest {
@Test public void testGetCityDetail() { Long cityId = 1024l; City city = cityDao.getCityDetail(cityId); assertThat(city.toString()).isEqualTo("City(id=1024, name=Mumbai (Bombay), " + "countryCode=IND, country=null, district=Maharashtra, population=10500000)"); }
@Test public void testAddCity() { String countryCode = "IND"; City city = new City(); city.setCountryCode(countryCode); city.setDistrict("District"); city.setName("City Name"); city.setPopulation(101010l);
long cityId = cityDao.addCity(countryCode, city); assertThat(cityId).isNotNull(); City cityFromDb = cityDao.getCityDetail(cityId); assertThat(cityFromDb).isNotNull(); assertThat(cityFromDb.getName()).isEqualTo("City Name"); }
@Test (expected = EmptyResultDataAccessException.class) public void testDeleteCity() { Long cityId = addCity(); cityDao.deleteCity(cityId); City cityFromDb = cityDao.getCityDetail(cityId); assertThat(cityFromDb).isNull(); }
private Long addCity() { String countryCode = "IND"; City city = new City(); city.setCountryCode(countryCode); city.setDistrict("District"); city.setName("City Name"); city.setPopulation(101010l);