springboot整合MongoDB

12/5/2022

1️⃣:加maven坐标

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1
2
3
4

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));
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

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);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

🔚:简单的实现到此结束

Last Updated: 10/30/2024, 10:57:00 AM