【问题标题】:Result set mapping in Grails / GORMGrails / GORM 中的结果集映射
【发布时间】:2011-01-26 17:45:46
【问题描述】:

我想将原生 SQL 查询的结果映射到 grails 中的简单 bean,类似于 @SqlResultSetMapping 注解的作用。

例如,给定一个查询

select x.foo, y.bar, z.baz from //etc...

将结果映射到

class FooBarBaz {
  String foo
  String bar
  String baz
}

谁能提供如何在 grails 中执行此操作的示例? 提前致谢。

【问题讨论】:

标签: grails jpa groovy grails-orm sqlresultsetmapping


【解决方案1】:

我在 Grails 控制台中测试成功

import groovy.sql.Sql

class FooBarBaz {
  String foo
  String bar
  String baz
}

// Initialising the Sql object like this ensures that the SQL statement
// will participate in the current transaction (if one exists)          
// 'ctx' refers to the Spring ApplicationContext when using the Grails console  
def sessionFactory = ctx.getBean('sessionFactory')
Sql sql = new Sql(sessionFactory.currentSession.connection())

def query = 'select email, user_real_name, phone from user'
def results = []
sql.eachRow query, {row -> results << new FooBarBaz(foo: row.email, bar: row.user_real_name, baz: row.phone)}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多