public boolean isDuplicateAccountMaterChartOfAccountsBySQL(MasterChartOfAccounts masterChartOfAccounts) {
String query = """
SELECT
COUNT(mcoa.id) as mcoaCount
FROM master_chart_of_accounts as mcoa
WHERE
mcoa.parent_account_id = ${masterChartOfAccounts.parentAccount?.id}
AND mcoa.account_code = '${masterChartOfAccounts.accountCode}'
AND mcoa.country_id = ${masterChartOfAccounts.countryId}
AND mcoa.account_status_id = ${AccountsConstants.DOMAIN_STATUS_ACTIVE}
"""
Sql db = new Sql(dataSource)
List<GroovyRowResult> resultList = db.rows(query)
return resultList.get(0).mcoaCount
}
这里字段parent_account_id可以是NULL或可以是Null。但是sql查询不能以这种方式对null值起作用。我们要写 其中mcoa.parent_account_id为NULL。 那么在字段可以为NULL或非空的情况下我该怎么办
答案 0 :(得分:3)
@Transactional(readOnly = true)
public boolean isDuplicateAccountMaterChartOfAccountsBySQL(MasterChartOfAccounts masterChartOfAccounts) {
String query = """
SELECT
COUNT(mcoa.id) as mcoaCount
FROM master_chart_of_accounts as mcoa
WHERE
mcoa.account_code = '${masterChartOfAccounts.accountCode}'
AND mcoa.country_id = ${masterChartOfAccounts.countryId}
AND mcoa.account_status_id = ${AccountsConstants.DOMAIN_STATUS_ACTIVE}
"""
if (masterChartOfAccounts.parentAccount?.id) {
query += " AND mcoa.parent_account_id = ${masterChartOfAccounts.parentAccount?.id}"
}
Sql db = new Sql(dataSource)
List<GroovyRowResult> resultList = db.rows(query)
return resultList.get(0).mcoaCount
}
是的我解决了。 :D