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
- 【遠端辦公】5分鐘實現內網穿透內網穿透
- spring原始碼閱讀環境(幾分鐘下載包)Spring原始碼
- 如何使用Spring Boot,Spring Data和H2 DB實現REST APISpring BootRESTAPI
- 使用Docker實現Spring Boot Restful Web服務案例原始碼DockerSpring BootRESTWeb原始碼
- 精盡Spring Boot原始碼分析 - 內嵌Tomcat容器的實現Spring Boot原始碼Tomcat
- 使用Spring Boot實現檔案上傳功能Spring Boot
- Spring Boot(十一):Spring Boot 中 MongoDB 的使用Spring BootMongoDB
- 在spring boot中3分鐘上手RPC框架DubboSpring BootRPC框架
- Spring Boot使用過濾器和攔截器分別實現REST介面簡易安全認證Spring Boot過濾器REST
- 三分鐘實戰手寫Spring Boot StarterSpring Boot
- SpringBoot實現mongoDB的CRUDSpring BootMongoDB
- Spring Boot + JPA實現MySQL批量更新原始碼 - githubSpring BootMySql原始碼Github
- Spring Boot中實現規則引擎原始碼教程Spring Boot原始碼
- 使用go在mongodb中進行CRUD操作MongoDB
- Spring Boot 使用NoSQL之 MongoDBSpring BootSQLMongoDB
- Spring-Boot:5分鐘掌握SpringBoot開發Spring Boot
- 5 分鐘,使用內網穿透快速實現遠端桌面內網穿透
- 五分鐘實現一個chrome外掛(含原始碼)Chrome原始碼
- 使用JBang構建Spring Boot Rest API教程Spring BootRESTAPI
- 使用Spring Boot REST API進行測試驅動開發Spring BootRESTAPI
- 用白碼,5分鐘即可實現定時微信通知功能
- spring原始碼解析:元註解功能的實現Spring原始碼
- Spring Boot功能實戰Spring Boot
- 文章內容分頁功能實現
- spring boot碼雲原始碼Spring Boot原始碼
- 使用Spring Boot實現模組化Spring Boot
- Spring Boot系列(四):Spring Boot原始碼解析Spring Boot原始碼
- SpringMVC聯手REST實現入門級的CRUDSpringMVCREST
- Spring Boot+MiniUI CRUD操作Spring BootUI
- 3分鐘內實現人臉檢測
- 5分鐘用Spring4 搭建一個REST WebServiceSpringRESTWeb
- 使用Kubernetes,Spring Boot和Flyway實現零停機部署原始碼專案Spring Boot原始碼
- 使用Spring Boot + Kafka實現Saga分散式事務模式的原始碼 - vinsguruSpring BootKafka分散式模式原始碼
- Spring Boot實現DDD的貨運Cargo微服務案例原始碼Spring BootCargo微服務原始碼
- 精盡Spring Boot原始碼分析 - Jar 包的啟動實現Spring Boot原始碼JAR