cities database demo

This commit is contained in:
Greg Gauthier 2020-11-19 08:43:58 +00:00
parent 840a4bad54
commit e9a1670133
10 changed files with 218 additions and 6 deletions

View File

@ -18,12 +18,18 @@ test {
dependencies {
implementation group: 'org.hibernate', name: 'hibernate-validator', version: '6.1.6.Final'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.4.0'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.4.0'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker', version: '2.4.0'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.4.0'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.7.0'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.7.0'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-7'
//implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2'
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2'
@ -31,10 +37,10 @@ dependencies {
testImplementation 'io.cucumber:cucumber-junit:6.7.0'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
//java {
// sourceCompatibility = JavaVersion.VERSION_1_8
// targetCompatibility = JavaVersion.VERSION_1_8
//}
configurations {
cucumberRuntime {

View File

@ -0,0 +1,12 @@
package com.gmgauthier.dbdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,24 @@
package com.gmgauthier.dbdemo.controller;
import com.gmgauthier.dbdemo.model.City;
import com.gmgauthier.dbdemo.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class CityController {
@Autowired
private ICityService cityService;
@GetMapping("/cities")
public String findCities(Model model) {
var cities = (List<City>) cityService.findAll(); // what in the world is this?
model.addAttribute("cities", cities);
return "showCities";
}
}

View File

@ -0,0 +1,89 @@
package com.gmgauthier.dbdemo.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "cities")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private int population;
public City() {
}
public City(Long id, String name, int population) {
this.id = id;
this.name = name;
this.population = population;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + Objects.hashCode(this.id);
hash = 79 * hash + Objects.hashCode(this.name);
hash = 79 * hash + this.population;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final City other = (City) obj;
if (this.population != other.population) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return Objects.equals(this.id, other.id);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("City{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", population=").append(population);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,10 @@
package com.gmgauthier.dbdemo.repository;
import com.gmgauthier.dbdemo.model.City;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CityRepository extends CrudRepository<City, Long> {
}

View File

@ -0,0 +1,20 @@
package com.gmgauthier.dbdemo.service;
import com.gmgauthier.dbdemo.model.City;
import com.gmgauthier.dbdemo.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CityService implements ICityService {
@Autowired
private CityRepository repository;
@Override
public List<City> findAll() {
return (List<City>) repository.findAll();
}
}

View File

@ -0,0 +1,9 @@
package com.gmgauthier.dbdemo.service;
import com.gmgauthier.dbdemo.model.City;
import java.util.List;
public interface ICityService {
List<City> findAll();
}

View File

@ -7,4 +7,8 @@ logging.level.org.hibernate=ERROR
demo.resource-url=http://duckduckgo.com
demo.resourcePort[0]=80
demo.resourcePort[1]=443
demo.resourceCount=10
demo.resourceCount=10
spring.datasource.url=jdbc:mysql://192.168.0.131:3306/testdb?useSSL=false&serverTimezone=UTC
spring.datasource.username=testdb
spring.datasource.password=testdb

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<a href="cities">Show cities</a>
</body>
</html>

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>Cities</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>List of cities</h2>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Population</th>
</tr>
<#list cities as city>
<tr>
<td>${city.id}</td>
<td>${city.name}</td>
<td>${city.population}</td>
</tr>
</#list>
</table>
</body>
</html>