gpt4 book ai didi

java - Spring Boot Mongodb 按 ID 搜索返回 null

转载 作者:行者123 更新时间:2023-12-03 15:59:45 30 4
gpt4 key购买 nike

我使用 mongodb 创建了一个 Spring Boot 项目,当我将数据插入集合时,它会被插入,但是当我尝试通过 idfindOne 获取基于插入的值时在 id 上它总是返回 null,我已经在下面给出了我的模型类和插入方法,请告诉我出了什么问题

Account.java

@Document(collection = "account")
public class Account {

@Id
private long _id;

@Field("account_name")
private String accountName;

@Field("connector_type")
private String connectorType;

@Field("xsiURI1")
private String xsiURI1;

@Field("xsiURI2")
private String xsiURI2;

@Field("oci1")
private String OCI1;

@Field("oci2")
private String OCI2;

@Field("telcomadmin_username")
private String telcomadminUsername;

@Field("telcomadmin_password")
private String telcomadminPassword;

@Field("password_expdays")
private String passwordExpdays;

@Field("account_email_address")
private String accountEmailAddress;

@DateTimeFormat(iso = ISO.DATE_TIME)
@Field("inserted_date")
private Date insertedDate;

@DateTimeFormat(iso = ISO.DATE_TIME)
@Field("updated_date")
private Date updatedDate;

@Field("isActive")
private Boolean isActive;

public long get_id() {
return _id;
}

public void set_id(long _id) {
this._id = _id;
}

public String getXsiURI1() {
return xsiURI1;
}

public void setXsiURI1(String xsiURI1) {
this.xsiURI1 = xsiURI1;
}

public String getXsiURI2() {
return xsiURI2;
}

public void setXsiURI2(String xsiURI2) {
this.xsiURI2 = xsiURI2;
}

public String getOCI1() {
return OCI1;
}

public void setOCI1(String oCI1) {
OCI1 = oCI1;
}

public String getOCI2() {
return OCI2;
}

public void setOCI2(String oCI2) {
OCI2 = oCI2;
}

public String getAccountName() {
return accountName;
}

public void setAccountName(String accountName) {
this.accountName = accountName;
}

public String getConnectorType() {
return connectorType;
}

public void setConnectorType(String connectorType) {
this.connectorType = connectorType;
}

public String getTelcomadminUsername() {
return telcomadminUsername;
}

public void setTelcomadminUsername(String telcomadminUsername) {
this.telcomadminUsername = telcomadminUsername;
}

public String getTelcomadminPassword() {
return telcomadminPassword;
}

public void setTelcomadminPassword(String telcomadminPassword) {
this.telcomadminPassword = telcomadminPassword;
}

public String getPasswordExpdays() {
return passwordExpdays;
}

public void setPasswordExpdays(String passwordExpdays) {
this.passwordExpdays = passwordExpdays;
}

public String getAccountEmailAddress() {
return accountEmailAddress;
}

public void setAccountEmailAddress(String accountEmailAddress) {
this.accountEmailAddress = accountEmailAddress;
}

public Date getInsertedDate() {
return insertedDate;
}

public void setInsertedDate(Date insertedDate) {
this.insertedDate = insertedDate;
}

public Date getUpdatedDate() {
return updatedDate;
}

public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}

public Boolean getIsActive() {
return isActive;
}

public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
}

AccountsController.java

@RestController
@RequestMapping("/accounts")
@CrossOrigin("*")
public class AccountsController {
@Autowired
AccountsRepository accountsRepository;

@Autowired
SequenceRepository sequenceRepository;

private static final String ACCOUNT_SEQ_KEY = "accountsequence";

@PostMapping("/create")
public Account createAccount(@Valid @RequestBody Account account) {
account.set_id(sequenceRepository.getNextSequenceId(ACCOUNT_SEQ_KEY));
account.setIsActive(true);
return accountsRepository.save(account);
}

@GetMapping(value = "/findByID/{id}")
public ResponseEntity<Account> getAccountById(@PathVariable("id") String id) {
Account account = accountsRepository.findOne(id);
if (account == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity<>(account, HttpStatus.OK);
}
}
}

AccountsRepository

public interface AccountsRepository {

List<Account> findAll(Sort sortByCreatedAtDesc);

Account save(Account account);

Account findOne(String id);

void delete(String id);

}

AccountsRepositoryIMPL

@Repository
public class AccountsRepositoryImpl implements AccountsRepository {
DBOperations dbOperations = new DBOperations();

@Override
public List<Account> findAll(Sort sortByCreatedAtDesc) {
Query q = new Query().with(new Sort(Sort.Direction.ASC, "inserted_date"));
List<Account> accountList = dbOperations.getMongoOpertion().findAllAndRemove(q, Account.class);
return accountList;
}

@Override
public Account save(Account account) {
try {
dbOperations.getMongoOpertion().save(account);
} catch (Exception e) {
e.printStackTrace();
}
return account;
}

@Override
public Account fin**strong text**dOne(String id) {
Account account = dbOperations.getMongoOpertion().findOne(Query.query(Criteria.where("_id").is(id)),
Account.class, "account");
return account;
}

@Override
public void delete(String id) {
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
Account account = dbOperations.getMongoOpertion().findOne(query, Account.class);
dbOperations.getMongoOpertion().remove(account);

}

}

DBOperations.java

public class DBOperations {

ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

public MongoOperations getMongoOpertion() {
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
return mongoOperation;
}

}

最佳答案

看看你的代码。您已将 _id 声明为 Long 类型。

@Id
private long _id;

但是在下面的方法中,您将传递String id来匹配条件。

所以它不起作用。

@Override
public Account findOne(String id) {
Account account = dbOperations.getMongoOpertion().findOne(Query.query(Criteria.where("_id").is(id)),
Account.class, "account");
return account;
}

@Override
public void delete(String id) {
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
Account account = dbOperations.getMongoOpertion().findOne(query, Account.class);
dbOperations.getMongoOpertion().remove(account);

}

关于java - Spring Boot Mongodb 按 ID 搜索返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217900/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com