Commit 2ff96754 authored by Suresh Kumar's avatar Suresh Kumar

SpringBoot Assignments

parent 6c965654
<?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.springboot.test</groupId>
<artifactId>JPASpringBootExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JPASpringBootExample</name>
<description>spring boot with jpa</description>
<properties>
<java.version>1.8</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-web</artifactId>
</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>
package com.springboot.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.springboot.test.dao.UserRepository;
import com.springboot.test.entities.User;
@SpringBootApplication
public class JpaSpringBootExampleApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(JpaSpringBootExampleApplication.class, args);
UserRepository userRepository = context.getBean(UserRepository.class);
User user = new User();
user.setRollNo ("102");
user.setName("Arsam Rajput");
User user2 = new User();
user2.setRollNo ("104");
user2.setName("Asad Iqbal");
User user1 = userRepository.save(user);
User user3 = userRepository.save(user2);
System.out.println(user1);
System.out.println(user3);
}
}
package com.springboot.test.dao;
import org.springframework.data.repository.CrudRepository;
import com.springboot.test.entities.User;
public interface UserRepository extends CrudRepository<User , Integer>{
}
package com.springboot.test.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String rollNo;
private String name;
public User(int id, String rollNo, String name) {
super();
this.id = id;
this.rollNo = rollNo;
this.name = name;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [id=" + id + ", rollNo=" + rollNo + ", name=" + name + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
spring.datasource.name=myhibernate
spring.datasource.url=jdbc:mysql://localhost:3306/myhibernate?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.hibernate.ddl-auto=update
\ No newline at end of file
package com.springboot.test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class JpaSpringBootExampleApplicationTests {
@Test
void contextLoads() {
}
}
<?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.studentcrudoperation</groupId>
<artifactId>manytomanyrelationshipusing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ManyToManyRelationshipUsingSpringBoot</name>
<description>Many To Many Relationship Using Spring Boot</description>
<properties>
<java.version>1.8</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-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>
package com.studentcrudoperation.Controller;
import java.util.List;
import com.studentcrudoperation.Model.Course;
import com.studentcrudoperation.Service.CourseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
public class CourseController {
@Autowired
CourseService courseService;
@GetMapping("/courses")
public List<Course> getAllStudents() {
return this.courseService.getAllCourses();
}
@GetMapping("/courses/{id}")
public Course getCourseByID(@PathVariable("id") int cid) {
return this.courseService.getCourseByID(cid);
}
@PostMapping("/courses")
public Course addCourse(@RequestBody Course course) {
Course co = this.courseService.addCourse(course);
return co;
}
@PutMapping("/courses/{courseId}")
public Course updateCourse(@RequestBody Course course, @PathVariable("courseId")int courseId) {
this.courseService.updateCourse(course, courseId);
return course;
}
@DeleteMapping("/courses/{courseId}")
public void deleteCourse(@PathVariable("courseId") int courseId) {
this.courseService.deleteCourseById(courseId);
}
}
\ No newline at end of file
package com.studentcrudoperation.Controller;
import java.util.List;
import com.studentcrudoperation.Model.Student;
import com.studentcrudoperation.Service.StudentService;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping("/students")
public List<Student> getAllStudents() {
return this.studentService.getAllStudents();
}
@GetMapping("/students/{id}")
public Student getStudentByID(@PathVariable("id") int id) {
return this.studentService.getStudentByID(id);
}
@PostMapping("/students")
public Student addStudent(@RequestBody Student student) {
Student st = this.studentService.addStudent(student);
return st;
}
@PutMapping("/students/{studentId}")
public Student updateStudent(@RequestBody Student student, @PathVariable("studentId")int studentId) {
this.studentService.updateStudent(student, studentId);
return student;
}
@DeleteMapping("/students/{studentId}")
public void deleteStudent(@PathVariable("studentId") int studentId) {
this.studentService.deleteStudentById(studentId);
}
}
package com.studentcrudoperation;
import com.studentcrudoperation.Model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j
public class ManyToManyRelationshipUsingSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(ManyToManyRelationshipUsingSpringBootApplication.class, args);
log.info("Project Started.......!!");
}
}
package com.studentcrudoperation.Model;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
import lombok.ToString;
import java.util.ArrayList;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.JoinTable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "course")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer cid;
private String cname;
@ManyToMany
@JoinTable(name="student_course", joinColumns=@JoinColumn(name="cid"),
inverseJoinColumns=@JoinColumn(name="sid"))
private List <Student> students = new ArrayList<>();
}
package com.studentcrudoperation.Model;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
import lombok.ToString;
import java.util.ArrayList;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Entity;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import javax.persistence.ManyToMany;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer sid;
private String rno;
private String name;
@ManyToMany(mappedBy ="students")
private List<Course> courses = new ArrayList<>();
}
package com.studentcrudoperation.Repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.studentcrudoperation.Model.Course;
@Repository
public interface CourseRepository extends CrudRepository <Course , Integer>{
Course findById(int cid);
}
\ No newline at end of file
package com.studentcrudoperation.Repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.studentcrudoperation.Model.Student;
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{
Student findById(int sid);
}
package com.studentcrudoperation.Service;
import java.util.List;
import com.studentcrudoperation.Model.Course;
public interface CourseService {
public List<Course> getAllCourses();
// Students get by id
public Course getCourseByID(int cid);
// Add student records
public Course addCourse(Course co);
//Delete by id
public void deleteCourseById(int coid);
// Update records
public void updateCourse(Course course, int CourseId);
public Course save(Course course);
}
package com.studentcrudoperation.Service;
import java.util.List;
import com.studentcrudoperation.Model.Course;
import org.springframework.stereotype.Service;
import com.studentcrudoperation.Repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseRepository crepo;
@Override
public List<Course> getAllCourses() {
List<Course> list = (List<Course>)crepo.findAll();
return list;
}
@Override
public Course getCourseByID(int cid) {
Course course = null;
course = crepo.findById(cid);
return course;
}
@Override
public Course addCourse(Course co) {
crepo.save(co);
return co;
}
@Override
public void deleteCourseById(int coid) {
this.crepo.deleteById(coid);
}
@Override
public void updateCourse(Course course, int CourseId) {
course.setCid(CourseId);
crepo.save(course);
}
@Override
public Course save(Course course) {
// TODO Auto-generated method stub
return null;
}
}
package com.studentcrudoperation.Service;
import java.util.List;
import com.studentcrudoperation.Model.Student;
public interface StudentService {
//Get All Students
public List<Student> getAllStudents();
// Students get by id
public Student getStudentByID(int id);
// Add student records
public Student addStudent(Student st);
//Delete by id
public void deleteStudentById(int ssid);
// Update records
public void updateStudent(Student student, int studentId);
public Student save(Student student);
}
\ No newline at end of file
package com.studentcrudoperation.Service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.studentcrudoperation.Model.Student;
import com.studentcrudoperation.Repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentRepository studentRepository;
@Override
public List<Student> getAllStudents() {
List<Student> list = (List<Student>) studentRepository.findAll();
return list;
}
@Override
public Student getStudentByID(int id) {
Student student = null;
student = studentRepository.findById(id );
return student;
}
@Override
public Student addStudent(Student st) {
studentRepository.save(st);
return st;
}
@Override
public void deleteStudentById(int ssid) {
this.studentRepository.deleteById(ssid);
}
@Override
public void updateStudent(Student student, int studentId) {
student.setSid(studentId);
studentRepository.save(student);
}
@Override
public Student save(Student student) {
// TODO Auto-generated method stub
return null;
}
}
\ No newline at end of file
server.port=8082
spring.datasource.name=springBoot
spring.datasource.url=jdbc:mysql://localhost:3306/springBoot?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
package com.studentcrudoperation;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ManyToManyRelationshipUsingSpringBootApplicationTests {
@Test
void contextLoads() {
}
}
<?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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.firstprogramspringboot</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootHelloWorld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
package com.firstprogramspringboot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@RequestMapping("/HelloWorld")
@ResponseBody
public String helloWorld() {
return "Hello World .......";
}
}
package com.firstprogramspringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
package com.firstprogramspringboot;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootHelloWorldApplicationTests {
@Test
void contextLoads() {
}
}
<?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.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.project</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootHelloWorldUsingJsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.63</version>
</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>
package com.springboot.project;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
package com.springboot.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootHelloWorldUsingJspApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldUsingJspApplication.class, args);
}
}
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<style>
*{
margin:0px;
padding:0px;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-primary">
<a class="navbar-brand text-info font-weight-bold" href="http://localhost:8080/CreateASimpleWebApplication/#">NISUM TECHNOLOGY</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="https://www.nisum.com/">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">About Us <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Contact Us <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-expanded="false">
Carears
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="card ">
<div class="card-header text-center">
<br>
<h5 class="card-title text-info">WELCOME TO NISUM TECHNOLOGY</h5>
<h4 class="card-text text-success">Nisum has developed proprietary tools that can be instantly integrated into your company's infrastructure to rapidly solve problems in business agility.</h4>
<a href="" class="btn btn-primary">Crud Operation</a>
</div>
<br><br><br>
<div class="card-body">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card rounded-lg border border-danger">
<div class="card-header">
<h1 align="center">WELCOME TO NISUM TECHNOLOGY</h1>
</div>
</div>
</div>
</div>
</div>
<br><br>
<h1 class="card-title text-primary text-center">NISUM IS HIRING FOR JAVA DEVELOPER .</h1>
<br><br>
<h5 class="card-title text-warning">We have a 15-year average tenure with our clients because we strive to consistently deliver excellence, and we have a proven track record of fulfilling our commitments. We believe our clients' success is our success and have grown because of our clients' trust in our expertise and integrity. We commit to continuing to invest in our partners' needs and strive to develop and grow successful, long-term relationships.</h5>
</div>
<div class="card-footer bg-info text-center">
<br><br><br>
<h5 class="card-title text-warning">BUILDING SUCCESS TOGETHER</h5>
<h4 class="card-text text-danger">Nisum Technology Is Hiring For Java Developer & Java Script Developer </h4>
<a href="https://www.nisum.com/" class="btn btn-dark">For More Updates Visit THis Site</a>
<br><br>
</div>
</body>
</html>
\ No newline at end of file
package com.springboot.project;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootHelloWorldUsingJspApplicationTests {
@Test
void contextLoads() {
}
}
server.port = 8090
spring.datasource.name=springBootPractice
spring.datasource.url=jdbc:mysql://localhost:3306/springBootPractice?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
<?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.studentcrud</groupId>
<artifactId>StudentCrudOperation </artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>StudentCrudOperationWIthSpringBootAndJpa</name>
<description>Creating Students API </description>
<properties>
<java.version>1.8</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.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>
package com.studentcrud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudentCrudOperationWIthSpringBootAndJpaApplication {
public static void main(String[] args) {
SpringApplication.run(StudentCrudOperationWIthSpringBootAndJpaApplication.class, args);
System.out.println("Project Started........");
}
}
package com.studentcrud.controller;
import java.util.List;
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.RestController;
import com.studentcrud.model.Student;
import com.studentcrud.service.StudentService;
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping("/students")
public List<Student> getAllStudents() {
return this.studentService.getAllStudents();
}
@GetMapping("/students/{id}")
public Student getStudentByID(@PathVariable("id") int id) {
return this.studentService.getStudentByID(id);
}
@PostMapping("/students")
public Student addStudent(@RequestBody Student student) {
Student st = this.studentService.addStudent(student);
return st;
}
@PutMapping("/students/{studentId}")
public Student updateStudent(@RequestBody Student student, @PathVariable("studentId")int studentId) {
this.studentService.updateStudent(student, studentId);
return student;
}
@DeleteMapping("/students/{studentId}")
public void deleteStudent(@PathVariable("studentId") int studentId) {
this.studentService.deleteStudentById(studentId);
}
}
package com.studentcrud.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student_record")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String rno;
private String name;
public Student(int id, String rno, String name) {
super();
this.id = id;
this.rno = rno;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRno() {
return rno;
}
public void setRno(String rno) {
this.rno = rno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [id=" + id + ", rno=" + rno + ", name=" + name + "]";
}
}
package com.studentcrud.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.studentcrud.model.Student;
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{
Student findById(int id);
}
package com.studentcrud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.studentcrud.model.Student;
import com.studentcrud.repository.StudentRepository;
@Service
public class StudentService {
@Autowired
private StudentRepository repo;
public List<Student> getAllStudents() {
List<Student> list = (List<Student>)repo.findAll();
return list;
}
// students get by id
public Student getStudentByID(int id) {
Student student = null;
student = repo.findById(id);
return student;
}
// add student records
public Student addStudent(Student st) {
repo.save(st);
return st;
}
//delete by id
public void deleteStudentById(int sid) {
this.repo.deleteById(sid);
}
// update records
public void updateStudent(Student student, int studentId) {
student.setId(studentId);
repo.save(student);
}
public Student save(Student student) {
// TODO Auto-generated method stub
return null;
}
}
server.port = 8090
spring.datasource.name=springBootPractice
spring.datasource.url=jdbc:mysql://localhost:3306/springBootPractice?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
package com.studentcrud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StudentCrudOperationWIthSpringBootAndJpaApplicationTests {
@Test
void contextLoads() {
}
}
<?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.studentcrud</groupId>
<artifactId>StudentCrudOperation </artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>StudentCrudOperationWIthSpringBootAndJpa</name>
<description>Creating Students API </description>
<properties>
<java.version>1.8</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.studentcrud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudentCrudOperationWIthSpringBootAndJpaApplication {
public static void main(String[] args) {
SpringApplication.run(StudentCrudOperationWIthSpringBootAndJpaApplication.class, args);
System.out.println("Project Started........");
}
}
package com.studentcrud.controller;
import java.util.List;
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.RestController;
import com.studentcrud.model.Student;
import com.studentcrud.service.StudentServiceImpl;
@RestController
public class StudentController {
@Autowired
StudentServiceImpl studentService;
@GetMapping("/students")
public List<Student> getAllStudents() {
return this.studentService.getAllStudents();
}
@GetMapping("/students/{id}")
public Student getStudentByID(@PathVariable("id") int id) {
return this.studentService.getStudentByID(id);
}
@PostMapping("/students")
public Student addStudent(@RequestBody Student student) {
Student st = this.studentService.addStudent(student);
return st;
}
@PutMapping("/students/{studentId}")
public Student updateStudent(@RequestBody Student student, @PathVariable("studentId")int studentId) {
this.studentService.updateStudent(student, studentId);
return student;
}
@DeleteMapping("/students/{studentId}")
public void deleteStudent(@PathVariable("studentId") int studentId) {
this.studentService.deleteStudentById(studentId);
}
}
package com.studentcrud.model;
import javax.persistence.Entity;
import javax.persistence.Table;
public class Course {
private Integer id;
private String name;
public Course(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Course() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Course [id=" + id + ", name=" + name + "]";
}
}
package com.studentcrud.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
@Entity
@Table(name = "student_record")
@Getter
@Setter
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String rno;
private String name;
public Student(int id, String rno, String name) {
super();
this.id = id;
this.rno = rno;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRno() {
return rno;
}
public void setRno(String rno) {
this.rno = rno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [id=" + id + ", rno=" + rno + ", name=" + name + "]";
}
}
package com.studentcrud.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.studentcrud.model.Student;
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{
Student findById(int id);
}
package com.studentcrud.service;
public interface CourseService {
}
package com.studentcrud.service;
public class CourseServiceImpl {
}
package com.studentcrud.service;
import java.util.List;
import com.studentcrud.model.Student;
public interface StudentService {
//Get All Students
public List<Student> getAllStudents();
// Students get by id
public Student getStudentByID(int id);
// Add student records
public Student addStudent(Student st);
//Delete by id
public void deleteStudentById(int sid);
// Update records
public void updateStudent(Student student, int studentId);
public Student save(Student student);
}
package com.studentcrud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.studentcrud.model.Student;
import com.studentcrud.repository.StudentRepository;
@Service
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentRepository repo;
@Override
public List<Student> getAllStudents() {
List<Student> list = (List<Student>)repo.findAll();
return list;
}
@Override
public Student getStudentByID(int id) {
Student student = null;
student = repo.findById(id);
return student;
}
@Override
public Student addStudent(Student st) {
repo.save(st);
return st;
}
@Override
public void deleteStudentById(int sid) {
this.repo.deleteById(sid);
}
@Override
public void updateStudent(Student student, int studentId) {
student.setId(studentId);
repo.save(student);
}
@Override
public Student save(Student student) {
// TODO Auto-generated method stub
return null;
}
// public List<Student> getAllStudents() {
// List<Student> list = (List<Student>)repo.findAll();
// return list;
// }
//
// // students get by id
// public Student getStudentByID(int id) {
// Student student = null;
// student = repo.findById(id);
// return student;
// }
//
// // add student records
// public Student addStudent(Student st) {
// repo.save(st);
// return st;
// }
//
// //delete by id
//
// public void deleteStudentById(int sid) {
// this.repo.deleteById(sid);
// }
//
// // update records
// public void updateStudent(Student student, int studentId) {
// student.setId(studentId);
// repo.save(student);
// }
//
// public Student save(Student student) {
// // TODO Auto-generated method stub
// return null;
// }
}
server.port=9090
spring.datasource.name=springBootPractice
spring.datasource.url=jdbc:mysql://localhost:3306/springBootPractice?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
\ No newline at end of file
package com.studentcrud;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class StudentCrudOperationWIthSpringBootAndJpaApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment