【问题标题】:Grails sql queriesGrails sql查询
【发布时间】:2011-06-08 16:52:39
【问题描述】:

想象一下我有这样的东西:

def example = {
   def temp = ConferenceUser.findAllByUser(User.get(session.user))
   [temp: temp]
}

解释我的问题: 尽管动态查找器非常易于使用且学习速度很快,但我必须将我网站的动态查找器替换为 sql 查询,因为这是必需的。由于我不太了解 SQL,所以我的主要问题是:

a) 我使用的是 SQLS 数据库,驱动程序和数据源配置良好,我的网站现在可以正常运行。如果我想用 sql 语句替换“findAllByUser”,我应该这样做:

def dataSource
...
def db = new Sql(dataSource)
def temp = db.rows("SELECT ... ")

b) 那行得通吗?我的意思是,如果我使用“findAllByUser”,临时对象将是一个列表,我是否需要打开与数据库的连接=?

【问题讨论】:

  • 也许您应该详细解释一下为什么要用 sql 语句替换 findAllByUser,为什么这是一个要求? Grails 有许多不同的查询方式,在section 5.4 of the User Guide 中有说明
  • 当然。在我的项目中,我需要使用 hibernate 和 sqls,因为我正在对两者进行评估。如果我不使用查询进行数据库搜索,那么要评估的 sqls 部分就没有了。
  • 这很奇怪,考虑到 Grails 查找器的目的是让事情变得比编写 SQL 更容易。但有时要求可能真的……很奇怪。
  • 对我来说听起来像是一个学校项目。

标签: sql grails


【解决方案1】:

是的,使用 grails,您可以进行普通 sql 和 hql 查询。 HQL 是“休眠查询语言”,允许您编写类似 sql 的语句,但使用您的域类和属性而不是表名和列名。要进行 hql 查询,请执行以下操作

def UserList = ConferenceUser.executeQuery('from ConferenceUser cu where cu.user = ?', [user]),  

这里有一个参数化查询——executeQuery 看到 ?在 hql 字符串中,并将数组中的参数替换为方法的第二个参数(在本例中为[user])。

http://grails.org/doc/latest/ref/Domain%20Classes/executeQuery.html

你可以看到这个关于如何使用 Grails 进行 sql 查询

Sql query for insert in grails

【讨论】:

    【解决方案2】:

    对于 Grails,您可以使用 Dynamic FindersCriteria BuildersHibernate Query Language (HQL)Groovy SQL

    使用Groovy SQL

    1. import groovy.sql.Sql
    2. 使用def dataSourcedef sessionFactory 请求对数据源的引用以进行交易
    3. 使用def sql = new Sql(dataSource)def sql = new Sql(sessionFactory.currentSession.connection()) 创建一个Sql 对象
    4. 根据需要使用Groovy SQL

    Grails 将自动管理与数据源的连接。

    Sql.rows 返回一个可以传递给您的view 的列表。

    例如:

    import groovy.sql.Sql
    
    class MyController {
        def dataSource
        def example = {
            def sql = new Sql(dataSource)
            [ temp: sql.rows("SELECT . . .") ]
        }
    }
    

    在交易中:

    import groovy.sql.Sql
    
    class MyController {
        def sessionFactory
        def example = {
            def sql = new Sql(sessionFactory.currentSession.connection())
            [ temp: sql.rows("SELECT . . .") ]
        }
    }
    

    我推荐这本书 Grails Persistence with GORM and GSQL 以获得很多很棒的技巧和技巧。

    【讨论】:

    • 谢谢你的电子书链接,我正在寻找这样的细节!
    【解决方案3】:

    更进一步/提示

    • 使用 Spring bean

    您可以在 Grails 应用程序中将 groovy.sql.Sql 实例设为 Spring bean。在grails-app/conf/spring/resources.groovy 中定义Sql bean:

    // File: grails-app/conf/spring/resources.groovy
    
    beans = {
    
        // Create Spring bean for Groovy SQL.
        // groovySql is the name of the bean and can be used
        // for injection.
        sql(groovy.sql.Sql, ref('dataSource'))
    
    }
    

    接下来在你的类中注入 Sql 实例。

    package com.example
    
    import groovy.sql.GroovyRowResult
    
    class CarService {
    
       // Reference to sql defined in resources.groovy.
       def sql
    
       List<GroovyRowResult> allCars(final String searchQuery) {
          final String searchString = "%${searchQuery.toUpperCase()}%"
    
          final String query = '''\
             select id, make, model
             from car
             where ...
             '''
    
            // Use groovySql bean to execute the query.
            final results = sql.rows(query, search: searchString)
            results
       }
    }
    
    • 多个数据源

      adminSql(groovy.sql.Sql, ref("dataSource_admin"))

      userSql(groovy.sql.Sql, ref("dataSource_user"))

    并注入 bean

    def userSql
    def adminSql
    

    进入需要它们的服务。

    或不注射

    import groovy.sql.Sql
    // ...
    // inject the datasource bean
    def dataSource_admin
    
    // ...
    // in a method
    Sql sql = new Sql(dataSource_admin)
    

    早期 Grails 版本

    在早期的 grails 版本中循环遍历 GORM 结果集可能会在模板循环中间导致不必要的查询。使用 groovy SQL 可以帮助解决这个问题。

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多