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.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Practice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.64</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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.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>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/demo5
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=update
server.port=8080
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.jsp
index.html
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "shown">
<h3>Enter Name : </h3>
<input type = "text" name = "name" />
<br />
<input type = "submit" value = "Submit" />
</form>
<br />
<h3>${obj}</h3>
<a href = "/home/"><button>Refresh</button></a>
<br />
<table border = 1 cellspacing = 5 cellpadding = 5>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
<td>Salary</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<c:forEach var="list" items="${list}">
<tr>
<td><c:out value = "${list.id}" /></td>
<td><c:out value = "${list.name}" /></td>
<td><c:out value = "${list.salary}" /></td>
<td><a href = "deleted?id=${list.id}">delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
EmpEntity.java
package com.example.demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "emp")
public class EmpEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column
String name;
@Column
int salary;
public EmpEntity() {
super();
// TODO Auto-generated constructor stub
}
public EmpEntity(Long id, String name, int salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
@Override
public String toString() {
return "EmpEntity [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
EmpRepo.java
package com.example.demo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmpRepo extends JpaRepository<EmpEntity, Long> {
List<EmpEntity> findByName(String name);
}
EmpService.java
package com.example.demo;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
@Service
public interface EmpService {
public void add(EmpEntity emp);
public List<EmpEntity> getall();
public Optional<EmpEntity> getbyid(Long id);
}
EmpServiceRepo.java
package com.example.demo;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EmpServiceRepo implements EmpService {
@Autowired
EmpRepo emprepo;
@Override
public void add(EmpEntity emp) {
emprepo.save(emp);
}
@Override
public List<EmpEntity> getall() {
return emprepo.findAll();
}
@Override
public Optional<EmpEntity> getbyid(Long id) {
// TODO Auto-generated method stub
return emprepo.findById(id);
}
}
HomeController.java
package com.example.demo;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
@RequestMapping("/home")
public class HomeController {
@Autowired
EmpRepo emprepo;
@Autowired
EmpService empservice;
@PostMapping("/add")
public String add(@RequestBody EmpEntity emp) {
empservice.add(emp);
return "inserted";
}
@GetMapping("/get")
public List<EmpEntity> get(){
return empservice.getall();
}
@GetMapping("/get/{id}")
public Optional<EmpEntity> getid(@PathVariable("id") Long id){
return empservice.getbyid(id);
}
@RequestMapping("/")
public ModelAndView mainpage() {
List<EmpEntity> emp = emprepo.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("list", emp);
mv.setViewName("index");
return mv;
}
@RequestMapping("/show")
public ModelAndView show(@RequestParam("id") Long id) {
ModelAndView mv = new ModelAndView();
EmpEntity emp = emprepo.findById(id).orElse(new EmpEntity());
System.out.println(emprepo.findByName("abc"));
mv.addObject("obj",emp);
mv.setViewName("index");
return mv;
}
@RequestMapping("/shown")
public ModelAndView show(@RequestParam("name") String name) {
ModelAndView mv = new ModelAndView();
String emp = emprepo.findByName(name).toString();
System.out.println(emprepo.findByName(name));
mv.addObject("obj",emp);
mv.setViewName("index");
return mv;
}
@GetMapping("/gett/{id}")
public Optional<EmpEntity> gettid(@PathVariable("id") Long id){
return emprepo.findById(id);
}
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id) {
emprepo.deleteById(id);
return "deleted";
}
@RequestMapping("/deleted")
public ModelAndView deleted(@RequestParam("id") Long id) {
ModelAndView mv = new ModelAndView();
emprepo.deleteById(id);
mv.setViewName("index");
return mv;
}
@PutMapping("/update")
public String update(@RequestBody EmpEntity emp) {
emprepo.save(emp);
return "updated";
}
}
PracticeApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PracticeApplication {
public static void main(String[] args) {
SpringApplication.run(PracticeApplication.class, args);
}
}
Comments
Post a Comment