Commit 649118c2 by Francisco Javier

freemarket con h2

parent 73c49d74
package com.example.FreeMarket;
import com.example.FreeMarket.model.Employee;
import com.example.FreeMarket.model.Sala;
import com.example.FreeMarket.repository.EmployeeRepository;
import com.example.FreeMarket.repository.SalaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
......@@ -15,9 +13,6 @@ import java.util.List;
public class FreeMarketApplication implements CommandLineRunner {
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private SalaRepository salaRepository;
public static void main(String[] args) {
......@@ -27,19 +22,5 @@ public class FreeMarketApplication implements CommandLineRunner {
@Override
public void run(String...args) throws Exception {
employeeRepository.save(new Employee("Ramesh", "Fadatare", "ramesh@gmail.com"));
employeeRepository.save(new Employee("Tom", "Cruise", "tom@gmail.com"));
employeeRepository.save(new Employee("John", "Cena", "john@gmail.com"));
employeeRepository.save(new Employee("tony", "stark", "stark@gmail.com"));
// get list of employees
List< Employee > employees = employeeRepository.findAll();
employees.forEach(employee -> System.out.println(employee.toString()));
salaRepository.save(new Sala("6 mesas","1 planta","3m cuadrados"));
salaRepository.save(new Sala("2 mesas","2 planta","6m cuadrados"));
salaRepository.save(new Sala("3 mesas","3 planta","9m cuadrados"));
List<Sala> salas = salaRepository.findAll();
salas.forEach(sala -> System.out.println(sala.toString()));
}
}
package com.example.FreeMarket.controller;
import com.example.FreeMarket.model.Employee;
import com.example.FreeMarket.repository.EmployeeRepository;
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 org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
/*@GetMapping("/")
public String index(Model model) {
return "index";
}*/
@GetMapping("/showEmployees")
public ModelAndView showCities() {
List<Employee> employees = employeeRepository.findAll();
Map< String, Object > params = new HashMap< String, Object >();
params.put("employees", employees);
return new ModelAndView("showEmployees", params);
}
}
package com.example.FreeMarket.controller;
import com.example.FreeMarket.model.Employee;
import com.example.FreeMarket.model.Sala;
import com.example.FreeMarket.repository.SalaRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -15,7 +13,6 @@ import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
@Controller
public class SalasController {
......
package com.example.FreeMarket.model;
import jakarta.persistence.*;
@Entity
@Table(name = "employees")
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "email_address", nullable = false)
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId +
"]";
}
}
package com.example.FreeMarket.repository;
import com.example.FreeMarket.model.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
spring.freemarker.template-loader-path: classpath:/templates
spring.freemarker.suffix: .ftl
spring.datasource.url=jdbc:h2:~/h2db/dawlibros;MODE=LEGACY;AUTO_SERVER=TRUE
spring.datasource.username=dawuser
spring.datasource.password=dawuser
\ No newline at end of file
......@@ -15,11 +15,9 @@
<div class="panel-heading">
<h2>Home Page</h2>
</div>
<a href="showEmployees">Show Employees (Retrieve employee data from database)</a>
<a href="showSalas">Ver Salas (Recuperar datos de salas de la base de datos.)</a>
<br/>
<a href="showSalas">Show Salas (Retrieve employee data from database)</a>
<br/>
<a href="home">home (Retrieve employee data from database)</a>
<a href="home">home </a>
</div>
</div>
</body>
......
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Employee List</h2>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<#list employees as employee>
<tr>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
<td>${employee.emailId}</td>
</tr>
</#list>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
package com.example.FreeMarket;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class FreeMarketApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment