SpringBoot Example 1
SpringBoot Example 1
SpringBoot Example 1
Example 1:
FirstStsProjectApplication.java
package com.cit.FirstSTSProject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstStsProjectApplication {
FirstController.java
package com.cit.FirstSTSProject.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class FirstController {
@RequestMapping("/test")
@ResponseBody
public String firstHandller() {
return "\n\n...This is Spring Boot using STS...\n\n";
}
}
Example 2:
FirstStsProjectforJspApplication .java
package com.cit.FirstSTSProject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstStsProjectforJspApplication {
MainController.java
package com.cit.FirstSTSProject.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MainController {
@RequestMapping("/home")
public String view() {
System.out.println("This is home page");
return "home";
}
@RequestMapping("/contact")
public String contact() {
System.out.println("This is contact page");
return "contact";
}
}
application.properties
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp
contact.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1> This is Contact page</h1>
</body>
</html>
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
Example 3
User.java
package com.cit.SpringBootJPAExample.entites;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", city=" + city + ", status=" + status
+ "]";
}
UserRepository.java
package com.cit.SpringBootJPAExample.Dao;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import com.cit.SpringBootJPAExample.entites.User;
//parameterize query
@Query("select u from User u where u.name =:n")
public List<User> getAllUserByName(@Param("n") String name);
SpringBootJpaExampleApplication.java
package com.cit.SpringBootJPAExample;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.cit.SpringBootJPAExample.Dao.UserRepository;
import com.cit.SpringBootJPAExample.entites.User;
@SpringBootApplication
public class SpringBootJpaExampleApplication {
ApplicationContext context =
SpringApplication.run(SpringBootJpaExampleApplication.class, args);
/*
* User user = new User(); user.setName("Shreya"); user.setCity("Kolkata");
* user.setStatus("It is Python"); User userResult = userRepository.save(user);
* System.out.println(userResult);
*/
// UPDATE
/*
* Optional<User> optinal = userRepository.findById(10); User user =
* optinal.get();
*
* user.setName("Trang"); user.setCity("Hanoi"); user.setStatus("It is Python");
* User userResult = userRepository.save(user); System.out.println(userResult);
*/
// READ
// findById() - returns optional containing your data
/*
* Iterator<User> iterator = itr.iterator(); while(iterator.hasNext()) { User
* user = iterator.next(); System.out.println(user); }
*/
/*
* itr.forEach(new Consumer<User>() {
*
* @Override public void accept(User t) { System.out.println(t); }
*
* });
*/
// itr.forEach(user->{System.out.println(user);});
// DELETE
//parameterize query
/*
* List<User> allUser = userRepository.getAllUserByName("Shreya");
* allUser.forEach(user->System.out.println(user));
*/
application.properties
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://localhost:3306/debarshi
spring.datasource.username=debarshi
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
Example 4:
Book.java
package com.cit.SpringBootRestAPIFirst.entities;
public Book() {
super();
// TODO Auto-generated constructor stub
}
BookController.java
package com.cit.SpringBootRestAPIFirst.Contollers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cit.SpringBootRestAPIFirst.Services.BookService;
import com.cit.SpringBootRestAPIFirst.entities.Book;
/*@Controller*/
@RestController
public class BookController {
/*
* @GetMapping("/books") public Book getBooks() {
*
* Book book=new Book(); book.setBookId(101);
* book.setTitle("Advance Java Programming");
* book.setAuthor("Debarshi Mazumder");
*
* return book; }
*/
@Autowired
private BookService bookService;
return this.bookService.getAllBooks();
}
return this.bookService.getBookById(id);
}
this.bookService.deleteBook(id);
}
//Updating book
@PutMapping("/books/{id}")
public Book updateBook(@RequestBody Book book, @PathVariable("id") int id) {
this.bookService.updateBook(book, id);
System.out.println(book);
return book;
}
}
BookService.java
package com.cit.SpringBootRestAPIFirst.Services;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cit.SpringBootRestAPIFirst.Dao.BookRepository;
import com.cit.SpringBootRestAPIFirst.entities.Book;
@Component
public class BookService {
//@Autowired
//private BookRepository bookRepository;
static {
list.add(new Book(102, "C++", "Hieu"));
list.add(new Book(103, "C#", "ABC"));
list.add(new Book(104, ".Net", "XYZ"));
/*
* list = list.stream().filter(book->{ if(book.getBookId()!=id) { return true; }
* else { return false; }
*
* }).collect(Collectors.toList());
*/
//this.bookRepository.deleteById(id);
}
SpringBootRestApiFirstApplication.java
package com.cit.SpringBootRestAPIFirst;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestApiFirstApplication {
application.properties
spring.datasource.name=test
spring.datasource.url=jdbc:mysql://localhost:3306/debarshi
spring.datasource.username=debarshi
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update