Select 来自 Hibernate Grails 的数据库
Select Database from Hibernate Grails
我需要帮助。我正在使用 HQL,但我卡在这段代码中,我不知道如何解决它。
def deposit = Deposit.executeQuery("select terminalID,deposit,created_at from Deposit")
我得到了这样的正确结果
[[1, 1000, 2020-07-29 13:29:28.0], [0, 1, 2020-07-29 13:42:11.0], [2, 1000, 2020-07-29 13:50:08.0], [0, 1, 2020-07-29 14:03:07.0]]
但是在查看页面中,我不能调用terminalID, deposit,created_at因为没有这样的属性
如果我使用
def deposit = Deposit.executeQuery("from Deposit")
我得到了这样的错误结果 [com.dm.Bank.Deposit : ID]
[com.dm.Bank.Deposit : 2, com.dm.Bank.Deposit : 2, com.dm.Bank.Deposit : 3, com.dm.Bank.Deposit : 2]
数据库存款:数量是auto_increment
存款结果 不同
数据库:
class Deposit implements Validateable{
String number
String id
String terminalID
String startDate
String endDate
String currency
String startBalance
String transactionDate
String branchCode
String transactionType
String transactionAmount
String currentBalance
String deposit
String transactionName
String trailer
String corporateId
String accountNumber
String created_at
String company
static mapping = {
datasource 'DBTarget'
table 'HISTORY_MUTASI'
number column: "Number"
id generator: 'assigned', column: "ID"
terminalID column: "TerminalID"
startDate column: "StartDate"
endDate column: "EndDate"
currency column: "Currency"
startBalance column: "StartBalance"
transactionDate column: "TransactionDate"
branchCode column: "BranchCode"
transactionType column: "TransactionType"
transactionAmount column: "TransactionAmount"
currentBalance column: "CurrentBalance"
deposit column: "Deposit"
transactionName column: "TransactionName"
trailer column: "Trailer"
corporateId column: "CorporateId"
accountNumber column: "AccountNumber"
created_at column: "created_at"
company column: "Company"
version false
}
static constraints = {
number unique:true,generator:"increment";
}
恕我直言,了解 executeQuery
returns.
的方式非常重要
The executeQuery method allows the execution of arbitrary HQL queries.
HQL queries can return domain class instances, or Arrays of specified
data when the query selects individual fields or calculated values.
Read more executeQuery from Grails documentation.
正如我所看到的结果格式,您必须使用 index-based 操作。
例如,我们可以将结果显示在如下视图中。
<g:each in="${deposit}" var="entry">
<p>TerminalID: ${entry[0]} with the deposit ${entry[1]} issued at ${entry[2]}</p>
</g:each>
我需要帮助。我正在使用 HQL,但我卡在这段代码中,我不知道如何解决它。
def deposit = Deposit.executeQuery("select terminalID,deposit,created_at from Deposit")
我得到了这样的正确结果
[[1, 1000, 2020-07-29 13:29:28.0], [0, 1, 2020-07-29 13:42:11.0], [2, 1000, 2020-07-29 13:50:08.0], [0, 1, 2020-07-29 14:03:07.0]]
但是在查看页面中,我不能调用terminalID, deposit,created_at因为没有这样的属性
如果我使用
def deposit = Deposit.executeQuery("from Deposit")
我得到了这样的错误结果 [com.dm.Bank.Deposit : ID]
[com.dm.Bank.Deposit : 2, com.dm.Bank.Deposit : 2, com.dm.Bank.Deposit : 3, com.dm.Bank.Deposit : 2]
数据库存款:数量是auto_increment
存款结果 不同
数据库:
class Deposit implements Validateable{
String number
String id
String terminalID
String startDate
String endDate
String currency
String startBalance
String transactionDate
String branchCode
String transactionType
String transactionAmount
String currentBalance
String deposit
String transactionName
String trailer
String corporateId
String accountNumber
String created_at
String company
static mapping = {
datasource 'DBTarget'
table 'HISTORY_MUTASI'
number column: "Number"
id generator: 'assigned', column: "ID"
terminalID column: "TerminalID"
startDate column: "StartDate"
endDate column: "EndDate"
currency column: "Currency"
startBalance column: "StartBalance"
transactionDate column: "TransactionDate"
branchCode column: "BranchCode"
transactionType column: "TransactionType"
transactionAmount column: "TransactionAmount"
currentBalance column: "CurrentBalance"
deposit column: "Deposit"
transactionName column: "TransactionName"
trailer column: "Trailer"
corporateId column: "CorporateId"
accountNumber column: "AccountNumber"
created_at column: "created_at"
company column: "Company"
version false
}
static constraints = {
number unique:true,generator:"increment";
}
恕我直言,了解 executeQuery
returns.
The executeQuery method allows the execution of arbitrary HQL queries. HQL queries can return domain class instances, or Arrays of specified data when the query selects individual fields or calculated values. Read more executeQuery from Grails documentation.
正如我所看到的结果格式,您必须使用 index-based 操作。
例如,我们可以将结果显示在如下视图中。
<g:each in="${deposit}" var="entry">
<p>TerminalID: ${entry[0]} with the deposit ${entry[1]} issued at ${entry[2]}</p>
</g:each>