Last active
June 1, 2022 12:56
-
-
Save DavidBernalGonzalez/38f252655f2eb0f5750aada91856c0ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.login.controllers.entities; | |
import java.io.Serializable; | |
public class User implements Serializable{ | |
private static final long serialVersionUID = 1L; | |
//Atributes | |
private Long id; | |
private String name; | |
private String surname; | |
//Constructors | |
public User() {} | |
public User(Long id, String name, String surname) { | |
this.id = id; | |
this.name = name; | |
this.surname = surname; | |
} | |
// Getters & Setters | |
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 String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.login.controllers; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
import com.login.controllers.entities.User; | |
@RestController | |
@RequestMapping("/api") | |
public class UsersController { | |
/* | |
* Injectamos el JdbcTemplate mediante a la anotación Autowired (mediante a la | |
* inyección de dependencias) | |
*/ | |
@Autowired private | |
JdbcTemplate jdbcTemplate; | |
@RequestMapping(value = "/users", method = RequestMethod.GET) | |
public List<User> getAllUsers() { | |
// La query que ejecutaremos/lanzaremos jdbcTemplate | |
final String QUERY1 = "SELECT * FROM USERS;"; | |
// Guardamos el resultado de la query | |
List<Map<String, Object>> results = jdbcTemplate.queryForList(QUERY1); | |
// Creamos el userList en el que guardaremos los usuarios | |
List<User> userList = new ArrayList<User>(); | |
// Recorremos los resultados | |
for (Map<String, Object> row : results) { | |
// Creramos el usuario | |
User user = new User(); | |
// Setteamos el campo ID | |
user.setId(((Integer) row.get("ID")).longValue()); | |
// Setteamos el campo NAME | |
user.setName((String) row.get("NOMBRE")); | |
// Setteamos el campo SURNAME | |
user.setSurname((String) row.get("APELLIDOS")); | |
// Una vez guardados los valores, añadimos el elemento a la lista de Users | |
userList.add(user); | |
} | |
return userList;// Devolvemos el listado | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment