SpringBoot | 第三十一章:MongoDB的集成和使用 (2)

2.配置文件中填写连接地址。

#mongodb # 单机模式 mongodb://name:pass@ip:port/database # 集群模式 mongodb://user:pwd@ip1:port1,ip2:port2/database spring.data.mongodb.uri=mongodb://127.0.0.1:27017/learning

注意,这里填写格式

单机模式:mongodb://name:pwd@ip:port/database

集群模式:mongodb://name:pwd@ip1:port1,ip2:port2/database

3.启动类编写。

/** * mongodb 集成示例 * @author oKong * */ @SpringBootApplication @EnableMongoAuditing //@EnableMongoRepositories(basePackages="cn.lqdev")//当有些dao不在default page下时 可通过此方法进行注册扫描包 @Slf4j public class MongodbApplication { public static void main(String[] args) throws Exception { SpringApplication.run(MongodbApplication.class, args); log.info("spring-boot-mongodb-chapter31启动!"); } }

注意:当使用MongoRepositories时,可能有些MongoRepositories类不在默认的包路径(启动类路径)下,可使用@EnableMongoRepositories的basePackages需要扫描的路径信息。若都在默认的包路径下,可以不加此注解的。

MongoTemplate方式

以下示例下MongoTemplate方式进行数据操作。

0.创建一个接口类:

/** * 接口服务 * @author oKong * */ public interface NotifyMsgService { /** * 保存数据 * @author 作者:oKong */ NotifyMsg saveNotifyMsg(NotifyMsg msg); /** * 根据消息号查找 * @author 作者:oKong */ NotifyMsg findNotifyMsgByNo(String notifyNo); /** * 根据消息日期查找 * @author 作者:oKong */ List<NotifyMsg> findNotifyMsgByDate(String notifyDate); /** * 根据id进行删除 返回删除的对象 * @author 作者:oKong */ NotifyMsg delNotifyMsgById(String id); }

1.接口实现类,引入MongoTemplate。

/** * MongoTemplate 访问实现 * @author oKong * */ @Service public class NotifyMsgServiceImpl implements NotifyMsgService{ @Autowired MongoTemplate mongoTemplate; @Override public NotifyMsg saveNotifyMsg(NotifyMsg msg) { //使用 save和insert都可以进行插入 //区别:当存在"_id"时 //insert 插入已经存在的id时 会异常 //save 则会进行更新 //简单来说 save 就是不存在插入 存在更新 mongoTemplate.insert(msg); mongoTemplate.save(msg); return msg; } @Override public NotifyMsg findNotifyMsgByNo(String notifyNo) { //根据Criteria 改造查询条件 Query query = new Query(Criteria.where("notifyNo").is(notifyNo)); return mongoTemplate.findOne(query, NotifyMsg.class); } @Override public List<NotifyMsg> findNotifyMsgByDate(String notifyDate) { //查找 notifyDate 根据Criteria 改造查询条件 Query query = new Query(Criteria.where("notifyDate").is(notifyDate)); return mongoTemplate.find(query, NotifyMsg.class); } @Override public NotifyMsg delNotifyMsgById(String id) { //查找 id 根据Criteria 改造查询条件 Query query = new Query(Criteria.where("id").is(id)); return mongoTemplate.findAndRemove(query, NotifyMsg.class); } }

针对查询,可以使用org.springframework.data.mongodb.core.query.Criteria对象进行构造查询条件。其提供的方法如下:

Criteria

具体使用时,可根据实际情况进行组合查询条件。

针对save和insert也需要注意下:

在无_id情况下,两者都能进行新增操作。

存在_id,同时记录库里不存在,两者都是进行插入操作。

存在_id,同时库里也存在记录,save相当于进行更新操作。而insert直接就异常了。

2.创建示例控制层。

/** * mongoTemplate 示例 * @author oKong * */ @RestController @RequestMapping("/template") @Slf4j public class MongoTemplateController { @Autowired NotifyMsgService notifyMsgService; @PostMapping("/add") public NotifyMsg add(NotifyMsg msg) { log.info("mongoTemplate方式新增:{}", msg); return notifyMsgService.saveNotifyMsg(msg); } @PostMapping("del/{id}") public NotifyMsg del(@PathVariable String id) { log.info("mongoTemplate方式删除:{}", id); return notifyMsgService.delNotifyMsgById(id); } @GetMapping("/find/{no}") public NotifyMsg findNotifyMsgByNo(@PathVariable String no){ log.info("mongoTemplate方式查找:notifyNo-{}", no); return notifyMsgService.findNotifyMsgByNo(no); } @GetMapping("/find/list/{date}") public List<NotifyMsg> findNotifyMsgByDate(@PathVariable String date){ log.info("mongoTemplate方式查找:notifyDate-{}", date); return notifyMsgService.findNotifyMsgByDate(date); } }

3.启动应用,使用Postman进行访问。

新增::8080//template/add

add

可以看见,已经返回对应的信息,同时数据库也可看见记录了。

SpringBoot | 第三十一章:MongoDB的集成和使用

其他的都是类似的,大家可自行访问下:

根据日期返回列表信息::8080//template/find/list/具体日期

根据ID删除::8080/template/del/具体ID值

根据消息号查询::8080//template/find/具体消息号

MongoRepository方式

以上还需要自己编写一些类啥的,现在是jpa的方式进行操作数据,优雅,简单。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyzgpf.html