从 Grails MVC 应用程序查询 Postgres
Querying Postgres from Grails MVC application
- 已配置 Datasource.groovy 和 BuildConfig.groovy
- 需要帮助查询 groovy 服务中的 Postgres 数据库。
在 postgres 中有以下 table:
table structure
需要在 grails 应用程序中编写以下 SQL 查询:
SELECT json_agg(r)
FROM(
SELECT data#>'{"name"}' as program,data#>'{"country"}' as agency,sum(cast(cast(data#>'{"amount"}' as text)as Integer)) as total_amount
FROM salary
group by data#>'{"name"}',data#>'{"country"}'
order by program, agency
)r
对不起,我还不能发表评论,因为我没有 50 个声誉,所以我在这里留下我的评论。
您是否尝试过在 postgres 中使用 CREATE VIEW 创建视图并使用服务中的简单查询?这就是我对我所做的。从 grails 中以这种方式查询要容易得多。
编辑 - 我的 SQL 服务示例
package com.myApp
import groovy.sql.Sql //Need to import this
class SomeService {
def dataSource //Need it to connect to your database
def result() {
def sql = new Sql(dataSource)
def resultRows = sql.rows('...your SQL statement...')
}
}
package com.myApp
class SomeController {
def someService //Inject your serivce here
def resultRows = someService.result() //Call service method here
[resultRows:resultRows] //Put the result into a map
}
并且您可以从视图中调用结果图。
现在,如果您不想在服务中使用长 SQL 语句,可以在 postgres 中使用 CREATE VIEW sql 语句。
CREATE or REPLACE VIEW result_table as
(... your SQL statement...);
希望对您有所帮助。
使用了以下代码并且有效...
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/TestDb");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
def sql = new Sql(dataSource)
def resultRows = sql.rows(---SQL Query here---)
return resultRows
需要帮助用 Hibernate 替换上面的 JDBC 方法。
- 已配置 Datasource.groovy 和 BuildConfig.groovy
- 需要帮助查询 groovy 服务中的 Postgres 数据库。
在 postgres 中有以下 table: table structure
需要在 grails 应用程序中编写以下 SQL 查询:
SELECT json_agg(r)
FROM(
SELECT data#>'{"name"}' as program,data#>'{"country"}' as agency,sum(cast(cast(data#>'{"amount"}' as text)as Integer)) as total_amount
FROM salary
group by data#>'{"name"}',data#>'{"country"}'
order by program, agency
)r
对不起,我还不能发表评论,因为我没有 50 个声誉,所以我在这里留下我的评论。 您是否尝试过在 postgres 中使用 CREATE VIEW 创建视图并使用服务中的简单查询?这就是我对我所做的。从 grails 中以这种方式查询要容易得多。
编辑 - 我的 SQL 服务示例
package com.myApp
import groovy.sql.Sql //Need to import this
class SomeService {
def dataSource //Need it to connect to your database
def result() {
def sql = new Sql(dataSource)
def resultRows = sql.rows('...your SQL statement...')
}
}
package com.myApp
class SomeController {
def someService //Inject your serivce here
def resultRows = someService.result() //Call service method here
[resultRows:resultRows] //Put the result into a map
}
并且您可以从视图中调用结果图。
现在,如果您不想在服务中使用长 SQL 语句,可以在 postgres 中使用 CREATE VIEW sql 语句。
CREATE or REPLACE VIEW result_table as
(... your SQL statement...);
希望对您有所帮助。
使用了以下代码并且有效...
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/TestDb");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
def sql = new Sql(dataSource)
def resultRows = sql.rows(---SQL Query here---)
return resultRows
需要帮助用 Hibernate 替换上面的 JDBC 方法。