Project Structure:
Post.java
package com.deepsingh44.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "posts")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String title;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
PostDao.java
package com.deepsingh44.demo.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.deepsingh44.demo.model.Post;
@Repository
public interface PostDao extends JpaRepository<Post, Integer> {
}
HelloController.java
package com.deepsingh44.demo.controller;
import java.util.List;
import java.util.Optional;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.deepsingh44.demo.dao.PostDao;
import com.deepsingh44.demo.model.Post;
@RestController
public class HelloController {
@Autowired
private PostDao postDao;
@PostMapping("/addpost")
public String hello(@RequestBody Post post) {
postDao.save(post);
return "Successfully Added";
}
@GetMapping("/posts")
public List<Post> getAllPost() {
return postDao.findAll();
}
@GetMapping("/find/{id}")
public Post getPostById(@PathVariable int id) {
Optional<Post> opt = postDao.findById(id);
return (!opt.isEmpty()) ? opt.get() : new Post();
}
@DeleteMapping("/delete")
public String deletePostById(@RequestParam int id) {
String msg = "Not Deleted";
Post post = getPostById(id);
if (post.getId() != 0) {
postDao.delete(post);
msg = "Successfully Deleted";
}
return msg;
}
@PutMapping("/update")
public Post updatePost(@RequestBody Post mypost) {
Post post = getPostById(mypost.getId());
return (post.getId() == 0) ? post : postDao.save(mypost);
}
}
WeekendHelloWorldApplication.java
package com.deepsingh44.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WeekendHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(WeekendHelloWorldApplication.class, args);
}
}
application.properties
server.port=8081
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/bootdb
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Add Post Screen
Show All Post Screen
Find Post Screen
Delete Post Screen
Update Post Screen
No comments:
Post a Comment