Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

boot-rest-multiple-DB

pom.xml

4.0.0com.candidjava.spring
	boot-rest-multiple-DB
	0.0.1-SNAPSHOTjarDemo project for Spring Bootorg.springframework.boot
		spring-boot-starter-parent
		2.0.5.RELEASEUTF-8UTF-81.8org.springframework.boot
			spring-boot-starter-data-jpa
		org.springframework.boot
			spring-boot-starter-web
		org.springframework.boot
			spring-boot-devtools
			runtimemysql
			mysql-connector-java
			runtimeorg.springframework.boot
			spring-boot-starter-data-mongodb
		org.springframework.boot
				spring-boot-maven-plugin
			boot-rest-multiple-DB

application.properties

spring.jpa.database=default
# My SQL Database
spring.datasource.driver = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3309/test3
spring.datasource.username = root
spring.datasource.password = root

#Mongo Db
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=test5

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Using MongoDB

CourseRepository.java

package com.candidjava.spring.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.candidjava.spring.bean.Course;

@Repository
public interface CourseRepository extends MongoRepository {


}

Course.java

package com.candidjava.spring.bean;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "course")
public class Course {
 @Id
 private String id;

 private String name;

 private String country;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}

CourseController.java

package com.candidjava.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.candidjava.spring.bean.Course;
import com.candidjava.spring.service.CourseService;

@RestController
@RequestMapping(value = {
 "/course"
})
public class CourseController {
 @Autowired
 CourseService courseService;

