Spring Boot Thymeleaf Upload and Play Video Files Using Mysql
Project Construction
Here is the projection structure.
Maven Dependencies pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-case" xsi:schemaLocation= "http://maven.apache.org/POM/iv.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.kicking</groupId> <artifactId>leap-boot-starter-parent</artifactId> <version>two.3.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.springboot.tutorial</groupId> <artifactId>springboot-helloworld-tutorial</artifactId> <version>0.0.i-SNAPSHOT</version> <proper noun>CreateEmployee</name> <description>Demo projection for Bound Kicking</description> <properties> <java.version>1.viii</coffee.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.kick</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.kicking</groupId> <artifactId>leap-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.kicking</groupId> <artifactId>leap-boot-devtools</artifactId> <telescopic>runtime</telescopic> <optional>truthful</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</telescopic> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>leap-boot-starter-test</artifactId> <telescopic>examination</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>leap-kicking-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Domain Layer
The domain layer is a collection of entity objects and related business concern logic that is designed to correspond the business model.
Let'south create a JPAEmployee entity:
bundle com.springboot.tutorial.entity ; import java.fourth dimension.LocalDate ; import javax.persistence.Column ; import javax.persistence.Entity ; import javax.persistence.GeneratedValue ; import javax.persistence.GenerationType ; import javax.persistence.Id ; import javax.persistence.Table ; import org.springframework.format.annotation.DateTimeFormat ; import lombok.Data ; @Entity @Table ( name = "employee" ) @Data public class Employee { @Id @GeneratedValue ( strategy = GenerationType . IDENTITY ) @Column ( name = "id" ) private int id ; @Column ( name = "proper noun" ) private String name ; @Column ( proper name = "age" ) private int age ; @Column ( name = "gender" ) private String gender ; @Cavalcade ( name = "joiningDate" ) @DateTimeFormat ( design = "yyyy-MM-dd" ) individual LocalDate joiningDate ; @Column ( proper noun = "retiringDate" ) @DateTimeFormat ( blueprint = "yyyy-MM-dd" ) private LocalDate retiringDate ; @Column ( name = "dept" ) private String dept ; } Controller Layer
This layer is responsible for processing the user'due south input and returning the correct response back to the user.
Allow's create an EmployeeController course to send requests and bind data to the user's view.
package com.springboot.tutorial.controller ; import java.util.List ; import org.springframework.beans.factory.note.Autowired ; import org.springframework.stereotype.Controller ; import org.springframework.ui.Model ; import org.springframework.validation.BindingResult ; import org.springframework.web.bind.note.ModelAttribute ; import org.springframework.web.bind.annotation.RequestMapping ; import org.springframework.web.bind.annotation.RequestMethod ; import org.springframework.web.bind.annotation.RequestParam ; import com.springboot.tutorial.entity.Employee ; import com.springboot.tutorial.service.EmployeeService ; @Controller public form EmployeeController { @Autowired private EmployeeService service ; @RequestMapping ( "/" ) public String viewHomePage ( Model model ) { List < Employee > employeeList = service . listAll (); model . addAttribute ( "listAll" , employeeList ); return "index" ; } @RequestMapping ( "/new" ) public String showNewEmployeePage ( Model model ) { Employee employee = new Employee (); model . addAttribute ( "employee" , employee ); render "new_employee" ; } @RequestMapping ( value = "/salve" , method = RequestMethod . Postal service ) public String saveEmployee ( @ModelAttribute ( "employee" ) Employee employee , Model model , BindingResult result ) { service . save ( employee ); Listing < Employee > employeeList = service . listAll (); model . addAttribute ( "listAll" , employeeList ); model . addAttribute ( "employeeAttr" , employee ); return "index" ; } } Service Layer
This layer is the middleware between Controller and Repository.
The service gathers information from controller, performs validation and business logic, and calling repositories for data manipulation.
Let'southward create EmployeeService interface to send the user's requests from Controller to Repository.
package com.springboot.tutorial.service ; import java.util.List ; import com.springboot.tutorial.entity.Employee ; public interface EmployeeService { List < Employee > listAll (); void save ( Employee employee ); } And create EmployeeServiceImpl grade that implements EmployeeService interface and connect to Repository past using @Autowired keyword.
parcel com.springboot.tutorial.service ; import java.util.List ; import org.springframework.beans.mill.note.Autowired ; import org.springframework.stereotype.Service ; import com.springboot.tutorial.entity.Employee ; import com.springboot.tutorial.repository.EmployeeRepository ; @Service public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeRepository repo ; @Override public List < Employee > listAll () { return repo . findAll (); } @Override public void save ( Employee emp ) { repo . save ( emp ); } } Repository Layer
Jump Data JPA allows implementing JPA-based repositories
We don't need to write any DAO code simply Bound Data JPA will generate an implementation automatically at runtime.
Let's create anEmployeeRepository class with the post-obit code:
package com.springboot.tutorial.repository ; import org.springframework.data.jpa.repository.JpaRepository ; import org.springframework.stereotype.Repository ; import com.springboot.tutorial.entity.Employee ; @Repository public interface EmployeeRepository extends JpaRepository < Employee , Long > { } View Layer
We demand to create the HTML pages for displaying the Employee's registration form and showing the registered Employee's list under the src/main/resource/templates folder.
Nosotros volition use Thymeleaf equally the underlying template engine for parsing the template files.
Let's create an alphabetize.html as a welcome page and to show the registered Employee'southward list.
<!DOCTYPE html> <html xmlns= "http://world wide web.w3.org/1999/xhtml" xmlns:th= "http://www.thymeleaf.org" > <caput> <meta charset= "utf-8" /> <title>Employee Management</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.vii/css/bootstrap.min.css" > <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" ></script> <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script> <link rel= "stylesheet" href= "https://cdn.datatables.net/1.x.12/css/jquery.dataTables.min.css" > <script src= "https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js" ></script> <script type= "text/javascript" > $ ( certificate ). ready ( part () { $ ( ' #data-table ' ). DataTable (); }); </script> </head> <body> <div class= "content" > <div class= "animated fadeIn" > <div class= "row" > <div course= "col-md-12" > <div class= "card" > <div class= "card-header" > <strong form= "card-championship" > <h1 mode= "text-align: center; color: #b30000; font-weight: assuming;" >EMPLOYEE Management</h1> </stiff> <div manner= "text-align: center; font-weight: bold" > <a href= "new" >Create New Employee</a> <br /> </div> </div> <div class= "card-body" > <tabular array id= "data-table" class= "table table-striped tabular array-bordered" > <thead> <tr> <th>Employee ID</th> <th>Proper name</th> <th>Historic period</th> <th>Department</th> <thursday>Gender</thursday> <th>Joining Date</thursday> <th>Retiring Appointment</th> </tr> </thead> <tbody> <tr th:each= "employee:${listAll}" > <td th:text= "${employee.id}" >Product ID</td> <td th:text= "${employee.proper name}" >Proper name</td> <td th:text= "${employee.age}" >Age</td> <td th:text= "${employee.dept}" >Department</td> <td th:text= "${employee.gender}" >Gender</td> <td th:text= "${employee.joiningDate}" >Joining Appointment</td> <td th:text= "${employee.retiringDate}" >Retiring Date</td> </tr> </tbody> </table> </div> </div> </div> </div> </div> </div> </torso> </html> And create a new_employee.html page equally a registration grade page.
<!DOCTYPE html> <html xmlns= "http://www.w3.org/1999/xhtml" xmlns:thursday= "http://www.thymeleaf.org" > <caput> <meta charset= "utf-eight" /> <championship>Create New Employee</title> <link rel= "stylesheet" href= "//lawmaking.jquery.com/ui/i.12.i/themes/base/jquery-ui.css" > <link rel= "stylesheet" href= "/resources/demos/way.css" > <script src= "https://lawmaking.jquery.com/jquery-ane.12.four.js" ></script> <script src= "https://lawmaking.jquery.com/ui/1.12.i/jquery-ui.js" ></script> <script> $ ( function () { $ ( " #join_datepicker " ). datepicker ({ dateFormat : " yy-mm-dd " }); $ ( " #retire_datepicker " ). datepicker ({ dateFormat : " yy-mm-dd " }); }); </script> </head> <torso> <div align= "center" > <h1>Create New Emloyee</h1> <br /> <class activity= "#" thursday:action= "@{/salve}" th:object= "${employee}" method= "postal service" > <table edge= "0" cellpadding= "10" > <tr> <td>Employee Name:</td> <td><input type= "text" th:field= "*{proper name}" /></td> </tr> <tr> <td>Age:</td> <td><input type= "text" thursday:field= "*{historic period}" /></td> </tr> <tr> <td>Section:</td> <td> <select th:field= "*{dept}" > <choice th:value= "Information technology" th:text= "IT" ></choice> <option thursday:value= "Hr" thursday:text= "HR" ></selection> <option th:value= "Sales" th:text= "Sales" ></pick> </select> </td> </tr> <tr> <td>Gender:</td> <td><input blazon= "radio" th:field= "*{gender}" thursday:value= "Male" thursday:text= "Male" /> <input type= "radio" th:field= "*{gender}" th:value= "Female" th:text= "Female" /></td> </tr> <tr> <td>Joining Engagement:</td> <td><input blazon= "date" id= "join_datepicker" th:field= "*{joiningDate}" ></td> </tr> <tr> <td>Retiring Date:</td> <td><input type= "engagement" id= "retire_datepicker" th:field= "*{retiringDate}" ></td> </tr> <tr> <td colspan= "2" ><button type= "submit" >Save</push button></td> </tr> </table> </form> </div> </trunk> </html> Running the Awarding
Let's access web application using http://localhost:8080. Initially, you don't have any employee in the list.
Let's add a new employee first.
Yous can download the source code at
github
rowlettfetwerivid.blogspot.com
Source: https://dev.to/reytech-lesson/create-and-view-employee-using-jpa-springboot-and-thymeleaf-4mme
0 Response to "Spring Boot Thymeleaf Upload and Play Video Files Using Mysql"
Post a Comment