1.先引入pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2.配置文件application.yml
spring: data: mongodb: uri: mongodb://IP*******:27017/库名
3.创建entity实体类
import lombok.Data; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; import java.io.Serializable; @Data @Document(collection = "user") public class User implements Serializable { private static final Long serialVersionUID = 1l; @Field("id") private String id; 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 String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAdd() { return add; } public void setAdd(String add) { this.add = add; } @Field("name") private String name; @Field("phone") private String phone; @Field("add") private String add; }
4.service层
import java.util.HashMap; public interface IUserService { HashMap<Object, Object> queryList(); User findUserByUserName(String name); long updateUser(User user); boolean deleteUserById(String id); String saveUser(User user); }
5.实现impl
import com.mongodb.client.result.UpdateResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.Service; import cn.javaguide.springbootkafka01sendobjects.service.IUserService; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; @Service public class UserServiceImpl implements IUserService { private static Logger logger = LoggerFactory.getLogger(UserContrller.class); @Resource private MongoTemplate mongoTemplate; /** * 时间 2022年3月1日 * 查询user表所有集合 * * @return */ @Override public HashMap<Object, Object> queryList() { HashMap<Object, Object> map = new HashMap<>(); Query query = new Query(); List<User> all = mongoTemplate.findAll(User.class); // List<AlarmRtTime> all1 = mongoTemplate.findAll(AlarmRtTime.class); long count = mongoTemplate.count(query, User.class); // boolean empty = all.isEmpty(); // Assert.isTrue(empty==false,"***********************查询信息为空***********************"); // Assert.isNull(all,"***********************查询信息为空***********************"); User user = new User(); if (all.isEmpty()) { // user.setId("001"); // user.setName("tom"); // user.setPhone("1234568910"); System.out.println("***********************查询信息为空***********************"); // all.add(user); } map.put("user table info :", all); map.put("user table count :", count); // map.put("user table AlarmRtTime :", all1); return map; } @Override public String saveUser(User user) { mongoTemplate.save(user); return "添加成功"; } /** * 根据name查询mongodb信息 * date 2022年3月1日 15点19分 * * @param name * @return */ @Override public User findUserByUserName(String name) { Query query = new Query(Criteria.where("name").is(name)); User user = mongoTemplate.findOne(query, User.class); // Assert.isNull(user,"***********************查询信息为空***********************"); return user; } /** * 根据id,修改用户信息 * 时间 2022年3月1日 * * @param user */ @Override public long updateUser(User user) { System.out.println(user); Query query = new Query(Criteria.where("id").is(user.getId())); Update update = new Update().set("name", user.getName()).set("phone", user.getPhone()).set("add", user.getAdd()); //更新查询返回结果集的第一条 UpdateResult result = mongoTemplate.updateFirst(query, update, User.class); //更新查询返回结果集的所有 // mongoTemplate.updateMulti(query,update,UserEntity.class); if (result != null) { return result.getMatchedCount(); } else { return 0; } } /** * 根据id删除对象 * 时间 2022年3月1日 * @param id */ @Override public boolean deleteUserById(String id) { Query query=new Query(Criteria.where("id").is(id)); User user = mongoTemplate.findOne(query, User.class); System.out.println(user); // Assert.notNull(user,"找不到对应id数据"); if (user==null){ System.out.println("找不到对应id数据"); return false; } mongoTemplate.remove(query,User.class); return true; } }
6.接口controller
import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 cn.javaguide.springbootkafka01sendobjects.service.IUserService; import javax.annotation.Resource; import java.util.HashMap; @RestController @RequestMapping("/user") public class UserContrller { private static Logger logger = LoggerFactory.getLogger(UserContrller.class); @Resource private IUserService userService; @PostMapping("/getInfo") private HashMap<Object, Object> getAllInfo() { return userService.queryList(); } @PostMapping("/findUserByUserName") private User findUserByUserName(String name) { return userService.findUserByUserName(name); } @PostMapping("/updateUser") private long updateUser(@RequestBody User user) { return userService.updateUser(user); } @PostMapping("/deleteUserById") private boolean deleteUserById(String id) { return userService.deleteUserById(id); } @PostMapping("/saveUser") private String saveUser(@RequestBody User user) { return userService.saveUser(user); } }
7.接口测试
--添加
--查询全部
****测试不一一截图了
建议配置下账号密码,开放性数据库容易被攻击,数据不安全。