 @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity getcourseById(@PathVariable("id") long id) {
  System.out.println("Fetching course with id " + id);
  Optional optcourse = courseService.findById(id);
  Course course = optcourse.get();
  if (course == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  return new ResponseEntity (course, HttpStatus.OK);
 }

 @PostMapping(value = "/create", headers = "Accept=application/json")
 public ResponseEntity createcourse(@RequestBody Course course, UriComponentsBuilder ucBuilder) {
  System.out.println("Creating course " + course.getName());
  courseService.createCourse(course);
  HttpHeaders headers = new HttpHeaders();
  headers.setLocation(ucBuilder.path("/course/{id}").buildAndExpand(course.getId()).toUri());
  return new ResponseEntity (headers, HttpStatus.CREATED);
 }

 @GetMapping(value = "/get", headers = "Accept=application/json")
 public List getAllcourse() {
  List tasks = courseService.getCourse();
  return tasks;

 }

 @DeleteMapping(value = "/{id}", headers = "Accept=application/json")
 public ResponseEntity deletecourse(@PathVariable("id") long id) {
  Optional optcourse = courseService.findById(id);
  Course course = optcourse.get();
  if (course == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  courseService.deleteCourseById(id);
  return new ResponseEntity (HttpStatus.NO_CONTENT);
 }

 @PatchMapping(value = "/{id}", headers = "Accept=application/json")
 public ResponseEntity updatecoursePartially(@PathVariable("id") long id,
  @RequestBody Course currentcourse) {
  Optional optcourse = courseService.findById(id);
  Course course = optcourse.get();
  if (course == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  Course usr = courseService.updatePartially(currentcourse, id);
  return new ResponseEntity (usr, HttpStatus.OK);
 }
}

CourseService.java

package com.candidjava.spring.service;

import java.util.List;
import java.util.Optional;

import com.candidjava.spring.bean.Course;


public interface CourseService {
 public void createCourse(Course course);
 public List getCourse();
 public Optional findById(long id);
 public Course update(Course course, long l);
 public void deleteCourseById(long id);
 public Course updatePartially(Course course, long id);
}

CourseServiceImp.java

package com.candidjava.spring.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.candidjava.spring.bean.Course;
import com.candidjava.spring.repository.CourseRepository;
import com.candidjava.spring.repository.UserRepository;


@Service
@Transactional
public class CourseServiceImp implements CourseService {
 @Autowired
 CourseRepository courseRepository;

 public void createCourse(Course course) {
  courseRepository.save(course);
 }

 public List getCourse() {
  return (List ) courseRepository.findAll();
 }

 public Optional findById(long id) {
  return courseRepository.findById(id);

 }

 public Course update(Course course, long l) {
  return courseRepository.save(course);
 }

 public void deleteCourseById(long id) {
  courseRepository.deleteById(id);
 }

 public Course updatePartially(Course user, long id) {
  Optional usr = findById(id);
  Course newuser = usr.get();
  return courseRepository.save(newuser);
 }

Using My SQL Database

UserRepository.java

package com.candidjava.spring.repository;

import org.springframework.data.repository.CrudRepository;

import com.candidjava.spring.bean.User;

public interface UserRepository extends CrudRepository {


}

User.java

package com.candidjava.spring.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@Entity
@Table(name = "UserInfo")
@JsonIgnoreProperties({
 "hibernateLazyInitializer",
 "handler"
})
public class User {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private long id;

 @Column(name = "country")
 private String country;
 @Column(name = "name")

 private String name;


 public long getId() {
  return id;
 }

 public void setId(long id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

}

UserController.java

package com.candidjava.spring.controller;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.candidjava.spring.bean.User;
import com.candidjava.spring.service.UserService;


@RestController
@RequestMapping(value = {
 "/user"
})
public class UserController {
 @Autowired
 UserService userService;

 @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity getUserById(@PathVariable("id") long id) {
  System.out.println("Fetching User with id " + id);
  Optional optuser = userService.findById(id);
  User user = optuser.get();
  if (user == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  return new ResponseEntity (user, HttpStatus.OK);
 }

 @PostMapping(value = "/create", headers = "Accept=application/json")
 public ResponseEntity createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
  System.out.println("Creating User " + user.getName());
  userService.createUser(user);
  HttpHeaders headers = new HttpHeaders();
  headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
  return new ResponseEntity (headers, HttpStatus.CREATED);
 }

 @GetMapping(value = "/get", headers = "Accept=application/json")
 public List getAllUser() {
  List tasks = userService.getUser();
  return tasks;

 }

 @PutMapping(value = "/update", headers = "Accept=application/json")
 public ResponseEntity updateUser(@RequestBody User currentUser) {
  System.out.println("sd");
  Optional optuser = userService.findById(currentUser.getId());
  User user = optuser.get();
  if (user == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  userService.update(currentUser, currentUser.getId());
  return new ResponseEntity (HttpStatus.OK);
 }

 @DeleteMapping(value = "/{id}", headers = "Accept=application/json")
 public ResponseEntity deleteUser(@PathVariable("id") long id) {
  Optional optuser = userService.findById(id);
  User user = optuser.get();
  if (user == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  userService.deleteUserById(id);
  return new ResponseEntity (HttpStatus.NO_CONTENT);
 }

 @PatchMapping(value = "/{id}", headers = "Accept=application/json")
 public ResponseEntity updateUserPartially(@PathVariable("id") long id, @RequestBody User currentUser) {
  Optional optuser = userService.findById(id);
  User user = optuser.get();
  if (user == null) {
   return new ResponseEntity (HttpStatus.NOT_FOUND);
  }
  User usr = userService.updatePartially(currentUser, id);
  return new ResponseEntity (usr, HttpStatus.OK);
 }
}

UserService.java

package com.candidjava.spring.service;

import java.util.List;
import java.util.Optional;

import com.candidjava.spring.bean.User;


public interface UserService {
 public void createUser(User user);
 public List getUser();
 public Optional findById(long id);
 public User update(User user, long l);
 public void deleteUserById(long id);
 public User updatePartially(User user, long id);
}

UserServiceImp.java

package com.candidjava.spring.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.candidjava.spring.bean.User;
import com.candidjava.spring.repository.UserRepository;

@Service
@Transactional
public class UserServiceImp implements UserService {
	@Autowired
	UserRepository userRepository;

	public void createUser(User user) {
		userRepository.save(user);
	}

	public List getUser() {
		return (List) userRepository.findAll();
	}

	public Optional findById(long id) {
		return userRepository.findById(id);

	}

	public User update(User user, long l) {
		return userRepository.save(user);
	}

	public void deleteUserById(long id) {
		userRepository.deleteById(id);
	}

	public User updatePartially(User user, long id) {
		Optional usr = findById(id);
		User newuser = usr.get();
		newuser.setCountry(user.getCountry());
		return userRepository.save(newuser);
	}

}

Application.java

package com.candidjava.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  SpringApplication.run(Application.class, args);
 }
}


This post first appeared on Core Java,Servlet,Jsp,Struts,Hibernate,Spring Fram, please read the originial post: here

Share the post

boot-rest-multiple-DB

×

Subscribe to Core Java,servlet,jsp,struts,hibernate,spring Fram

Get updates delivered right to your inbox!

Thank you for your subscription

×