Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////ENTITY//////////
- //////////Pokemon//////////
- package com.example.demo.entity;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name = "pokemon")
- public class Pokemon {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- @Column(name = "id")
- private int id;
- @Column(name = "nombre")
- private String nombre;
- @Column(name = "peso")
- private double peso;
- @Column(name = "nivel")
- private int nivel;
- @Column(name = "poder")
- private double poder;
- @ManyToOne(fetch = FetchType.EAGER)
- @JoinColumn(name = "idtipo")
- private Tipo tipo;
- public Pokemon() {
- // TODO Auto-generated constructor stub
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public double getPeso() {
- return peso;
- }
- public void setPeso(double peso) {
- this.peso = peso;
- }
- public Tipo getTipo() {
- return tipo;
- }
- public void setTipo(Tipo tipo) {
- this.tipo = tipo;
- }
- public double getPoder() {
- return poder;
- }
- public void setPoder(double poder) {
- this.poder = poder;
- }
- public int getNivel() {
- return nivel;
- }
- public void setNivel(int nivel) {
- this.nivel = nivel;
- }
- }
- //////////Tipo//////////
- package com.example.demo.entity;
- import java.util.List;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Entity
- @Table(name = "tipo")
- public class Tipo {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- @Column(name = "id")
- private int id;
- @Column(name = "nombre")
- private String nombre;
- @OneToMany(mappedBy = "tipo", fetch = FetchType.LAZY)
- private List<Pokemon> pokemons;
- public Tipo() {
- // TODO Auto-generated constructor stub
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNombre() {
- return nombre;
- }
- public void setNombre(String nombre) {
- this.nombre = nombre;
- }
- public List<Pokemon> getPokemons() {
- return pokemons;
- }
- public void setPokemons(List<Pokemon> pokemons) {
- this.pokemons = pokemons;
- }
- }
- //////////REPOSITORY//////////
- //////////PokemonRepository//////////
- package com.example.demo.repository;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import org.springframework.transaction.annotation.Transactional;
- import com.example.demo.entity.Pokemon;
- @Repository
- @Transactional
- public interface PokemonRepository extends JpaRepository<Pokemon, Integer>{
- }
- //////////TipoRepository//////////
- package com.example.demo.repository;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.stereotype.Repository;
- import org.springframework.transaction.annotation.Transactional;
- import com.example.demo.entity.Tipo;
- @Repository
- @Transactional
- public interface TipoRepository extends JpaRepository<Tipo, Integer> {
- }
- //////////SERVICE//////////
- //////////IPokemonService//////////
- package com.example.demo.service;
- import java.util.List;
- import com.example.demo.entity.Pokemon;
- public interface IPokemonService {
- public Pokemon save(Pokemon pokemon);
- public void delete (int id);
- public List<Pokemon> findByAll();
- }
- //////////ITipoService//////////
- package com.example.demo.service;
- import java.util.List;
- import com.example.demo.entity.Tipo;
- public interface ITipoService {
- public List<Tipo> findByAll();
- }
- //////////PokemonService//////////
- package com.example.demo.service;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.example.demo.entity.Pokemon;
- import com.example.demo.repository.PokemonRepository;
- @Service
- public class PokemonService implements IPokemonService {
- @Autowired
- PokemonRepository pokemonRepository;
- @Override
- public Pokemon save(Pokemon pokemon) {
- // TODO Auto-generated method stub
- int x = pokemon.getNivel();
- double y = pokemon.getPeso();
- double z = (x * y)/3;
- pokemon.setPoder(z);
- return pokemonRepository.save(pokemon);
- }
- @Override
- public void delete(int id) {
- // TODO Auto-generated method stub
- pokemonRepository.delete(id);
- }
- @Override
- public List<Pokemon> findByAll() {
- // TODO Auto-generated method stub
- return pokemonRepository.findAll();
- }
- }
- //////////TipoService//////////
- package com.example.demo.service;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.example.demo.entity.Tipo;
- import com.example.demo.repository.TipoRepository;
- @Service
- public class TipoService implements ITipoService {
- @Autowired
- TipoRepository tipoRepository;
- @Override
- public List<Tipo> findByAll() {
- // TODO Auto-generated method stub
- return tipoRepository.findAll();
- }
- }
- //////////CONTROLLER//////////
- package com.example.demo.controller;
- 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.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import com.example.demo.entity.Pokemon;
- import com.example.demo.service.IPokemonService;
- import com.example.demo.service.ITipoService;
- @Controller
- @RequestMapping("/pokemon")
- public class PokemonController {
- @Autowired
- private ITipoService tipoService;
- @Autowired
- private IPokemonService pokemonService;
- @GetMapping("/listar")
- public String Listar(Model model) {
- model.addAttribute("pokemon", new Pokemon());
- model.addAttribute("tipos", tipoService.findByAll());
- model.addAttribute("pokemons", pokemonService.findByAll());
- return "pokemon";
- }
- @PostMapping("/agregar")
- public String Registrar(Pokemon pokemon) {
- pokemonService.save(pokemon);
- return "redirect:/pokemon/listar";
- }
- @PostMapping("/eliminar")
- public String Eliminar(@RequestParam(name = "id") int id) {
- pokemonService.delete(id);
- return "redirect:/pokemon/listar";
- }
- }
- //////////VISTA//////////
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Final</title>
- </head>
- <body>
- <h1 >PokeDex</h1>
- <h3 >Registrar Pokemon </h3>
- <form method="post" th:object="${pokemon}" th:action="@{/pokemon/agregar}">
- Nombre:
- <input type="text" th:field="*{nombre}" placeholder="ingrese nombre" />
- <br/>
- Nivel:
- <input type="text" th:field="*{nivel}" placeholder="ingrese nivel" />
- <br/>
- Peso:
- <input type="text" th:field="*{peso}" placeholder="ingrese peso" />
- <br/>
- Tipo:
- <select th:field="*{tipo}">
- <option th:each="tipo : ${tipos}"
- th:value="${tipo.id}" th:text="${tipo.nombre}">
- </option>
- </select>
- <br/>
- <button type="submit" >Registrar Pokemon</button>
- </form>
- <!--<p th:text="${resultado}">Resultado.</p>-->
- <h3>Listado de Pokemons</h3>
- <table>
- <thead>
- <tr>
- <td>Codigo</td>
- <td>Nombre</td>
- <td>Nivel</td>
- <td>Peso</td>
- <td>Tipo</td>
- <td>Poder</td>
- <td>Eliminar</td>
- </tr>
- </thead>
- <tr th:each="pokemon : ${pokemons}">
- <td th:text=${pokemon.id}></td>
- <td th:text=${pokemon.nombre}></td>
- <td th:text=${pokemon.nivel}></td>
- <td th:text=${pokemon.peso}></td>
- <td th:text=${pokemon.tipo.nombre}></td>
- <td th:text=${pokemon.poder}></td>
- <td>
- <form action="/pokemon/eliminar" method="post">
- <input type="hidden" name="id" th:value="${pokemon.id}"/>
- <button type="submit" >Eliminar</button>
- </form>
- </td>
- </tr>
- </table>
- </body>
- </html>
- //////////PROPIEDADES//////////
- spring.datasource.driver-class-name = com.mysql.jdbc.Driver
- spring.datasource.url = jdbc:mysql://localhost/basepokemon
- spring.datasource.username = root
- spring.datasource.password = craft458
- spring.thymeleaf.mode=LEGACYHTML5
- spring.thymeleaf.cache=false
- //////////DEPENDENCIAS//////////
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>net.sourceforge.nekohtml</groupId>
- <artifactId>nekohtml</artifactId>
- <version>1.9.21</version>
- </dependency>
- </dependencies>
Add Comment
Please, Sign In to add comment