【发布时间】:2018-09-15 22:31:11
【问题描述】:
我正在尝试在一个项目中创建 2 个实体(不相关)。我正在使用 Java springboot 连接到 mysql 数据库。那么我能做的最好的方法是什么?因为我试图制作 2 个存储库,但我只成功了其中的 1 个。另一个给我一个错误。它给了我错误,即使当我在 mysql 工作台中运行查询时它实际上很好
在我的存储库 (AccountRepository.java) 中不起作用,我放了这样的内容:
public interface AccountRepository extends CrudRepository<Accounts, Integer> {
@Query("SELECT applicationPassword FROM Accounts where applicationName = 'myApp'")
String findPasswordApp();
}
在实体文件(Accounts.java)中,我放了这样的东西
@Entity
public class Accounts {
@Id
private Integer accountID;
private String applicationName;
private String applicationPassword;
public void setAccountID(Integer accountID) {
this.accountID = accountID;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public void setApplicationPassword(String applicationPassword) {
this.applicationPassword = applicationPassword;
}
public Integer getAccountID() {
return accountID;
}
public String getApplicationName() {
return applicationName;
}
public String getApplicationPassword() {
return applicationPassword;
}
}
但它给了我错误。我从这个实体想要的是我可以检查我已经在 mysql 工作台中手动放入的数据库中的值(读取)。
这是我首先在 mysl 工作台中执行的表查询
CREATE TABLE Accounts (
accountID int,
applicationName varchar(255),
applicationPassword varchar(255)
);
当我成功将数据插入其中时:
INSERT INTO Accounts (accountID, applicationName, applicationPassword)
VALUES ('1', 'myApp', 'password'));
我的代码有什么问题吗?请帮忙
编辑:我试图掩盖这一行的 try catch 'String password = accountRepository.findPasswordApp(); ' 这给了我像'java.lang.NullPointerException'这样的异常
这是我的全部例外: 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:499) 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:402) 在 org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:206) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) 在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) 在 java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 在 java.lang.Thread.run(Thread.java:748) '
【问题讨论】:
-
抛出的异常是什么?
-
我尝试用 try catch 覆盖它,但异常显示为“java.lang.NullPointerException”。但我不知道为什么。因为我在 mysql 工作台上运行查询并且它工作正常
-
尝试调试并找出
accountRepositoryreference 是否为空...如果是,请检查 accountRepository 的 bean 接线/依赖注入 -
请发布有关您的堆栈跟踪的更多信息。
标签: java mysql spring spring-boot