Kamidake

El Imperio Contraataca

Jul 14th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1. //////////ENTITY//////////
  2. //////////Pokemon//////////
  3. package com.example.demo.entity;
  4.  
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.FetchType;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.GenerationType;
  10. import javax.persistence.Id;
  11. import javax.persistence.JoinColumn;
  12. import javax.persistence.ManyToOne;
  13. import javax.persistence.Table;
  14.  
  15. @Entity
  16. @Table(name = "pokemon")
  17. public class Pokemon {
  18.  
  19. @Id
  20. @GeneratedValue(strategy = GenerationType.AUTO)
  21. @Column(name = "id")
  22. private int id;
  23.  
  24. @Column(name = "nombre")
  25. private String nombre;
  26.  
  27. @Column(name = "peso")
  28. private double peso;
  29.  
  30. @Column(name = "nivel")
  31. private int nivel;
  32.  
  33. @Column(name = "poder")
  34. private double poder;
  35.  
  36. @ManyToOne(fetch = FetchType.EAGER)
  37. @JoinColumn(name = "idtipo")
  38. private Tipo tipo;
  39.  
  40. public Pokemon() {
  41. // TODO Auto-generated constructor stub
  42. }
  43.  
  44. public int getId() {
  45. return id;
  46. }
  47.  
  48. public void setId(int id) {
  49. this.id = id;
  50. }
  51.  
  52. public String getNombre() {
  53. return nombre;
  54. }
  55.  
  56. public void setNombre(String nombre) {
  57. this.nombre = nombre;
  58. }
  59.  
  60. public double getPeso() {
  61. return peso;
  62. }
  63.  
  64. public void setPeso(double peso) {
  65. this.peso = peso;
  66. }
  67.  
  68. public Tipo getTipo() {
  69. return tipo;
  70. }
  71.  
  72. public void setTipo(Tipo tipo) {
  73. this.tipo = tipo;
  74. }
  75.  
  76. public double getPoder() {
  77. return poder;
  78. }
  79.  
  80. public void setPoder(double poder) {
  81. this.poder = poder;
  82. }
  83.  
  84. public int getNivel() {
  85. return nivel;
  86. }
  87.  
  88. public void setNivel(int nivel) {
  89. this.nivel = nivel;
  90. }
  91. }
  92. //////////Tipo//////////
  93. package com.example.demo.entity;
  94.  
  95. import java.util.List;
  96.  
  97. import javax.persistence.Column;
  98. import javax.persistence.Entity;
  99. import javax.persistence.FetchType;
  100. import javax.persistence.GeneratedValue;
  101. import javax.persistence.GenerationType;
  102. import javax.persistence.Id;
  103. import javax.persistence.OneToMany;
  104. import javax.persistence.Table;
  105.  
  106. @Entity
  107. @Table(name = "tipo")
  108. public class Tipo {
  109.  
  110. @Id
  111. @GeneratedValue(strategy = GenerationType.AUTO)
  112. @Column(name = "id")
  113. private int id;
  114.  
  115. @Column(name = "nombre")
  116. private String nombre;
  117.  
  118. @OneToMany(mappedBy = "tipo", fetch = FetchType.LAZY)
  119. private List<Pokemon> pokemons;
  120.  
  121. public Tipo() {
  122. // TODO Auto-generated constructor stub
  123. }
  124.  
  125. public int getId() {
  126. return id;
  127. }
  128.  
  129. public void setId(int id) {
  130. this.id = id;
  131. }
  132.  
  133. public String getNombre() {
  134. return nombre;
  135. }
  136.  
  137. public void setNombre(String nombre) {
  138. this.nombre = nombre;
  139. }
  140.  
  141. public List<Pokemon> getPokemons() {
  142. return pokemons;
  143. }
  144.  
  145. public void setPokemons(List<Pokemon> pokemons) {
  146. this.pokemons = pokemons;
  147. }
  148. }
  149. //////////REPOSITORY//////////
  150. //////////PokemonRepository//////////
  151. package com.example.demo.repository;
  152.  
  153. import org.springframework.data.jpa.repository.JpaRepository;
  154. import org.springframework.stereotype.Repository;
  155. import org.springframework.transaction.annotation.Transactional;
  156.  
  157. import com.example.demo.entity.Pokemon;
  158.  
  159. @Repository
  160. @Transactional
  161. public interface PokemonRepository extends JpaRepository<Pokemon, Integer>{
  162.  
  163. }
  164. //////////TipoRepository//////////
  165. package com.example.demo.repository;
  166.  
  167. import org.springframework.data.jpa.repository.JpaRepository;
  168. import org.springframework.stereotype.Repository;
  169. import org.springframework.transaction.annotation.Transactional;
  170.  
  171. import com.example.demo.entity.Tipo;
  172.  
  173. @Repository
  174. @Transactional
  175. public interface TipoRepository extends JpaRepository<Tipo, Integer> {
  176.  
  177. }
  178. //////////SERVICE//////////
  179. //////////IPokemonService//////////
  180. package com.example.demo.service;
  181.  
  182. import java.util.List;
  183.  
  184. import com.example.demo.entity.Pokemon;
  185.  
  186. public interface IPokemonService {
  187.  
  188. public Pokemon save(Pokemon pokemon);
  189.  
  190. public void delete (int id);
  191.  
  192. public List<Pokemon> findByAll();
  193.  
  194. }
  195. //////////ITipoService//////////
  196. package com.example.demo.service;
  197.  
  198. import java.util.List;
  199.  
  200. import com.example.demo.entity.Tipo;
  201.  
  202. public interface ITipoService {
  203.  
  204. public List<Tipo> findByAll();
  205.  
  206. }
  207. //////////PokemonService//////////
  208. package com.example.demo.service;
  209.  
  210. import java.util.List;
  211.  
  212. import org.springframework.beans.factory.annotation.Autowired;
  213. import org.springframework.stereotype.Service;
  214.  
  215. import com.example.demo.entity.Pokemon;
  216. import com.example.demo.repository.PokemonRepository;
  217.  
  218. @Service
  219. public class PokemonService implements IPokemonService {
  220.  
  221. @Autowired
  222. PokemonRepository pokemonRepository;
  223.  
  224. @Override
  225. public Pokemon save(Pokemon pokemon) {
  226. // TODO Auto-generated method stub
  227. int x = pokemon.getNivel();
  228.  
  229. double y = pokemon.getPeso();
  230.  
  231. double z = (x * y)/3;
  232.  
  233. pokemon.setPoder(z);
  234.  
  235. return pokemonRepository.save(pokemon);
  236. }
  237.  
  238. @Override
  239. public void delete(int id) {
  240. // TODO Auto-generated method stub
  241. pokemonRepository.delete(id);
  242. }
  243.  
  244. @Override
  245. public List<Pokemon> findByAll() {
  246. // TODO Auto-generated method stub
  247. return pokemonRepository.findAll();
  248. }
  249. }
  250. //////////TipoService//////////
  251. package com.example.demo.service;
  252.  
  253. import java.util.List;
  254.  
  255. import org.springframework.beans.factory.annotation.Autowired;
  256. import org.springframework.stereotype.Service;
  257.  
  258. import com.example.demo.entity.Tipo;
  259. import com.example.demo.repository.TipoRepository;
  260.  
  261. @Service
  262. public class TipoService implements ITipoService {
  263.  
  264. @Autowired
  265. TipoRepository tipoRepository;
  266.  
  267. @Override
  268. public List<Tipo> findByAll() {
  269. // TODO Auto-generated method stub
  270. return tipoRepository.findAll();
  271. }
  272.  
  273. }
  274. //////////CONTROLLER//////////
  275. package com.example.demo.controller;
  276.  
  277. import org.springframework.beans.factory.annotation.Autowired;
  278. import org.springframework.stereotype.Controller;
  279. import org.springframework.ui.Model;
  280. import org.springframework.web.bind.annotation.GetMapping;
  281. import org.springframework.web.bind.annotation.PostMapping;
  282. import org.springframework.web.bind.annotation.RequestMapping;
  283. import org.springframework.web.bind.annotation.RequestParam;
  284.  
  285. import com.example.demo.entity.Pokemon;
  286. import com.example.demo.service.IPokemonService;
  287. import com.example.demo.service.ITipoService;
  288.  
  289. @Controller
  290. @RequestMapping("/pokemon")
  291. public class PokemonController {
  292.  
  293. @Autowired
  294. private ITipoService tipoService;
  295.  
  296. @Autowired
  297. private IPokemonService pokemonService;
  298.  
  299. @GetMapping("/listar")
  300. public String Listar(Model model) {
  301.  
  302. model.addAttribute("pokemon", new Pokemon());
  303. model.addAttribute("tipos", tipoService.findByAll());
  304. model.addAttribute("pokemons", pokemonService.findByAll());
  305.  
  306. return "pokemon";
  307. }
  308.  
  309. @PostMapping("/agregar")
  310. public String Registrar(Pokemon pokemon) {
  311.  
  312. pokemonService.save(pokemon);
  313.  
  314. return "redirect:/pokemon/listar";
  315. }
  316.  
  317. @PostMapping("/eliminar")
  318. public String Eliminar(@RequestParam(name = "id") int id) {
  319. pokemonService.delete(id);
  320. return "redirect:/pokemon/listar";
  321. }
  322. }
  323. //////////VISTA//////////
  324. <!DOCTYPE html>
  325. <html lang="en">
  326. <head>
  327. <meta charset="utf-8">
  328. <title>Final</title>
  329. </head>
  330. <body>
  331. <h1 >PokeDex</h1>
  332.  
  333. <h3 >Registrar Pokemon </h3>
  334. <form method="post" th:object="${pokemon}" th:action="@{/pokemon/agregar}">
  335.  
  336. Nombre:
  337. <input type="text" th:field="*{nombre}" placeholder="ingrese nombre" />
  338. <br/>
  339.  
  340. Nivel:
  341. <input type="text" th:field="*{nivel}" placeholder="ingrese nivel" />
  342. <br/>
  343.  
  344. Peso:
  345. <input type="text" th:field="*{peso}" placeholder="ingrese peso" />
  346. <br/>
  347.  
  348. Tipo:
  349. <select th:field="*{tipo}">
  350. <option th:each="tipo : ${tipos}"
  351. th:value="${tipo.id}" th:text="${tipo.nombre}">
  352. </option>
  353. </select>
  354. <br/>
  355.  
  356. <button type="submit" >Registrar Pokemon</button>
  357. </form>
  358.  
  359. <!--<p th:text="${resultado}">Resultado.</p>-->
  360.  
  361. <h3>Listado de Pokemons</h3>
  362.  
  363. <table>
  364. <thead>
  365. <tr>
  366. <td>Codigo</td>
  367. <td>Nombre</td>
  368. <td>Nivel</td>
  369. <td>Peso</td>
  370. <td>Tipo</td>
  371. <td>Poder</td>
  372. <td>Eliminar</td>
  373. </tr>
  374. </thead>
  375.  
  376. <tr th:each="pokemon : ${pokemons}">
  377. <td th:text=${pokemon.id}></td>
  378. <td th:text=${pokemon.nombre}></td>
  379. <td th:text=${pokemon.nivel}></td>
  380. <td th:text=${pokemon.peso}></td>
  381. <td th:text=${pokemon.tipo.nombre}></td>
  382. <td th:text=${pokemon.poder}></td>
  383.  
  384. <td>
  385. <form action="/pokemon/eliminar" method="post">
  386. <input type="hidden" name="id" th:value="${pokemon.id}"/>
  387. <button type="submit" >Eliminar</button>
  388. </form>
  389. </td>
  390.  
  391. </tr>
  392. </table>
  393. </body>
  394. </html>
  395. //////////PROPIEDADES//////////
  396. spring.datasource.driver-class-name = com.mysql.jdbc.Driver
  397. spring.datasource.url = jdbc:mysql://localhost/basepokemon
  398. spring.datasource.username = root
  399. spring.datasource.password = craft458
  400.  
  401. spring.thymeleaf.mode=LEGACYHTML5
  402. spring.thymeleaf.cache=false
  403. //////////DEPENDENCIAS//////////
  404. <dependencies>
  405. <dependency>
  406. <groupId>org.springframework.boot</groupId>
  407. <artifactId>spring-boot-starter-data-jpa</artifactId>
  408. </dependency>
  409. <dependency>
  410. <groupId>org.springframework.boot</groupId>
  411. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  412. </dependency>
  413. <dependency>
  414. <groupId>org.springframework.boot</groupId>
  415. <artifactId>spring-boot-starter-web</artifactId>
  416. </dependency>
  417.  
  418. <dependency>
  419. <groupId>org.springframework.boot</groupId>
  420. <artifactId>spring-boot-devtools</artifactId>
  421. <scope>runtime</scope>
  422. </dependency>
  423. <dependency>
  424. <groupId>mysql</groupId>
  425. <artifactId>mysql-connector-java</artifactId>
  426. <scope>runtime</scope>
  427. </dependency>
  428. <dependency>
  429. <groupId>org.springframework.boot</groupId>
  430. <artifactId>spring-boot-starter-test</artifactId>
  431. <scope>test</scope>
  432. </dependency>
  433. <dependency>
  434. <groupId>net.sourceforge.nekohtml</groupId>
  435. <artifactId>nekohtml</artifactId>
  436. <version>1.9.21</version>
  437. </dependency>
  438. </dependencies>
Add Comment
Please, Sign In to add comment