Skip to content

Commit a0e75db

Browse files
author
SKP
committed
Spring Boot MySQL Example
Spring Boot MySQL Example
1 parent 3154847 commit a0e75db

8 files changed

Lines changed: 248 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

springboot-mysql-example/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.javaoutofbounds</groupId>
7+
<artifactId>springboot-mysql-example</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-mysql-example</name>
12+
<description>SpringBoot MySQL Example</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>mysql</groupId>
39+
<artifactId>mysql-connector-java</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
59+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.javaoutofbounds.pojo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootMysqlExampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootMysqlExampleApplication.class, args);
11+
}
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.javaoutofbounds.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Student {
10+
11+
@Id
12+
@GeneratedValue(strategy=GenerationType.AUTO)
13+
private Integer id;
14+
private String name;
15+
private String contact;
16+
17+
public Integer getId() {
18+
return id;
19+
}
20+
21+
public void setId(Integer id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getContact() {
34+
return contact;
35+
}
36+
37+
public void setContact(String contact) {
38+
this.contact = contact;
39+
}
40+
41+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.javaoutofbounds.pojo;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
@Controller
10+
public class StudentController {
11+
12+
@Autowired
13+
StudentRepository studentRepository;
14+
15+
/**
16+
* Adds New Record to MySQL db_StudentRecords database
17+
*
18+
*/
19+
@GetMapping("/create")
20+
@ResponseBody
21+
public String createStudentRecord(@RequestParam String name, @RequestParam String contact) {
22+
23+
final Student student = new Student();
24+
student.setName(name);
25+
student.setContact(contact);
26+
studentRepository.save(student);
27+
28+
return "Record Created Successfully!";
29+
}
30+
31+
/**
32+
* Reads All records from MySQL db_StudentRecords database
33+
*
34+
*/
35+
@GetMapping("/read")
36+
@ResponseBody
37+
public Iterable<Student> readAllStudentRecords() {
38+
39+
return studentRepository.findAll();
40+
}
41+
42+
/**
43+
* Update specified records to MySQL db_StudentRecords database
44+
*
45+
*/
46+
@GetMapping("/update")
47+
@ResponseBody
48+
public String updateStudentRecord(@RequestParam Integer id, @RequestParam String name,
49+
@RequestParam String contact) {
50+
51+
final Student student = studentRepository.findOne(id);
52+
student.setName(name);
53+
student.setContact(contact);
54+
studentRepository.save(student);
55+
56+
return "Record Updated Successfully!";
57+
}
58+
59+
/**
60+
* Deletes specified records from MySQL db_StudentRecords database
61+
*
62+
*/
63+
@GetMapping("/delete")
64+
@ResponseBody
65+
public String deleteStudentRecord(@RequestParam Integer id) {
66+
67+
studentRepository.delete(id);
68+
69+
return "Record Deleted Successfully!";
70+
}
71+
72+
/**
73+
* Custom query to find records using contactNumber from MySQL db_StudentRecords database
74+
*
75+
*/
76+
@GetMapping("/findByContactNumber")
77+
@ResponseBody
78+
public Student findRecordByContactNumber(@RequestParam String contact) {
79+
80+
final Student student = studentRepository.findByContact(contact);
81+
return student;
82+
}
83+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.javaoutofbounds.pojo;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface StudentRepository extends CrudRepository<Student, Integer> {
6+
7+
public Student findByContact(String contact);
8+
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.jpa.hibernate.ddl-auto=create
2+
spring.datasource.url=jdbc:mysql://localhost:3306/db_StudentRecords
3+
spring.datasource.username=root
4+
spring.datasource.password=admin
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javaoutofbounds.pojo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringbootMysqlExampleApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)