Guest User

Untitled

a guest
Dec 14th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. package com.studentProject.entity;
  2. import java.io.Serializable;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. import org.springframework.stereotype.Component;
  9.  
  10. @Entity
  11. @Component
  12. @Table(name="students", schema="test_schema")
  13. public class StudentEntity implements Serializable{
  14. private static final long serialVersionUID = 1L;
  15.  
  16. @Id
  17. @GeneratedValue
  18. @Column(name="id")
  19. private Integer id;
  20.  
  21. @Column(name="name")
  22. private String name;
  23.  
  24. @Column(name="course")
  25. private String course;
  26.  
  27. @Column(name="country")
  28. private String country;
  29.  
  30. @Column(name="phone")
  31. private String phone;
  32.  
  33. public StudentEntity(Integer id, String name, String course, String country, String phone) {
  34. super();
  35. this.id = id;
  36. this.name = name;
  37. this.course = course;
  38. this.country = country;
  39. this.phone = phone;
  40. }
  41.  
  42. public StudentEntity() {
  43. super();
  44. // TODO Auto-generated constructor stub
  45. }
  46.  
  47. public Integer getId() {
  48. return id;
  49. }
  50. public void setId(Integer id) {
  51. this.id = id;
  52. }
  53.  
  54. public String getName() {
  55. return name;
  56. }
  57.  
  58. public void setName(String name) {
  59. this.name = name;
  60. }
  61.  
  62. public String getCourse() {
  63. return course;
  64. }
  65.  
  66. public void setCourse(String course) {
  67. this.course = course;
  68. }
  69.  
  70. public String getCountry() {
  71. return country;
  72. }
  73.  
  74. public void setCountry(String country) {
  75. this.country = country;
  76. }
  77.  
  78. public String getPhone() {
  79. return phone;
  80. }
  81.  
  82. public void setPhone(String phone) {
  83. this.phone = phone;
  84. }
  85. }
  86.  
  87. package com.studentProject.repository;
  88.  
  89. import org.springframework.data.repository.CrudRepository;
  90.  
  91. import org.springframework.stereotype.Repository;
  92.  
  93. import com.studentProject.entity.StudentEntity;
  94.  
  95. @Repository
  96.  
  97. public interface StudentRepository extends CrudRepository<StudentEntity, Integer> {
  98.  
  99. StudentEntity findByName(String name);
  100.  
  101. StudentEntity findByCourse(String course);
  102.  
  103. }
  104.  
  105. package com.studentProject.controller;
  106.  
  107. import java.util.ArrayList;
  108. import java.util.List;
  109. import java.util.Optional;
  110.  
  111. import org.springframework.beans.factory.annotation.Autowired;
  112. import org.springframework.web.bind.annotation.GetMapping;
  113. import org.springframework.web.bind.annotation.PathVariable;
  114. import org.springframework.web.bind.annotation.RequestParam;
  115. import org.springframework.web.bind.annotation.RestController;
  116.  
  117. import com.studentProject.entity.StudentEntity;
  118. import com.studentProject.repository.StudentRepository;
  119.  
  120. @RestController
  121. public class StudentController {
  122.  
  123.  
  124. @Autowired
  125. private StudentRepository studentRepository;
  126.  
  127. @Autowired
  128. List<StudentEntity> studentList;
  129.  
  130. public StudentController(StudentRepository studentRepository, StudentEntity studentEntity,
  131. List<StudentEntity> studentList) {
  132. super();
  133. this.studentRepository = studentRepository;
  134. this.studentList = studentList;
  135. }
  136.  
  137. @GetMapping("/getStudentDetails")
  138. public Iterable<StudentEntity> getAllStudents(){
  139.  
  140. return studentRepository.findAll();
  141. }
  142.  
  143. @GetMapping("/getStudentDetails/getByName")
  144. public StudentEntity findByName(@RequestParam(value="name") String name) {
  145.  
  146. return studentRepository.findByName(name);
  147. }
  148.  
  149. @GetMapping("/getStudentDetails/getById/{id}")
  150. public Optional<StudentEntity> findById(@PathVariable(value="id") Integer id) {
  151.  
  152. return studentRepository.findById(id);
  153. }
  154.  
  155. @GetMapping("/getStudentDetails/welcome")
  156. public String welcome() {
  157.  
  158. return "hi brother welcome";
  159. }
  160. }
  161.  
  162. server.port = 8080
  163. spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
  164. spring.datasource.url = jdbc:mysql://localhost:3306/
  165. spring.datasource.username = root
  166. spring.datasource.password = root
  167. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  168. spring.data.jpa.repositories.enabled=true
  169. spring.jpa.show-sql = true
  170. spring.jpa.hibernate.ddl-auto = none
  171. spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
  172.  
  173. <?xml version="1.0" encoding="UTF-8"?>
  174. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  175. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  176. <modelVersion>4.0.0</modelVersion>
  177. <parent>
  178. <groupId>org.springframework.boot</groupId>
  179. <artifactId>spring-boot-starter-parent</artifactId>
  180. <version>2.1.1.RELEASE</version>
  181. <relativePath/> <!-- lookup parent from repository -->
  182. </parent>
  183. <groupId>com.StudentProject</groupId>
  184. <artifactId>StudentProject-1</artifactId>
  185. <version>0.0.1-SNAPSHOT</version>
  186. <name>StudentProject-1</name>
  187. <description>Demo project for Spring Boot</description>
  188.  
  189. <properties>
  190. <java.version>1.8</java.version>
  191. </properties>
  192.  
  193. <dependencies>
  194. <dependency>
  195. <groupId>org.springframework.boot</groupId>
  196. <artifactId>spring-boot-starter-actuator</artifactId>
  197. </dependency>
  198. <dependency>
  199. <groupId>org.springframework.boot</groupId>
  200. <artifactId>spring-boot-starter-data-jpa</artifactId>
  201. </dependency>
  202. <dependency>
  203. <groupId>org.springframework.boot</groupId>
  204. <artifactId>spring-boot-starter-data-rest</artifactId>
  205. </dependency>
  206. <dependency>
  207. <groupId>org.springframework.boot</groupId>
  208. <artifactId>spring-boot-starter-web</artifactId>
  209. </dependency>
  210.  
  211. <dependency>
  212. <groupId>org.springframework.boot</groupId>
  213. <artifactId>spring-boot-devtools</artifactId>
  214. <scope>runtime</scope>
  215. </dependency>
  216. <dependency>
  217. <groupId>org.springframework.boot</groupId>
  218. <artifactId>spring-boot-starter-jdbc</artifactId>
  219. </dependency>
  220.  
  221. <dependency>
  222. <groupId>mysql</groupId>
  223. <artifactId>mysql-connector-java</artifactId>
  224. <scope>runtime</scope>
  225. </dependency>
  226. <dependency>
  227. <groupId>org.projectlombok</groupId>
  228. <artifactId>lombok</artifactId>
  229. <optional>true</optional>
  230. </dependency>
  231. <dependency>
  232. <groupId>org.springframework.boot</groupId>
  233. <artifactId>spring-boot-starter-test</artifactId>
  234. <scope>test</scope>
  235. </dependency>
  236. </dependencies>
  237.  
  238. <build>
  239. <plugins>
  240. <plugin>
  241. <groupId>org.springframework.boot</groupId>
  242. <artifactId>spring-boot-maven-plugin</artifactId>
  243. </plugin>
  244. </plugins>
  245. </build>
  246.  
  247. </project>
  248.  
  249. package com.studentProject;
  250.  
  251. import org.springframework.boot.SpringApplication;
  252. import org.springframework.boot.autoconfigure.SpringBootApplication;
  253.  
  254. @SpringBootApplication
  255. public class StudentProject1Application {
  256.  
  257. public static void main(String[] args) {
  258. SpringApplication.run(StudentProject1Application.class, args);
  259. }
  260.  
  261. }
Add Comment
Please, Sign In to add comment