2012-07-16 2 views
0

데이터베이스에 연결하여 쿼리를 실행하는 다음 코드가 있는데이 코드 (모델/서비스)를 배치 할 때 명확성이 없습니다.데이터베이스 관련 코드를 Grails에 넣을 곳

def value 
def url  = ConfigurationHolder.config.dataSource.url 
def username = ConfigurationHolder.config.dataSource.username 
def password = ConfigurationHolder.config.dataSource.password 
def driver = ConfigurationHolder.config.dataSource.driverClassName 
def sql  = Sql.newInstance(url, username, password, driver) 

sql.eachRow("select field_value from application_configuration where field_name=?", [field]) { 
     value=it.field_value 
} 

클래스에 ApplicationConfiguaration이 있는데이 도메인에서 쿼리하고 있습니다.

나는 쿼리 실행이 서비스 측에 간다 쿼리 실행 로직

답변

1

, 당신은 그것을 고려하고 domain object finders 또는 HQL을 사용해야합니다.

GORM 외부에서 쿼리를 수행해야하는 경우 서비스에 포함되어야합니다. DataSource 객체가 자동으로 서비스에 삽입 될 수 있고 Sql 객체가 직접 생성 될 수 있습니다.

import groovy.sql.Sql 

class ApplicationConfigurationService { 
    def dataSource 

    def valueForName(name) { 
     def sql = new Sql(dataSource) 

     sql.eachRow(...) { 
      value=it.field_value 
     } 
    } 
} 
-1

을 넣어 데이터베이스 연결 논리 2)을 넣어이 의심

1

)가 있습니다.

뷰는 컨트롤러와 상호 작용하며,이 뷰는 서비스와 함께 작동하고 마지막 뷰는 정의 된 모델 개체에 매핑 된 DB 데이터와 상호 작용합니다.

데이터베이스 연결 로그인이 DataSource.groovy에 콘에 간다/컨퍼런스 당신이 application_configuration을 대표하는 도메인 오브젝트를 작성하지 않은 경우

DataSource { 
pooled = true 
driverClassName = "com.mysql.jdbc.Driver" 
} 
hibernate { 
cache.use_second_level_cache=true 
cache.use_query_cache=true 
cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider' 
} 
// environment specific settings 
environments { 
development { 
    dataSource { 
     dbCreate = "update" // one of 'create', 'create-drop','update' 
     url = "jdbc:mysql://localhost:3306/" 
     username="" 
     password="" 
    } 
} 
test { 
    dataSource { 
     dbCreate = "update" 
     url = "jdbc:mysql://localhost:3306/" 
     username="" 
     password="" 

    } 
} 
production { 
    dataSource { 
     dbCreate = "update" 
     jndiName = "java:comp/env/" 
    } 
} 
} 
+1

Groovy SQL 객체를 사용하여 실행할 필요가 없습니다. 예를 들어 도메인 객체와 직접 상호 작용할 수 있습니다. 'ApplicationConfiguaration.list {eq "fieldName", value}' – tojofo