需求很簡單,保存一個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
}
}
因此,最好是將constraints設置好約束。參加:http://akunamotata.iteye.com/blog/1742859
是爲記,後面有內容再補充。