├── .gitignore ├── target └── classes │ ├── me │ └── salisuwy │ │ ├── Blog.class │ │ ├── BlogController.class │ │ ├── BlogRespository.class │ │ └── MainApplicationClass.class │ ├── application.properties │ └── database.sql ├── src └── main │ ├── resources │ ├── application.properties │ └── database.sql │ └── java │ └── me │ └── salisuwy │ ├── MainApplicationClass.java │ ├── BlogRespository.java │ ├── Blog.java │ └── BlogController.java ├── README.md ├── pom.xml └── SpringRest.iml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ -------------------------------------------------------------------------------- /target/classes/me/salisuwy/Blog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salisuwy/building-spring-boot-resp-api-v3/HEAD/target/classes/me/salisuwy/Blog.class -------------------------------------------------------------------------------- /target/classes/me/salisuwy/BlogController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salisuwy/building-spring-boot-resp-api-v3/HEAD/target/classes/me/salisuwy/BlogController.class -------------------------------------------------------------------------------- /target/classes/me/salisuwy/BlogRespository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salisuwy/building-spring-boot-resp-api-v3/HEAD/target/classes/me/salisuwy/BlogRespository.class -------------------------------------------------------------------------------- /target/classes/me/salisuwy/MainApplicationClass.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/salisuwy/building-spring-boot-resp-api-v3/HEAD/target/classes/me/salisuwy/MainApplicationClass.class -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.jpa.hibernate.ddl-auto=create 2 | spring.datasource.url=jdbc:mysql://localhost:3306/restapi 3 | spring.datasource.username=root 4 | spring.datasource.password= -------------------------------------------------------------------------------- /target/classes/application.properties: -------------------------------------------------------------------------------- 1 | #spring.jpa.hibernate.ddl-auto=create 2 | spring.datasource.url=jdbc:mysql://localhost:3306/restapi 3 | spring.datasource.username=root 4 | spring.datasource.password= -------------------------------------------------------------------------------- /src/main/resources/database.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE restapi; 2 | USE restapi; 3 | CREATE TABLE blog ( 4 | id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 5 | title VARCHAR(500) NOT NULL, 6 | content VARCHAR(5000) NOT NULL 7 | ); -------------------------------------------------------------------------------- /target/classes/database.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE restapi; 2 | USE restapi; 3 | CREATE TABLE blog ( 4 | id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 5 | title VARCHAR(500) NOT NULL, 6 | content VARCHAR(5000) NOT NULL 7 | ); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Building a String Boot Rest API v3. 2 | 3 | Tutorial https://medium.com/@salisuwy/building-a-spring-boot-rest-api-part-iii-integrating-mysql-database-and-jpa-81391404046a 4 | 5 | Check http://medium.com/@salisuwy 6 | -------------------------------------------------------------------------------- /src/main/java/me/salisuwy/MainApplicationClass.java: -------------------------------------------------------------------------------- 1 | package me.salisuwy; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MainApplicationClass { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MainApplicationClass.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/me/salisuwy/BlogRespository.java: -------------------------------------------------------------------------------- 1 | package me.salisuwy; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import java.util.List; 7 | 8 | @Repository 9 | public interface BlogRespository extends JpaRepository { 10 | 11 | List findByTitleContainingOrContentContaining(String text, String textAgain); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/me/salisuwy/Blog.java: -------------------------------------------------------------------------------- 1 | package me.salisuwy; 2 | 3 | import javax.persistence.*; 4 | 5 | @Entity 6 | public class Blog { 7 | @Id 8 | @GeneratedValue(strategy = GenerationType.AUTO) 9 | private int id; 10 | 11 | private String title; 12 | private String content; 13 | 14 | public Blog() { } 15 | 16 | public Blog(String title, String content) { 17 | this.setTitle(title); 18 | this.setContent(content); 19 | } 20 | 21 | public Blog(int id, String title, String content) { 22 | this.setId(id); 23 | this.setTitle(title); 24 | this.setContent(content); 25 | } 26 | 27 | public int getId() { 28 | return id; 29 | } 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getTitle() { 36 | return title; 37 | } 38 | 39 | public void setTitle(String title) { 40 | this.title = title; 41 | } 42 | 43 | public String getContent() { 44 | return content; 45 | } 46 | 47 | public void setContent(String content) { 48 | this.content = content; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "Blog{" + 54 | "id=" + id + 55 | ", title='" + title + '\'' + 56 | ", content='" + content + '\'' + 57 | '}'; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | me.salisuwy 8 | SpringRest 9 | 1.0-SNAPSHOT 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.5.9.RELEASE 15 | 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-data-jpa 28 | 29 | 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | 35 | 36 | 37 | 38 | 1.8 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/me/salisuwy/BlogController.java: -------------------------------------------------------------------------------- 1 | package me.salisuwy; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.*; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | @RestController 10 | public class BlogController { 11 | 12 | @Autowired 13 | BlogRespository blogRespository; 14 | 15 | @GetMapping("/blog") 16 | public List index(){ 17 | return blogRespository.findAll(); 18 | } 19 | 20 | @GetMapping("/blog/{id}") 21 | public Blog show(@PathVariable String id){ 22 | int blogId = Integer.parseInt(id); 23 | return blogRespository.findOne(blogId); 24 | } 25 | 26 | @PostMapping("/blog/search") 27 | public List search(@RequestBody Map body){ 28 | String searchTerm = body.get("text"); 29 | return blogRespository.findByTitleContainingOrContentContaining(searchTerm, searchTerm); 30 | } 31 | 32 | @PostMapping("/blog") 33 | public Blog create(@RequestBody Map body){ 34 | String title = body.get("title"); 35 | String content = body.get("content"); 36 | return blogRespository.save(new Blog(title, content)); 37 | } 38 | 39 | @PutMapping("/blog/{id}") 40 | public Blog update(@PathVariable String id, @RequestBody Map body){ 41 | int blogId = Integer.parseInt(id); 42 | // getting blog 43 | Blog blog = blogRespository.findOne(blogId); 44 | blog.setTitle(body.get("title")); 45 | blog.setContent(body.get("content")); 46 | return blogRespository.save(blog); 47 | } 48 | 49 | @DeleteMapping("blog/{id}") 50 | public boolean delete(@PathVariable String id){ 51 | int blogId = Integer.parseInt(id); 52 | blogRespository.delete(blogId); 53 | return true; 54 | } 55 | 56 | 57 | } -------------------------------------------------------------------------------- /SpringRest.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | --------------------------------------------------------------------------------