grails 中实现数据库的可配置(二)

Grails中的动态修改数据库配置文件

1.在conf中定义datasource.properties,如下所示
driverClass = com.mysql.jdbc.Driver
username = root
password = root
url = jdbc:mysql: // localhost:3306/testdb?useUnicode=true&characterEncoding=utf8

2. 修改DataSource.groovy,如下所示
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.support.PropertiesLoaderUtils

def properties 
=  PropertiesLoaderUtils.loadProperties( new  ClassPathResource('datasource.properties'))
dataSource {
    pooled 
=   true
    driverClassName 
=  properties.getProperty( " driverClass " )
    username 
=  properties.getProperty( " username " )
    password 
=  properties.getProperty( " password " )
    url 
=  properties.getProperty( " url " )
//     driverClassName = "org.hsqldb.jdbcDriver"
//
    username = "sa"
//
    password = ""
}


参考http://bloodwolf-china.javaeye.com/blog/266166

你可能感兴趣的:(mysql,数据库,jdbc,url,grails)