Groovy Grails 教程 mysql数据库配置使用方法【Grails中文教程】
grails框架使用 mysql的配置方法,
1.首先把mysql jdbc jar包放到grails项目的lib目录下
mysql jdbc jar包 下载 mysql-connector-java-5.1.27.zip
或者直接到官网上面下载。
2.打开项目找到conf文件夹下面有DataSource.groovy文件次文件就是database配置文件
在文件中找到dataSource配置:
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = "root"
}
上面就是设置说明:driverClassName就是设置驱动名称,username:是数据库用户名 ,password:是数据库的密码。
在文件中找到environments设置:
dbCreate = "update" //设置成update模式数据不会被删除 url = "jdbc:mysql://localhost:3306/grails?autoreconnect=true" //设置mysql 访问的URL连接 然后 autoreconnect=true就是自动连接数据库
一开始不知道grails的默认配置 每次重启grails 数据库中的数据都会被删除,是需要把开发模式下的create-drop 改成 update就可以了。
完整的DataSource.groovy配置文件内容如下:
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = "root"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
// cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/grails?autoreconnect=true"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/grails?autoreconnect=true"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/grails?autoreconnect=true"
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=false
validationQuery="SELECT 1"
jdbcInterceptors="ConnectionState"
}
}
}
}
上面就是要主要标红的位置的写法。
来源://作者:admin/更新时间:2013-11-19
顶
踩
相关文章:

