Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GrailsSQL查询_Sql_Grails - Fatal编程技术网

GrailsSQL查询

GrailsSQL查询,sql,grails,Sql,Grails,想象一下我有这样的东西: def example = { def temp = ConferenceUser.findAllByUser(User.get(session.user)) [temp: temp] } def dataSource ... def db = new Sql(dataSource) def temp = db.rows("SELECT ... ") 解释我的问题: 虽然动态查找器非常容易使用和快速学习,但我必须更换我的网站上的动态查找器以进行sql查询

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

def example = {
   def temp = ConferenceUser.findAllByUser(User.get(session.user))
   [temp: temp]
}
def dataSource
...
def db = new Sql(dataSource)
def temp = db.rows("SELECT ... ")
解释我的问题: 虽然动态查找器非常容易使用和快速学习,但我必须更换我的网站上的动态查找器以进行sql查询,因为这是一项要求。由于我不太了解SQL,我的主要问题是:

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

def example = {
   def temp = ConferenceUser.findAllByUser(User.get(session.user))
   [temp: temp]
}
def dataSource
...
def db = new Sql(dataSource)
def temp = db.rows("SELECT ... ")

b) 这样行吗?我的意思是,如果我使用“findAllByUser”,temp对象将是一个列表,我是否需要打开到数据库的连接=?

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

def UserList = ConferenceUser.executeQuery('from ConferenceUser cu where cu.user = ?', [user]),  
这里有一个参数化查询——executeQuery可以看到?并替换数组中的参数,该数组是方法的第二个参数(在本例中为
[user]

您可以在如何使用Grails执行sql查询中看到这一点

对于Grails,您可以使用、或

使用:

  • 导入groovy.sql.sql
  • 对于事务,使用
    def datasource
    def sessionFactory
    请求对数据源的引用
  • 使用
    defsql=newsql(数据源)
    defsql=newsql(sessionFactory.currentSession.connection())创建
    Sql
    对象
  • 按要求使用
  • Grails将自动管理到数据源的连接

    返回可传递给用户的列表

    例如:

    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 . . .") ]
        }
    }
    

    我推荐这本书有很多很棒的技巧和技巧。

    更进一步/tips

    • 使用春豆
    您可以将
    groovy.sql.sql
    实例作为Grails应用程序中的Springbean。在
    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
       }
    }
    
    进入需要它们的服务

    还是不注射

    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有助于实现这一点。

    也许您应该进一步解释为什么要用SQL语句替换FindallByser,为什么这是一个要求?Grails有许多不同的查询方法,这些都有详细的解释。在我的项目中,我需要使用hibernate和sqls,因为im将在这两个平台上进行评估。如果我不使用查询进行数据库搜索,那么要计算的sqls部分就没有了。这很奇怪,考虑到Grails Finder的目的是让事情比编写SQL更容易。但是需求可以是真的。。。有时候很奇怪。听起来像是学校的项目。谢谢你的电子书链接,我一直在寻找这样的细节!