Article code for Spring boot with MongoDB
This commit is contained in:
BIN
Spring-Boot/spring-boot-mongodb/.DS_Store
vendored
Normal file
BIN
Spring-Boot/spring-boot-mongodb/.DS_Store
vendored
Normal file
Binary file not shown.
32
Spring-Boot/spring-boot-mongodb/.gitignore
vendored
Normal file
32
Spring-Boot/spring-boot-mongodb/.gitignore
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
HELP.md
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**
|
||||
!**/src/test/**
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
25
Spring-Boot/spring-boot-mongodb/build.gradle
Normal file
25
Spring-Boot/spring-boot-mongodb/build.gradle
Normal file
@@ -0,0 +1,25 @@
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.2.4.RELEASE'
|
||||
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'com.mongo.springbootmongodb'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '1.8'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
testImplementation('org.springframework.boot:spring-boot-starter-test') {
|
||||
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
|
||||
}
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/")
|
||||
public class EmployeeEndpoint {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public EmployeeEndpoint(EmployeeRepository userRepository) {
|
||||
this.employeeRepository = userRepository;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "", method = RequestMethod.GET)
|
||||
public List<EmployeeModel> getAllUsers() {
|
||||
logger.info("Getting all Employees.");
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public EmployeeModel getEmployee(@PathVariable long id) {
|
||||
logger.info("Getting Employee with ID: {}.", id);
|
||||
Optional<EmployeeModel> employeeModel = employeeRepository.findById(id);
|
||||
return employeeModel.get();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public EmployeeModel add(@RequestBody EmployeeModel employeeModel) {
|
||||
logger.info("Saving Employee.");
|
||||
return employeeRepository.save(employeeModel);
|
||||
}
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
|
||||
public void delete(@PathVariable Long id) {
|
||||
EmployeeModel model = employeeRepository.findById(id).get();
|
||||
logger.info("Deleting Employee.");
|
||||
employeeRepository.delete(model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/template")
|
||||
public class EmployeeEndpointUsingTemplate {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private final MongoTemplate mongoTemplate;
|
||||
|
||||
public EmployeeEndpointUsingTemplate(MongoTemplate mongoTemplate) {
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "", method = RequestMethod.GET)
|
||||
public List<EmployeeModel> getAllUsers() {
|
||||
logger.info("Getting all Employees.");
|
||||
return mongoTemplate.findAll(EmployeeModel.class);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
public EmployeeModel getEmployee(@PathVariable long id) {
|
||||
logger.info("Getting Employee with ID: {}.", id);
|
||||
EmployeeModel employeeModel = mongoTemplate.findById(id,EmployeeModel.class);
|
||||
return employeeModel;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/create", method = RequestMethod.POST)
|
||||
public EmployeeModel add(@RequestBody EmployeeModel employeeModel) {
|
||||
logger.info("Saving Employee.");
|
||||
return mongoTemplate.save(employeeModel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Document
|
||||
public class EmployeeModel {
|
||||
@Id
|
||||
private long id;
|
||||
private String name;
|
||||
private String address;
|
||||
private Date creationDate = new Date();
|
||||
|
||||
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 getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Date getCreationDate() {
|
||||
return creationDate;
|
||||
}
|
||||
|
||||
public void setCreationDate(Date creationDate) {
|
||||
this.creationDate = creationDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface EmployeeRepository extends MongoRepository<EmployeeModel, Long> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootMongodbApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootMongodbApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
spring.data.mongodb.database=employee_db
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.host=localhost
|
||||
|
||||
server.port=8080
|
||||
spring.application.name=Spring boot with MongoDB
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ayoosh.mongoboot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MongoBootApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.mongo.springbootmongodb;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringBootMongodbApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user