Spring Boot原始碼:使用MongoDB MongoTemplate公開REST在幾分鐘內實現CRUD功能
原始碼點選標題進入。
業務失血模型User程式碼:
import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Getter @Setter @Document public class User { @Id private String userId; private int rollno; private String name; private int mobno; private Address address; //Using Lombok for getter and setter } |
服務介面:
public interface UserService { List<User> getAllUsers(); User getUserById(int userId); User addNewUser(User user); UpdateResult updateUser(User user); User deleteUser(int usrId); } |
REST控制器:
@RestController public class ApiController { @Autowired private UserService userService; // This method Save user in Database @PostMapping(value = "/adduser") public User addUser(@RequestBody User user) { if (user!=null) { return userService.addNewUser(user); } else { throw new RuntimeException("Please provide the user detail to create new user"); } } // This method get user from Database for specific ID @RequestMapping(value = "/getuser/{userId}") public User getUserById(@PathVariable("userId") String userId) { int usrId = 0; usrId = Integer.valueOf(userId); User user = userService.getUserById(usrId); if (user!=null){ return userService.getUserById(usrId); } else throw new RuntimeException("No user found with userId: " + userId); } // This method get all user from Database @GetMapping(value = "/getallusers") public List<User> getAllUser() { return userService.getAllUsers(); } // This method update user in Database @PutMapping(value = "/updateuser") public UpdateResult updateUser(@RequestBody User user) { return userService.updateUser(user); } // This method d delete user from Database @DeleteMapping(value = "/deleteuser/{userId}") public User updateUser(@PathVariable("userId") String userId) { int usrId = 0; usrId = Integer.valueOf(userId); return userService.deleteUser(usrId); } } |
服務實現:
@Service public class UserServiceImpl implements UserService { @Autowired private MongoTemplate mongoTemplate; @Override public List<User> getAllUsers() { return mongoTemplate.findAll(User.class); } @Override public User getUserById(int rollno) throws RuntimeException{ Query query = new Query(); query.addCriteria(Criteria.where("rollno").is(rollno)); User user = mongoTemplate.findOne(query, User.class); if (user!=null){ return user; } else { throw new UserNotFoundException("User not found"); } } @Override public User addNewUser(User user) { Assert.notNull(user, "Object to save must not be null!"); return mongoTemplate.save(user); } @Override public UpdateResult updateUser(User user) { Query query = new Query(); query.addCriteria(Criteria.where("rollno").is(user.getRollno())); Document doc = new Document(); // org.bson.Document mongoTemplate.getConverter().write(user, doc); return mongoTemplate.upsert(query, Update.fromDocument(doc), User.class); } @Override public User deleteUser(int usrId) { Query query = new Query(); query.addCriteria(Criteria.where("rollno").is(usrId)); return mongoTemplate.findAndRemove(query, User.class); } } |
相關文章
- 使用Spring Boot反應式R2DBC實現PostgreSQL的CRUD操作原始碼 - RajeshSpring BootSQL原始碼
- Spring Boot之搞定mongoTemplateSpring BootGo
- Spring Boot入門系列(十六)使用pagehelper實現分頁功能Spring Boot
- 如何使用Spring Boot,Spring Data和H2 DB實現REST APISpring BootRESTAPI
- 精盡Spring Boot原始碼分析 - 內嵌Tomcat容器的實現Spring Boot原始碼Tomcat
- spring原始碼閱讀環境(幾分鐘下載包)Spring原始碼
- 使用Docker實現Spring Boot Restful Web服務案例原始碼DockerSpring BootRESTWeb原始碼
- 使用Spring Boot實現檔案上傳功能Spring Boot
- SpringBoot實現mongoDB的CRUDSpring BootMongoDB
- 【遠端辦公】5分鐘實現內網穿透內網穿透
- 在spring boot中3分鐘上手RPC框架DubboSpring BootRPC框架
- 基於 MongoTemplate 實現MongoDB的複雜查詢MongoDB
- Spring Boot使用過濾器和攔截器分別實現REST介面簡易安全認證Spring Boot過濾器REST
- Spring Boot + JPA實現MySQL批量更新原始碼 - githubSpring BootMySql原始碼Github
- Spring Boot中實現規則引擎原始碼教程Spring Boot原始碼
- 三分鐘實戰手寫Spring Boot StarterSpring Boot
- 使用go在mongodb中進行CRUD操作MongoDB
- 使用Kubernetes,Spring Boot和Flyway實現零停機部署原始碼專案Spring Boot原始碼
- 使用Spring Boot + Kafka實現Saga分散式事務模式的原始碼 - vinsguruSpring BootKafka分散式模式原始碼
- 使用JBang構建Spring Boot Rest API教程Spring BootRESTAPI
- Spring Boot+MiniUI CRUD操作Spring BootUI
- spring原始碼解析:元註解功能的實現Spring原始碼
- 使用Spring Boot REST API進行測試驅動開發Spring BootRESTAPI
- Spring Boot系列(四):Spring Boot原始碼解析Spring Boot原始碼
- Spring Boot功能實戰Spring Boot
- 精盡Spring Boot原始碼分析 - @ConfigurationProperties 註解的實現Spring Boot原始碼
- SpringBoot整合Redis使用Restful風格實現CRUD功能Spring BootRedisREST
- springboot(十一):Spring boot中mongodb的使用Spring BootMongoDB
- 使用Elasticsearch實現Spring Boot的自動完成功能 -Milos BiljanovicElasticsearchSpring Boot
- 5 分鐘,使用內網穿透快速實現遠端桌面內網穿透
- 五分鐘實現一個chrome外掛(含原始碼)Chrome原始碼
- Spring Boot實現DDD的貨運Cargo微服務案例原始碼Spring BootCargo微服務原始碼
- 精盡Spring Boot原始碼分析 - Jar 包的啟動實現Spring Boot原始碼JAR
- 使用Spring Boot實現的GraphQL示例Spring Boot
- 使用Spring Boot實現模組化Spring Boot
- 使用Spring Boot實現事務管理Spring Boot
- MongoDB 入門教程系列之二:使用 Spring Boot 操作 MongoDBMongoDBSpring Boot
- java版Spring Cloud+Spring Boot+Mybatis實現工程管理系統原始碼JavaCloudSpring BootMyBatis原始碼