springboot整合mongodbid自增
时间: 2023-09-17 08:11:29 浏览: 79
整合FineReport到SpringBoot项目
在Spring Boot中整合MongoDB实现自增ID可以通过自定义序列来实现。以下是一种常见的实现方式:
1. 首先,创建一个名为Sequence的实体类,用于保存自增ID的当前值:
```java
package com.example.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "sequences")
public class Sequence {
@Id
private String id;
private long value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public long getValue() {
return value;
}
public void setValue(long value) {
this.value = value;
}
}
```
2. 接下来,创建一个名为SequenceGenerator的自定义序列生成器类,用于生成自增ID:
```java
package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.FindAndModifyOptions;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import com.example.entity.Sequence;
@Service
public class SequenceGenerator {
private final MongoOperations mongoOperations;
@Autowired
public SequenceGenerator(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
public long generateSequence(String seqName) {
Query query = new Query().addCriteria(Criteria.where("_id").is(seqName));
Update update = new Update().inc("value", 1);
FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true).upsert(true);
Sequence sequence = mongoOperations.findAndModify(query, update, options, Sequence.class);
return sequence.getValue();
}
}
```
3. 最后,在需要生成自增ID的实体类中使用@Autowired注入SequenceGenerator,并使用@PrePersist注解在保存实体之前生成自增ID:
```java
package com.example.entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PrePersist;
import org.springframework.data.mongodb.core.mapping.Document;
import com.example.service.SequenceGenerator;
@Document(collection = "users")
public class User {
@Id
private long id;
private String name;
private int age;
@Autowired
private SequenceGenerator sequenceGenerator;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@PrePersist
public void generateId() {
this.id = sequenceGenerator.generateSequence(User.SEQUENCE_NAME);
}
}
```
通过以上方式,就可以在Spring Boot整合MongoDB中实现自增ID的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [springboot jpa id 自增问题解决](https://blog.csdn.net/helloexp/article/details/121474077)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文