Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.studentProject.entity;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.springframework.stereotype.Component;
- @Entity
- @Component
- @Table(name="students", schema="test_schema")
- public class StudentEntity implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue
- @Column(name="id")
- private Integer id;
- @Column(name="name")
- private String name;
- @Column(name="course")
- private String course;
- @Column(name="country")
- private String country;
- @Column(name="phone")
- private String phone;
- public StudentEntity(Integer id, String name, String course, String country, String phone) {
- super();
- this.id = id;
- this.name = name;
- this.course = course;
- this.country = country;
- this.phone = phone;
- }
- public StudentEntity() {
- super();
- // TODO Auto-generated constructor stub
- }
- 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 String getCourse() {
- return course;
- }
- public void setCourse(String course) {
- this.course = course;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- }
- package com.studentProject.repository;
- import org.springframework.data.repository.CrudRepository;
- import org.springframework.stereotype.Repository;
- import com.studentProject.entity.StudentEntity;
- @Repository
- public interface StudentRepository extends CrudRepository<StudentEntity, Integer> {
- StudentEntity findByName(String name);
- StudentEntity findByCourse(String course);
- }
- package com.studentProject.controller;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import com.studentProject.entity.StudentEntity;
- import com.studentProject.repository.StudentRepository;
- @RestController
- public class StudentController {
- @Autowired
- private StudentRepository studentRepository;
- @Autowired
- List<StudentEntity> studentList;
- public StudentController(StudentRepository studentRepository, StudentEntity studentEntity,
- List<StudentEntity> studentList) {
- super();
- this.studentRepository = studentRepository;
- this.studentList = studentList;
- }
- @GetMapping("/getStudentDetails")
- public Iterable<StudentEntity> getAllStudents(){
- return studentRepository.findAll();
- }
- @GetMapping("/getStudentDetails/getByName")
- public StudentEntity findByName(@RequestParam(value="name") String name) {
- return studentRepository.findByName(name);
- }
- @GetMapping("/getStudentDetails/getById/{id}")
- public Optional<StudentEntity> findById(@PathVariable(value="id") Integer id) {
- return studentRepository.findById(id);
- }
- @GetMapping("/getStudentDetails/welcome")
- public String welcome() {
- return "hi brother welcome";
- }
- }
- server.port = 8080
- spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
- spring.datasource.url = jdbc:mysql://localhost:3306/
- spring.datasource.username = root
- spring.datasource.password = root
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- spring.data.jpa.repositories.enabled=true
- spring.jpa.show-sql = true
- spring.jpa.hibernate.ddl-auto = none
- spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
- <?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 http://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.1.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.StudentProject</groupId>
- <artifactId>StudentProject-1</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>StudentProject-1</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-actuator</artifactId>
- </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-data-rest</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>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </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>
- </plugin>
- </plugins>
- </build>
- </project>
- package com.studentProject;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class StudentProject1Application {
- public static void main(String[] args) {
- SpringApplication.run(StudentProject1Application.class, args);
- }
- }
Add Comment
Please, Sign In to add comment