需求很简单,保存一个api调用时的相关信息,利用grails+oracle来进行开发。
实际编码过程中,却出现很多问题,特此记录,以备查阅。
基本设置
log4j配置
在Config.groovy中:
log4j.main = {
//更改此处的error为info、debug即可更改log的基本
error 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'org.codehaus.groovy.grails.web.sitemesh', // layouts
'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
'org.codehaus.groovy.grails.web.mapping', // URL mapping
'org.codehaus.groovy.grails.commons', // core / classloading
'org.codehaus.groovy.grails.plugins', // plugins
'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
'org.springframework',
'org.hibernate',
'net.sf.ehcache.hibernate'
}
sql语句显示配置
在DataSource.groovy中的environments中:
development {
dataSource {
dbCreate = "update"
url = "jdbc:oracle:thin:@192.168.10.77:1521:orcl"
logSql = true //显示sql语句
}
}
在idea中开启debug模式
在BuildConfig.groovy中,直接注释以下代码:
grails.project.fork = [
// configure settings for compilation JVM, note that if you alter the Groovy version forked compilation is required
// compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
// configure settings for the test-app JVM, uses the daemon by default
test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
// configure settings for the run-app JVM
//run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
run: [maxMemory: 768, minMemory: 64, debug: true, maxPerm: 256, forkReserve:false],
// configure settings for the run-war JVM
war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
// configure settings for the Console UI JVM
console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]
添加如下代码:
run:false
然后点击idea的debug启动就能正常调试了。
连接oracle设置
dataSource {
pooled = true
jmxExport = true
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "name"
password = "pwd"
dialect = org.hibemate.dialect.Oracle10Dialect
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:oracle:thin:@192.168.1.17:1521:orcl"
logSql = true //显示sql语句
}
}
}
先建表,后建domain
grails一般是先有domain,然后运行让其自动构建表.此处是已经存在了数据库表,根据表来构建domain class,诸多细节,于此记录:
指定表
为domain class指定表,需要用到如下语法:
static mapping = {
table 'API_MONITOR_SOB1'
}
禁用domain的version字段:
static mapping = {
table 'API_MONITOR_SOB1'
version false
}
static mapping = {
table 'API_MONITOR_SOB1'
version false
id generator: 'sequence',column:'MONID',params: [sequence:'api_monitor_sob1_sequence']
}
为属性指定不同名的字段
此处需要注意oracle数据库的大小写显著,字段名称一定要符合数据库设置,否则可能出现“**未指定列”的异常。
static mapping = {
table 'API_MONITOR_SOB1'
version false
id generator: 'sequence',column:'MONID',params: [sequence:'api_monitor_sob1_sequence']
userId column: 'USERID' //USERID数据库表的字段名称
paramStr column: 'PARAMSTR'
}
controller中domain的save方法无反应
在实际开发中,遇到在controller中调用save方法保存domain,结果sql不见发出,异常也没有抛出,数据库里面也没有记录,检查之后是因为domain的constraints为空的话,grails会默认所有domain属性not null,保存时也不会提交反馈,更不会抛出异常,如果要显示具体是哪些字段数据不符合约束规范,可用如下代码打印显示:
if(!m.save()){
m.errors.allErrors.each{
println it
}
}
是为记,后面有内容再补充。