Skip to main content

spring boot project | spring boot crud web application | Spring Boot Simple Project

 spring boot crud web application


for Explanation watch video









Directory Structure





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-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>SpringBootCrud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootCrud</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>



application.properties



#server port
server.port=3031

#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/new
spring.datasource.username=root
spring.datasource.password=root

#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update



SpringBootCrudApplication 


package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootCrudApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootCrudApplication.class, args);
}

}



EmployeeController 


package com.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.app.model.Employee;
import com.app.service.IEmployeeService;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private IEmployeeService service;
//http://3031/employee/show
@GetMapping("/show")
public String show() {
return "EmployeeRegister";
}
@PostMapping("/register")
public String saveEmployee(@ModelAttribute Employee emp) {
service.saveEmployee(emp);
return "redirect:all";
}
@GetMapping("/all")
public String showEmployees(Model model) {
List<Employee> list = service.getAllEmployees();
model.addAttribute("list", list);
return "EmployeeData";
}
//http://3031/employee/remove?id=2
@GetMapping("/remove")
public String removeEmployee(@RequestParam Integer id) {
service.deleteEmployeeById(id);
return "redirect:all";
}
@GetMapping("/edit")
public String editEmployee(@RequestParam Integer id,Model model) {
Employee emp = service.getEmployeeById(id);
model.addAttribute("emp", emp);
return "EmployeeEdit";
}
@PostMapping("/update")
public String updateEmployee(@ModelAttribute Employee emp) {
service.updateEmployee(emp);
return "redirect:all";
}
}



Employee 


package com.app.model;

import java.util.Date;

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 javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
public class Employee {
@Id
@Column(name = "eid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer empId;
@Column(name = "ename")
private String empName;
@Column(name = "egen")
private String empGen;
@Column(name = "edept")
private String empDept;
@Column(name = "esal")
private Double empSal;
@Column(name = "email")
private String email;
@DateTimeFormat(iso = ISO.DATE)
@Temporal(TemporalType.DATE)
@Column(name = "Doj")
private Date empDoj;
}


EmployeeRepository 



package com.app.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.app.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Integer>{

}


IEmployeeService 


package com.app.service;

import java.util.List;

import com.app.model.Employee;

public interface IEmployeeService {
void saveEmployee(Employee emp);
List<Employee> getAllEmployees();
void deleteEmployeeById(Integer id);
Employee getEmployeeById(Integer id);
void updateEmployee(Employee emp);
}


EmployeeServiceImpl 


package com.app.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.model.Employee;
import com.app.repo.EmployeeRepository;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

@Autowired
private EmployeeRepository repo;
@Override
public void saveEmployee(Employee emp) {
repo.save(emp);
}

public List<Employee> getAllEmployees() {
return repo.findAll();
}

@Override
public void deleteEmployeeById(Integer id) {
repo.deleteById(id);
}

@Override
public Employee getEmployeeById(Integer id) {
Optional<Employee> opt = repo.findById(id);
if(opt.isEmpty()) {
return null;
}
return opt.get();
}

@Override
public void updateEmployee(Employee emp) {
repo.save(emp);
}

}


EmployeeData.html


<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>Register</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-sm bg-light">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" th:href="@{/employee/all}">Data</a></li>
<li class="nav-item"><a class="nav-link" th:href="@{/employee/show}">Register</a></li>
</ul>
</nav>
<div class="card">
<div class="card-header bg-primary text-white text-center">
<h2>Employees Data</h2>
</div>
<div class="card-body">
<table class="table table-hover">
<tr>
<th>EmpName</th>
<th>Gender</th>
<th>Sal</th>
<th>Email</th>
<th>Dept</th>
<th>Doj</th>
<th>Operations</th>
</tr>
<tr th:each="ob:${list}">
<td>[[${ob.empName}]]</td>
<td>[[${ob.empGen}]]</td>
<td>[[${ob.empSal}]]</td>
<td>[[${ob.email}]]</td>
<td>[[${ob.empDept}]]</td>
<td>[[${ob.empDoj}]]</td>
<td><a th:href="@{/employee/remove(id=${ob.empId})}"
class="btn btn-danger">Delete</a> <a
th:href="@{/employee/edit(id=${ob.empId})}"
class="btn btn-warning">Edit</a></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>



EmployeeEdit.html


<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>Register</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-sm bg-light">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link"
th:href="@{/employee/all}">Data</a></li>
<li class="nav-item"><a class="nav-link"
th:href="@{/employee/show}">Register</a></li>
</ul>
</nav>
<div class="card">
<div class="card-header bg-primary text-white text-center">
<h2>Employee Register Page</h2>
</div>
<div class="card-body">
<form method="post" th:action="@{/employee/update}"
th:object="${emp}">
Id : <input type="text" th:field="*{empId}" class="form-control"
readonly> Name : <input type="text" th:field="*{empName}"
class="form-control"> Sal : <input type="text"
th:field="*{empSal}" class="form-control"> Gender : <input
type="radio" value="male" th:field="*{empGen}">Male <input
type="radio" value="female" th:field="*{empGen}">Female <br>
Email: <input type="text" th:field="*{email}" class="form-control">
Dept : <select class="form-control" th:field="*{empDept}">
<option value="">-select-</option>
<option value="DEV">DEV</option>
<option value="QA">QA</option>
<option value="BA">BA</option>
</select> Doj : <input type="date" th:field="*{empDoj}" class="form-control" />
<input type="submit" value="register"
class="btn btn-outline-success">
</form>
</div>
</div>
</div>
</body>
</html>



EmployeeRegister.html



<html xmlns:th="https://www.thymeleaf.org/">
<head>
<title>Register</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<nav class="navbar navbar-expand-sm bg-light">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link"
th:href="@{/employee/all}">Data</a></li>
<li class="nav-item"><a class="nav-link"
th:href="@{/employee/show}">Register</a></li>
</ul>
</nav>
<div class="card">
<div class="card-header bg-primary text-white text-center">
<h2>Employee Register Page</h2>
</div>
<div class="card-body">
<form method="post" th:action="@{/employee/register}">
Name : <input type="text" name="empName" class="form-control">
Sal : <input type="text" name="empSal" class="form-control">
Gender : <input type="radio" value="male" name="empGen">Male
<input type="radio" value="female" name="empGen">Female <br>
Email: <input type="text" name="email" class="form-control">
Dept : <select class="form-control" name="empDept">
<option value="">-select-</option>
<option value="DEV">DEV</option>
<option value="QA">QA</option>
<option value="BA">BA</option>
</select> Doj : <input type="date" name="empDoj" class="form-control" /> <input
type="submit" value="register" class="btn btn-outline-success">
</form>
</div>
</div>
</div>
</body>
</html>


Comments

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }