Skip to content

Commit 92a6848

Browse files
committed
Clases de acceso a datos, modelo, servicio y script de BBDD
1 parent 37cfa22 commit 92a6848

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed

scripts/incidencia.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
--
3+
-- Estructura de tabla para la tabla `incidencia`
4+
--
5+
6+
CREATE TABLE `incidencia` (
7+
`id` int(11) NOT NULL,
8+
`titulo` varchar(255) DEFAULT NULL,
9+
`descripcion` varchar(2000) DEFAULT NULL,
10+
`estado` int(20) NOT NULL,
11+
`autor` varchar(50) NOT NULL
12+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
13+
14+
15+
16+
--
17+
-- Indices de la tabla `incidencia`
18+
--
19+
ALTER TABLE `incidencia`
20+
ADD PRIMARY KEY (`id`);
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
package org.tecnificados.boot.dao;
3+
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.tecnificados.boot.model.Incidencia;
7+
8+
9+
/**
10+
* @author Juan Carlos Ballesteros (tecnificados.com)
11+
*
12+
*/
13+
public interface IncidenciaDAO extends CrudRepository<Incidencia, Long> {
14+
15+
@Query(value = "SELECT coalesce(max(id), 0) FROM Incidencia")
16+
Long getMaxTransactionId();
17+
18+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
package org.tecnificados.boot.model;
3+
4+
import javax.persistence.Column;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.GenerationType;
8+
import javax.persistence.Id;
9+
10+
/**
11+
* @author Juan Carlos Ballesteros (tecnificados.com)
12+
*
13+
*/
14+
15+
@Entity
16+
public class Incidencia {
17+
18+
@Id
19+
private Long id;
20+
21+
@Column
22+
private String titulo;
23+
24+
@Column
25+
private String descripcion;
26+
27+
@Column
28+
private Integer estado;
29+
30+
@Column
31+
private String autor;
32+
33+
public Long getId() {
34+
return id;
35+
}
36+
37+
public void setId(Long id) {
38+
this.id = id;
39+
}
40+
41+
public String getTitulo() {
42+
return titulo;
43+
}
44+
45+
public void setTitulo(String titulo) {
46+
this.titulo = titulo;
47+
}
48+
49+
public String getDescripcion() {
50+
return descripcion;
51+
}
52+
53+
public void setDescripcion(String descripcion) {
54+
this.descripcion = descripcion;
55+
}
56+
57+
public Integer getEstado() {
58+
return estado;
59+
}
60+
61+
public void setEstado(Integer estado) {
62+
this.estado = estado;
63+
}
64+
65+
public String getAutor() {
66+
return autor;
67+
}
68+
69+
public void setAutor(String autor) {
70+
this.autor = autor;
71+
}
72+
73+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
package org.tecnificados.boot.service;
3+
4+
import java.io.Serializable;
5+
import java.util.List;
6+
7+
/**
8+
* @author Juan Carlos Ballesteros (tecnificados.com)
9+
*
10+
*/
11+
public interface GenericService<T, ID extends Serializable> {
12+
13+
List<T> getAll();
14+
15+
T save(T entity);
16+
17+
void delete(ID id);
18+
19+
T get(ID id);
20+
21+
Long maxId();
22+
23+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
*
3+
*/
4+
package org.tecnificados.boot.service;
5+
6+
7+
8+
import java.io.Serializable;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
import org.springframework.data.repository.CrudRepository;
14+
import org.springframework.stereotype.Service;
15+
16+
/**
17+
* @author Juan Carlos Ballesteros (tecnificados.com)
18+
*
19+
*/
20+
21+
22+
@Service
23+
public abstract class GenericServiceImpl<T, ID extends Serializable> implements GenericService<T, ID> {
24+
25+
@Override
26+
public T save(T entity) {
27+
return getDao().save(entity);
28+
}
29+
30+
@Override
31+
public void delete(ID id) {
32+
getDao().deleteById(id);
33+
}
34+
35+
@Override
36+
public T get(ID id) {
37+
Optional<T> obj = getDao().findById(id);
38+
if (obj.isPresent()) {
39+
return obj.get();
40+
}
41+
return null;
42+
}
43+
44+
@Override
45+
public List<T> getAll() {
46+
List<T> returnList = new ArrayList<>();
47+
getDao().findAll().forEach(obj -> returnList.add(obj));
48+
return returnList;
49+
}
50+
51+
52+
53+
54+
public abstract CrudRepository<T, ID> getDao();
55+
56+
57+
58+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
package org.tecnificados.boot.service;
4+
5+
import org.tecnificados.boot.model.Incidencia;
6+
7+
/**
8+
* @author Juan Carlos Ballesteros (tecnificados.com)
9+
*
10+
*/
11+
public interface IncidenciaService extends GenericService<Incidencia, Long> {
12+
13+
14+
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
package org.tecnificados.boot.service;
3+
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.repository.CrudRepository;
6+
import org.springframework.stereotype.Service;
7+
import org.tecnificados.boot.dao.IncidenciaDAO;
8+
import org.tecnificados.boot.model.Incidencia;
9+
10+
/**
11+
* @author Juan Carlos Ballesteros (tecnificados.com)
12+
*
13+
*/
14+
15+
@Service
16+
public class IncidenciaServiceImpl extends GenericServiceImpl<Incidencia, Long> implements IncidenciaService {
17+
18+
@Autowired
19+
private IncidenciaDAO incidenciaDAO;
20+
21+
@Override
22+
public CrudRepository<Incidencia, Long> getDao() {
23+
return incidenciaDAO;
24+
}
25+
26+
@Override
27+
public Long maxId() {
28+
return incidenciaDAO.getMaxTransactionId();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)