/////Lo que te puedes olvidar
spring.datasource.url=jdbc:mysql://localhost:3306/dbexamen
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Repository
@Transactional
public interface ArticuloRepository extends CrudRepository<Articulo, Integer>
@RequestMapping(value="/articulo/new",method=RequestMethod.POST) //HOME
public String saveArticulo(@Valid Articulo a,BindingResult result,Model model){
try {
if(result.hasErrors()){
model.addAttribute("message",result.toString());
return "newArticulo";
}
precioventa=articuloService.calcularPrecioVenta(a);
articuloService.saveArticulo(a);
model.addAttribute("message",
"El precio de venta ="+precioventa);
return "newArticulo";
} catch (Exception e) {
// TODO: handle exception
model.addAttribute("message",
e.getMessage());
return "newArticulo";
}
}
////ejem de querys
List<Articulo> findByClavearticuloOrderByPrecioventaDesc(int clavearticulo);
@Query("select u from Articulo u where u.precioventa between ?1 and ?2 ")
List<Articulo> findByPrecioventaRange(double pv1, double pv2);
@Query("select count(u) from Articulo u where u.clavearticulo=?1")
int countArticuloClavearticulo(int clavearticulo);
////Repository
package com.example.repository;
import java.util.List;
//import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.*;
import org.springframework.transaction.annotation.Transactional;
import com.example.entities.Articulo;
@Repository
@Transactional
public interface ArticuloRepository extends CrudRepository<Articulo,Integer> {
List<Articulo> findByClavearticuloOrderByPrecioventaDesc(int clavearticulo);
@Query("select count(u) from Articulo u where u.clavearticulo=?1")
int countArticuloClavearticulo(int clavearticulo);
@Query("select u from Articulo u where u.precioventa between ?1 and ?2")
List<Articulo> findByPrecioVenta(double pv1, double pv2);
}
/////Service////1.-Irterface
package com.example.service;
import com.example.entities.Articulo;
public interface ArticuloService {
Articulo saveArticulo(Articulo a);
Iterable<Articulo> listAllArticulos();
Iterable<Articulo> findClavearticuloOrderByPrecioventaDesc(int clavearticulo);
Iterable<Articulo> findByPrecioventaRange(double pv1, double pv2);
int countArticuloClavearticulo(int clavearticulo);
double calcularPrecioVenta(Articulo a);
}
//clase service
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entities.Articulo;
import com.example.repository.ArticuloRepository;
@Service
public class ArticuloServiceImple implements ArticuloService {
@Autowired
private ArticuloRepository articuloRepository;
@Override
public Articulo saveArticulo(Articulo a) {
// TODO Auto-generated method stub
return articuloRepository.save(a);
}
@Override
public Iterable<Articulo> listAllArticulos() {
// TODO Auto-generated method stub
return articuloRepository.findAll();
}
@Override
public Iterable<Articulo> findClavearticuloOrderByPrecioventaDesc(int clavearticulo) {
// TODO Auto-generated method stub
return articuloRepository.findByClavearticuloOrderByPrecioventaDesc(clavearticulo);
}
@Override
public Iterable<Articulo> findByPrecioventaRange(double pv1, double pv2) {
// TODO Auto-generated method stub
return articuloRepository.findByPrecioVenta(pv1, pv2);
}
@Override
public int countArticuloClavearticulo(int clavearticulo) {
// TODO Auto-generated method stub
return articuloRepository.countArticuloClavearticulo(clavearticulo);
}
@Override
public double calcularPrecioVenta(Articulo a) {
// TODO Auto-generated method stub
return 0;
}
}
////Controller
package com.example.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.entities.Articulo;
import com.example.service.ArticuloService;
@Controller
public class ArticuloController {
@Autowired
private ArticuloService articuloService;
@RequestMapping(value="/") //HOME
public String articulos(Model model)
{
model.addAttribute("articulo", new Articulo());
return "newArticulo"; //la vistaaaaa ctmare
}
@RequestMapping(value="/articulo/new",method=RequestMethod.POST) // home
public String saveArticulo(@Valid Articulo a, BindingResult result, Model model)
{
articuloService.saveArticulo(a);
return "newArticulo";
}
@RequestMapping(value="/articulos") // HOME
public String listararticulos(Model model)
{
model.addAttribute("articulos", articuloService.listAllArticulos());
return "articulos";//vista html
}
@RequestMapping(value="/articulosrange",method=RequestMethod.POST)
public String listarticulosRange(Model model,@RequestParam double pv1,@RequestParam double pv2)
{
model.addAttribute("articulos", articuloService.findByPrecioventaRange(pv1, pv2));
return "articulos";//vista html
}
@RequestMapping(value="/articulosclavearticulo", method=RequestMethod.POST)
public String listarticulosclavearticulo(Model model, @RequestParam int clavearticulo)
{
model.addAttribute("cantiarticulos", articuloService.countArticuloClavearticulo(clavearticulo));
model.addAttribute("articulos", articuloService.findClavearticuloOrderByPrecioventaDesc(clavearticulo));
return "articulos";
}
}
////vistas 1
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
<h2>Calcular Precio Venta</h2>
<div>
<!-- formulario de boostrap esto lo saque de:http://v4-alpha.getbootstrap.com/components/forms/ -->
<form class="form-horizontal" th:object="${articulo}"
th:action="@{/articulo/new}" method="post">
<input type="hidden" th:field="*{id}" />
<div class="form-group">
<label class="col-sm-2 control-label">Nombre Articulo:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{nombre}"
required="required" /> <span
th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}">Nombre
Error </span>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Clave Articulo:</label>
<div class="col-sm-10">
<select class="form-control" th:field="*{clavearticulo}">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="3">4</option>
<option value="3">5</option>
<option value="3">6</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Costo Materia Prima:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{costomp}"
required="required" /> <span
th:if="${#fields.hasErrors('costomp')}" th:errors="*{costomp}">Costomp
Error </span>
</div>
</div>
<!-- Estos botones los saque de :http://v4-alpha.getbootstrap.com/components/buttons/ -->
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-10">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
<div class="form-group">
<p th:text="${message}">1</p>
</div>
</div>
</div>
///vista 2
<body>
<!-- Division container solo tiene una tabla<table> -->
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
<!-- Division para buscar rango precio venta -->
<div class="panel panel-default">
<div class="panel-body">
<form role="form" th:action="@{/articulosrange}" method="post">
<div class="row">
<div class="col-sm-2">
<label for="name">Precio Min:</label>
</div>
<div class="col-sm-2">
<input type="number" step="any" class="form-control" id="pv1"
name="pv1" required="required" />
</div>
<div class="col-sm-2">
<label for="name">Precio Max:</label>
</div>
<div class="col-sm-2">
<input type="number" step="any" class="form-control" id="pv2"
name="pv2" required="required" />
</div>
<div class="col-sm-2">
<input type="submit" value="Buscar" class="btn btn-success" />
</div>
</div>
</form>
</div>
</div>
<h2>Lista Articulos</h2>
<div class="row">
<table class="table table-striped">
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Clave</th>
<th>Costo Materia Prima</th>
<th>Costo Mano Obra</th>
<th>Gasto Fabricacion</th>
<th>Costo Produccion</th>
<th>Precio Venta</th>
</tr>
<tr th:each="a : ${articulos}">
<td th:text="${a.id}">id</td>
<td th:text="${a.nombre}">Nombre</td>
<td th:text="${a.clavearticulo}">clavearticulo</td>
<td th:text="${a.costomp}">costomp</td>
<td th:text="${a.costomo}">costomo</td>
<td th:text="${a.gastofabricacion}">gastofabricacion</td>
<td th:text="${a.costoproduccion}">costoproduccion</td>
<td th:text="${a.precioventa}">precioventa</td>
</tr>
</table>
</div>
</div>
</body>
///vista que corre
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<form th:object="${articulo}" th:action="@{/articulo/new}" method="post">
Nombre:
<input type="text" th:field="*{nombre}" />
<br/>
Clave Articulo:
<select th:field="*{clavearticulo}">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="3">4</option>
<option value="3">5</option>
<option value="3">6</option>
</select>
<br/>
Costo Materia Prima:
<input type="text" th:field="*{costomp}"/>
<br/>
<button type="submit">Save</button>
</form>
<p>1</p>
</body>
</html>