SpringBoot Mongodb重新封装自增ID(数值)

目的是还原JpaRepository中的自增ID设置

1、自定义注解:@GeneratedValue

2、定义枚举GenerationType, 用于选择ID赋值的类型(本文章并未真正使用,可作为拓展功能)

3、重写监听事件里面的方法,而进行某些处理,该类需要继承AbstractMongoEventListener类,

注意这里定义的集合中Id默认都是数值类型, 如果需要使用ObjectId则需要过滤集合。

@Slf4j
@Configuration
@RequiredArgsConstructor
public class NosqlIdConfig extends AbstractMongoEventListener<Object> {

    private final MongoTemplate mongoTemplate;

    @PostConstruct
    public void init(){
        log.info("文档型数据库开始初始化所有集合Id......");
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                Set<String> collectionNames = mongoTemplate.getCollectionNames();
                for (String collectionName : collectionNames) {
                    long count = mongoTemplate.count(new Query(), collectionName);
                    Long id = 0L;
                    if (count > 0){
                        List<Map> maps = mongoTemplate.findAll(Map.class, collectionName);
                        for (Map map : maps) {
                            Long src = (Long) map.get("_id");
                            if (src.longValue() > id.longValue()){
                                id = src;
                            }
                        }
                    }
                    identity.put(collectionName, id);
                }
                log.info("文档型数据库所有集合Id初始化成功: {}", identity);
            }
        });
        thread.setName("Document");
        thread.start();
    }

    @PreDestroy
    public synchronized void destory(){
        identity.clear();
    }

    private static Map<String, Long> identity = new LinkedHashMap<>();

    @Override
    public synchronized void onBeforeConvert(BeforeConvertEvent<Object> event) {
        Object source = event.getSource();
        Class<?> sourceClass = source.getClass();
        if (!sourceClass.isAnnotation()){
            super.onBeforeConvert(event);
        }
        Document document = sourceClass.getAnnotation(Document.class);
        if (null != source) {
            ReflectionUtils.doWithFields(source.getClass(), new ReflectionUtils.FieldCallback() {
                @Override
                public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                    ReflectionUtils.makeAccessible(field);
                    if (field.isAnnotationPresent(GeneratedValue.class)) {
                        Object value = field.get(source);
                        if (value == null) {
                            field.set(source, getAutoId(document.collection()));
                        }else {
                            field.set(source, value);
                        }
                    }
                }
            });
        }
    }

    private Long getAutoId(String collectionName){
        Long id = 0L;
        boolean exist = identity.containsKey(collectionName);
        if (exist){
            id = identity.get(collectionName) + 1L;
        }else {
            id = 1L;
        }
        identity.put(collectionName, id);
        return id;
    }

}
4、自定义注解:@EnableMongodb
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({NosqlIdConfig.class})
public @interface EnableMongodb {

}

5、在工程中应用只需使用注解@EnableMongodb便可将自定义配置生效,注意需要在文档对象的Id字段上加上@GeneratedValue方可生效

@EnableMongodb
@EnableMinio
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个开源的Java开发框架,用于简化Spring应用的开发和部署。MongoDB是一个面向文档的NoSQL数据库,具有高性能、可扩展和灵活的特点。在使用Spring Boot开发应用程序时,可以通过封装MongoDB来简化与数据库的交互。 在Spring Boot中使用MongoDB,通常需要引入相关的依赖库。可以在项目的pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> ``` 引入该依赖后,Spring Boot会自动配置MongoDB的连接信息,包括数据库地址、端口号、用户名和密码等。可以在配置文件中设置相关的配置参数,如application.properties或application.yml。 通过MongoTemplate类,可以进行MongoDB的各种操作,例如插入数据、查询数据、更新数据和删除数据等。在使用MongoTemplate时,可以通过编写相应的Repository接口来定义数据库操作的方法。Spring Boot会根据方法的命名规则自动生成查询语句。 除了使用MongoTemplate进行数据库操作外,还可以使用Spring Data MongoDB来简化数据库操作。Spring Data MongoDB提供了各种注解和接口,可以通过编写实体类、定义接口方法和使用注解来进行数据访问和操作。 通过封装MongoDB,可以避免编写繁琐的数据库访问代码,提高开发效率。同时,Spring Boot提供了丰富的功能和插件,可以帮助开发者进行数据库操作的扩展和优化。使用Spring Boot和MongoDB的组合,可以轻松构建高性能、可扩展和灵活的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值