grails连接mysql数据库

来源:互联网 发布:淘宝速捷运动专营店 编辑:程序博客网 时间:2024/06/04 18:54
Grails切换到mysql数据库需要做如下的配置:
1、将mysql-connector-java-5.1.22-bin.jar驱动复制到Grails应用的根目录lib下面。
2、修改conf/DataSource.groovy;Grails默认用的是H2数据库,我们需要将driverClassName = "org.h2.Driver" 改成 driverClassName = 'com.mysql.jdbc.Driver';url改成我们自己数据库的url

上面的改法会发生几个问题,
1、如果单纯光把驱动复制到lib目录,会发生异常:
java.lang.ClassNotFoundException: com/mysql/jdbc/Driver
找不到这个驱动,此时我们还需要在BuildConfig.groovy文件中加入下面的配置:
dependencies {
    runtime 'mysql:mysql-connector-java:5.1.22'
}

2、尽管我们自己的应用用的是mysql,但是可能某些插件或者框架用的是H2数据库,这样如果我们之配置mysql的数据源,可能导致数据库连接驱动不匹配的异常。
因此我们需要保留原来的数据源配置,另外添加mysql的数据源。
dataSource {
    pooled = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
        }

dataSource_mysql {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = 'root'
password = 'root'
url = 'jdbc:mysql://localhost/DojoGrails'
dbCreate = 'update'
}
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
            pooled = true
            properties {
               maxActive = -1
               minEvictableIdleTimeMillis=1800000
               timeBetweenEvictionRunsMillis=1800000
               numTestsPerEvictionRun=3
               testOnBorrow=true
               testWhileIdle=true
               testOnReturn=true
               validationQuery="SELECT 1"
            }
        }
    }
}

上面是DataSource配置的例子,添加了第二个数据源 dataSource_mysql
在这个多数据源应用里,我们需要在使用dataSource_mysql数据源的domain类里加入如下配置:
static mapping = {
datasource 'mysql'
}
以表明这个domain用的是哪个数据源;


~~~~~~~~~~~~~~
官方对多数据源的说明:

Configuring Additional DataSources
The default DataSource configuration in grails-app/conf/DataSource.groovy looks something like this:

dataSource {
    pooled = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}
hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
environments {
    development {
        dataSource {
            dbCreate = "create-drop"
            url = "jdbc:h2:mem:devDb"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb"
        }
    }
}

This configures a single DataSource with the Spring bean named dataSource. To configure extra DataSources, add another dataSource block (at the top level, in an environment block, or both, just like the standard DataSource definition) with a custom name, separated by an underscore. For example, this configuration adds a second DataSource, using MySQL in the development environment andOracle in production:

environments {
    development {
        dataSource {
            dbCreate = "create-drop"
            url = "jdbc:h2:mem:devDb"
        }
        dataSource_lookup {
            dialect = org.hibernate.dialect.MySQLInnoDBDialect
            driverClassName = 'com.mysql.jdbc.Driver'
            username = 'lookup'
            password = 'secret'
            url = 'jdbc:mysql://localhost/lookup'
            dbCreate = 'update'
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb"
        }
        dataSource_lookup {
            dialect = org.hibernate.dialect.Oracle10gDialect
            driverClassName = 'oracle.jdbc.driver.OracleDriver'
            username = 'lookup'
            password = 'secret'
            url = 'jdbc:oracle:thin:@localhost:1521:lookup'
            dbCreate = 'update'
        }
    }
}
You can use the same or different databases as long as they're supported by Hibernate.

Configuring Domain Classes
If a domain class has no DataSource configuration, it defaults to the standard 'dataSource'. Set the datasource property in the mapping block to configure a non-default DataSource. For example, if you want to use the ZipCode domain to use the 'lookup' DataSource, configure it like this;

class ZipCode {
   String code

   static mapping = {
      datasource 'lookup'
   }
}

A domain class can also use two or more DataSources. Use the datasources property with a list of names to configure more than one, for example:

class ZipCode {
   String code

   static mapping = {
      datasources(['lookup', 'auditing'])
   }
}

If a domain class uses the default DataSource and one or more others, use the special name 'DEFAULT' to indicate the default DataSource:

class ZipCode {
   String code

   static mapping = {
      datasources(['lookup', 'DEFAULT'])
   }
}

If a domain class uses all configured DataSources use the special value 'ALL':

class ZipCode {
   String code

   static mapping = {
      datasource 'ALL'
   }
}
原创粉丝点击