springboot整合MongoDB(简单demo实现包含注意点及踩坑)

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: springboot整合MongoDB(简单demo实现包含注意点及踩坑)

1️⃣:加maven坐标

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2️⃣:配置类实现

package com.todoitbo.tallybookdasmart.config;
import lombok.NonNull;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import javax.annotation.Resource;
/**
 * @author xiaobo
 * @date 2022/12/5
 * @info 此类若不加,那么插入的一行会默认添加一个_class字段来存储实体类类型 如(com.example.demo.entity.Student)
 */
@Configuration
public class MongoDbListener implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    MongoTemplate mongoTemplate;
    private static final String TYPEKEY = "_class";
    @Override
    public void onApplicationEvent(@NonNull ContextRefreshedEvent contextRefreshedEvent) {
        MongoConverter converter = mongoTemplate.getConverter();
        if (converter.getTypeMapper().isTypeKey(TYPEKEY)) {
            ((MappingMongoConverter) converter).setTypeMapper(new DefaultMongoTypeMapper(null));
        }
    }
}

3️⃣:实体类

package com.todoitbo.tallybookdasmart.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
/**
 * @author xiaobo
 * @date 2022/12/5
 */
@Data
@Document(collection = "sys_exception_log")
public class SysExceptionLog implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 主键
     */
    /*** 自定义mongo主键 加此注解可自定义主键类型以及自定义自增规则
     *  若不加 插入数据数会默认生成 ObjectId 类型的_id 字段
     *  org.springframework.data.annotation.Id 包下
     *  mongo库主键字段还是为_id (本文实体类中字段为为id,意思查询字段为_id,但查询结果_id会映射到实体对象id字段中)
     */
    @Id
    private Long id;
    /**
     * 操作时间
     */
    private Long createTime;
}

4️⃣:service实现

package com.todoitbo.tallybookdasmart.service.impl;
import com.todoitbo.tallybookdasmart.entity.SysExceptionLog;
import com.todoitbo.tallybookdasmart.service.ISysExceptionLogService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @author xiaobo
 * @date 2022/12/5
 */
@Service
public class SysExceptionLogServiceImpl implements ISysExceptionLogService {
    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public void insertSysExceptionLog(SysExceptionLog sysExceptionLog){
        mongoTemplate.insert(sysExceptionLog);
    }
}

🔚:简单的实现到此结束

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
4月前
|
NoSQL Java 测试技术
spring boot MongoDB实战(二)
spring boot MongoDB实战
86 1
|
4月前
|
NoSQL Java MongoDB
spring boot整合MongoDB 一(2)
spring boot整合MongoDB 一
61 0
|
4月前
|
NoSQL Java MongoDB
spring boot整合MongoDB 一(3)
spring boot整合MongoDB 一
56 0
|
4月前
|
NoSQL Java MongoDB
Spring Boot中MongoDB的使用和实战
Spring Boot中MongoDB的使用和实战
63 0
|
4月前
|
存储 NoSQL MongoDB
spring boot整合MongoDB 一(1)
spring boot整合MongoDB 一
82 0
|
4月前
|
NoSQL MongoDB 数据库
MongoDB【部署 03】Windows系统安装mongodb并设置用户名密码(无需安装mongosh)及SpringBoot集成报错 Command failed with error 18
MongoDB【部署 03】Windows系统安装mongodb并设置用户名密码(无需安装mongosh)及SpringBoot集成报错 Command failed with error 18
144 0
|
4月前
|
NoSQL Java MongoDB
spring boot MongoDB实战(一)
spring boot MongoDB实战
55 1
|
4月前
|
存储 NoSQL Java
SpringBoot 整合MongoDB
SpringBoot 整合MongoDB
52 0
|
NoSQL Java 测试技术
补习系列(17)-springboot mongodb 内嵌数据库
简介 前面的文章中,我们介绍了如何在SpringBoot 中使用MongoDB的一些常用技巧。那么,与使用其他数据库如 MySQL 一样,我们应该怎么来做MongoDB的单元测试呢? 使用内嵌数据库的好处是不需要依赖于一个外部环境,如果每一次跑单元测试都需要依赖一个稳定的外部环境,那么这样的测试是极不稳定的。
1883 0
|
19天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源