├── pom.xml └── src ├── main ├── java │ └── com │ │ └── example │ │ ├── DemoApplication.java │ │ ├── controller │ │ ├── EmployeeController.java │ │ └── EmployeeRestController.java │ │ ├── model │ │ └── Employee.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ └── service │ │ ├── EmployeeService.java │ │ └── EmployeeServiceImpl.java └── resources │ ├── application.properties │ ├── data.sql │ ├── static │ └── js │ │ └── datatable.js │ └── templates │ └── index.html └── test └── java └── com └── example └── DemoApplicationTests.java /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | demo 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | demo 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-thymeleaf 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web 39 | 40 | 41 | 42 | com.h2database 43 | h2 44 | runtime 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | net.sourceforge.nekohtml 55 | nekohtml 56 | 1.9.21 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/com/example/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.example.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | 7 | @Controller 8 | public class EmployeeController { 9 | 10 | @RequestMapping(path="/", method=RequestMethod.GET) 11 | public String goHome(){ 12 | return "index"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/example/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.example.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.example.model.Employee; 12 | import com.example.service.EmployeeService; 13 | 14 | @RestController 15 | public class EmployeeRestController { 16 | 17 | @Autowired 18 | private EmployeeService employeeService; 19 | 20 | @RequestMapping(path="/employees", method=RequestMethod.GET) 21 | public List getAllEmployees(){ 22 | return employeeService.getAllEmployees(); 23 | } 24 | @RequestMapping(value = "/employee/{id}", method = RequestMethod.GET) 25 | public Employee getEmployeeById(@PathVariable("id") long id){ 26 | return employeeService.getEmployeeById(id); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/example/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.example.model; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.Id; 7 | 8 | @Entity 9 | public class Employee { 10 | 11 | @Id 12 | @GeneratedValue 13 | @Column(name="id") 14 | private long id; 15 | @Column(name="name") 16 | private String name; 17 | @Column(name="last_name") 18 | private String lastName; 19 | @Column(name="email") 20 | private String email; 21 | @Column(name="phone") 22 | private String phone; 23 | @Column(name="active") 24 | private boolean active; 25 | 26 | public Employee() { 27 | super(); 28 | } 29 | 30 | public Employee(String name, String lastName, String email, String phone, boolean active) { 31 | super(); 32 | this.name = name; 33 | this.lastName = lastName; 34 | this.email = email; 35 | this.phone = phone; 36 | this.active = active; 37 | } 38 | 39 | public long getId() { 40 | return id; 41 | } 42 | public void setId(long id) { 43 | this.id = id; 44 | } 45 | public String getName() { 46 | return name; 47 | } 48 | public void setName(String name) { 49 | this.name = name; 50 | } 51 | public String getLastName() { 52 | return lastName; 53 | } 54 | public void setLastName(String lastName) { 55 | this.lastName = lastName; 56 | } 57 | public String getEmail() { 58 | return email; 59 | } 60 | public void setEmail(String email) { 61 | this.email = email; 62 | } 63 | public String getPhone() { 64 | return phone; 65 | } 66 | public void setPhone(String phone) { 67 | this.phone = phone; 68 | } 69 | public boolean isActive() { 70 | return active; 71 | } 72 | public void setActive(boolean active) { 73 | this.active = active; 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/example/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.example.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.example.model.Employee; 7 | 8 | @Repository("employeeRepository") 9 | public interface EmployeeRepository extends JpaRepository{ 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import com.example.model.Employee; 6 | 7 | public interface EmployeeService { 8 | 9 | public List getAllEmployees(); 10 | public Employee getEmployeeById(long id); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/example/service/EmployeeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.example.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.example.model.Employee; 9 | import com.example.repository.EmployeeRepository; 10 | 11 | @Service("employeeService") 12 | public class EmployeeServiceImpl implements EmployeeService { 13 | 14 | @Autowired 15 | private EmployeeRepository employeeRepository; 16 | 17 | @Override 18 | public List getAllEmployees() { 19 | return employeeRepository.findAll(); 20 | } 21 | 22 | @Override 23 | public Employee getEmployeeById(long id) { 24 | return employeeRepository.findOne(id); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # =============================== 2 | # = Thymeleaf configurations 3 | # =============================== 4 | spring.thymeleaf.mode=LEGACYHTML5 5 | spring.thymeleaf.cache=false 6 | 7 | # =============================== 8 | # = data.sql file will be executed? 9 | # =============================== 10 | spring.datasource.initialize=true 11 | -------------------------------------------------------------------------------- /src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO employee(name, last_name, email, phone, active) values ('Gustavo','Ponce','test@test.com','1234567890',true); 2 | INSERT INTO employee(name, last_name, email, phone, active) values ('Bob','Marley','one@love.com','6483748590',false); 3 | INSERT INTO employee(name, last_name, email, phone, active) values ('David','Gilmour','high@hopes.com','7648909831',true); 4 | INSERT INTO employee(name, last_name, email, phone, active) values ('John','Lennon','standby@me.com','7689485620',true); 5 | INSERT INTO employee(name, last_name, email, phone, active) values ('Ozzy','Osbourne','children@grave.com','6483748590',false); 6 | INSERT INTO employee(name, last_name, email, phone, active) values ('Jimmy','Page','stairway@heaven.com','7648909831',true); 7 | INSERT INTO employee(name, last_name, email, phone, active) values ('Jimi','Hendrix','purple@haze.com','8754091236',false); 8 | INSERT INTO employee(name, last_name, email, phone, active) values ('Sex','Pistols','save@queen.com','6729098761',true); 9 | INSERT INTO employee(name, last_name, email, phone, active) values ('Jim','Morrison','riders@storm.com','8754091236',false); 10 | INSERT INTO employee(name, last_name, email, phone, active) values ('Richard','Blackmore','highway@star.com','8754091236',true); 11 | INSERT INTO employee(name, last_name, email, phone, active) values ('Jay','Kay','cosmic@girl.com','0926389871',true); 12 | INSERT INTO employee(name, last_name, email, phone, active) values ('David','Bowie','heroes@oneday.com','4338490981',true); 13 | INSERT INTO employee(name, last_name, email, phone, active) values ('Bob','Dylan','knocking@doors.com','4338490981',false); 14 | INSERT INTO employee(name, last_name, email, phone, active) values ('Manu','Chao','mala@vida.com','8923098753',true); 15 | INSERT INTO employee(name, last_name, email, phone, active) values ('The','Specials','ghost@thown.com','7590498573',true); 16 | INSERT INTO employee(name, last_name, email, phone, active) values ('Jymmy','Cliff','see@clearly.com','4338490981',false); 17 | INSERT INTO employee(name, last_name, email, phone, active) values ('The','Temptations','my@girl.com','7639864096',true); 18 | INSERT INTO employee(name, last_name, email, phone, active) values ('Simon','Garfunkel','mr@robinson.com','8750987531',true); 19 | INSERT INTO employee(name, last_name, email, phone, active) values ('catch','22','takes@sometime.com','7098653427',true); 20 | INSERT INTO employee(name, last_name, email, phone, active) values ('Janis','Joplin','cry@baby.com','6739087641',false); 21 | INSERT INTO employee(name, last_name, email, phone, active) values ('Lou','Red','wild@side.com','6789045678',true); 22 | INSERT INTO employee(name, last_name, email, phone, active) values ('Iggy','Pop','the@passenger.com','6934980751',true); 23 | INSERT INTO employee(name, last_name, email, phone, active) values ('Dead','Kennedys','holiday@cambodia.com','2389096457',false); 24 | INSERT INTO employee(name, last_name, email, phone, active) values ('The','Cure','dont@cry.com','8749340987',false); 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/resources/static/js/datatable.js: -------------------------------------------------------------------------------- 1 | $(document).ready( function () { 2 | var table = $('#employeesTable').DataTable({ 3 | "sAjaxSource": "/employees", 4 | "sAjaxDataProp": "", 5 | "order": [[ 0, "asc" ]], 6 | "aoColumns": [ 7 | { "mData": "id"}, 8 | { "mData": "name" }, 9 | { "mData": "lastName" }, 10 | { "mData": "email" }, 11 | { "mData": "phone" }, 12 | { "mData": "active" } 13 | ] 14 | }) 15 | }); -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Spring Boot + JPA + Datatables 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |

Employees Table

17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 |
IdNameLast NameEmailPhoneActive
IdNameLast NameEmailPhoneActive
42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/java/com/example/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------