项目结构
下面直接进入代码操作
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
application.properties:
spring.data.mongodb.uri=mongodb://localhost:27017/mydb
User.class
package com.zyu.entity; /** * @program: SpringBoot-mongo * @description: * @author: zyu * @create: 2019-11-28 16:06 */ public class User { private String id; private String name; private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { final StringBuffer sb = new StringBuffer("User{"); sb.append("id='").append(id).append('\''); sb.append(", name='").append(name).append('\''); sb.append(", age=").append(age); sb.append('}'); return sb.toString(); } }
UserDao.class
package com.zyu.dao; import com.zyu.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Component; import java.util.List; /** * @program: SpringBoot-mongo * @description: * @author: zyu * @create: 2019-11-29 16:05 */ @Component public class UserDao { @Autowired private MongoTemplate mongoTemplate; /** * 创建对象 * @param user */ public User saveUser(User user) { User save = mongoTemplate.save(user); return save; } /** * 更新对象 * @param user */ public void updateUser(User user) { Query query = new Query(Criteria.where("id").is(user.getId())); Update update = new Update().set("name", user.getName()).set("age", user.getAge()); mongoTemplate.updateFirst(query, update, User.class); } /** * 根据用户名查询对象 * @param name * @return */ public User findUserByName(String name) { Query query = new Query(Criteria.where("name").is(name)); User user = mongoTemplate.findOne(query, User.class); return user; } /** * 查询所有对象 * @return */ public List<User> findUserList() { List<User> all = mongoTemplate.findAll(User.class); return all; } /** * 删除对象 * @param id */ public void deleteUserById(String id) { Query query = new Query(Criteria.where("id").is(id)); mongoTemplate.remove(query, User.class); } }
HomeController.class
package com.zyu.controller; import com.zyu.dao.UserDao; import com.zyu.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @program: SpringBoot-mongo * @description: * @author: zyu * @create: 2019-11-28 16:08 */ @RestController public class HomeController { @Autowired private UserDao userDao; @GetMapping("/findList") public List<User> findUserList() { List<User> userList = userDao.findUserList(); return userList; } @GetMapping("/findByName") public User findUserByName(@RequestParam String name) { User userByName = userDao.findUserByName(name); return userByName; } @GetMapping("/save") public User saveUser(@RequestParam String name, @RequestParam Integer age) { User user = new User(); user.setName(name); user.setAge(age); return userDao.saveUser(user); } @GetMapping("/update") public String updateUser(@RequestParam String id, @RequestParam String name, @RequestParam Integer age) { User user = new User(); user.setName(name); user.setAge(age); user.setId(id); userDao.updateUser(user); return "Success"; } }
下面我们启动程序进行测试
1. 新增: http://localhost:8080/save?name=张三&age=20
可以看到新增成功
2. 查询:http://localhost:8080/findList
可以看到数据插入成功
3. 修改:http://localhost:8080/update?name=张三&age=22&id=5de0dbee2a21b94db4fc367b
其他接口大家自己去测试吧