由於在過去一段時間內
已有多位朋友向我詢問如何用Grails處理遺留數據庫
為了回答這個問題
我給出一個實例
並適當講解
不足之處
敬請諒解
我使用的數據庫為MySQL其中存在一個名為legacy_dev的schemalegacy_dev中有一張表叫user
創建Grails應用程序我將它命名為legacygrails createapp legacy
創建一個域類Usergrails createdomainclass User
修改grailsapp\domain\Usergroovy的內容如下所示
class User {
String userId
String password
static constraints = {
userId(blank: false maxSize: )
password(blank: false maxSize: )
}
}
生成與域類User相關的所有Grails應用程序工件(artifact)grails generateall User
將grailsapp\conf\DevelopmentDataSourcegroovy的內容改為
class DevelopmentDataSource {
boolean pooling = true
// 將這行注釋掉
// String dbCreate = update // one of create createdropupdate
// url和driver要正確
String url = jdbc:mysql://localhost:/legacy_dev
String driverClassName = commysqljdbcDriver
String username = root
String password = // 這裡為您的密碼 :)
}
自行配置Hibernate
hibernatecfgxml
<? xml version= encoding=UTF ?>
<! DOCTYPE hibernateconfiguration PUBLIC
//Hibernate/Hibernate Configuration DTD //EN
configurationdtd >
< hibernateconfiguration >
< sessionfactory >
< property name =connectiondriver_class > commysqljdbcDriver
</ property >
< property name =connectionurl > jdbc:mysql://localhost:/legacy_dev
</ property >
< property name =connectionusername > root </ property >
< property name =connectionpassword ></ property >
< property name =connectionpool_size > </ property >
< property name =dialect > orghibernatedialectMySQLDialect </ property >
< property name =current_session_context_class > thread </ property >
< property name =cacheprovider_class > orghibernatecache
NoCacheProvider </ property >
< property name =show_sql > true </ property >
< property name =hbmddlauto > validate </ property >
< mapping resource =Userhbmxml />
</ sessionfactory >
</ hibernateconfiguration >
Userhbmxml
<? xml version= ?>
<! DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN
mappingdtd >
< hibernatemapping >
< class name =User table =user >
< id name =userId column =user_id type =javalangString length = >
<generator class=assigned />
</id>
<property name=password column=password type=javalangString length= />
</class>
</hibernatemapping>
最後別忘了修改grailsapp\controllers\UserControllergroovy以及各GSP的代碼
(試驗代碼時請不要在Edit User頁面中更新用戶的userId否則出發生異常因為主鍵不可更改
在自己的應用程序中可以disable掉Edit User頁面中的User Id文本域)
UserControllergroovy
class UserController {
def index = { redirect(action:listparams:params) }
// the delete save and update actions only
// accept POST requests
def allowedMethods = [delete: POST
save: POST
update: POST ]
def list = {
if ( ! paramsmax)paramsmax =
[ userList: Userlist( params ) ]
}
def show = {
// [ user : Userget( paramsid ) ]
[ user : UserfindByUserId(paramsid) ]
}
def delete = {
// def user = Userget( paramsid )
def user = UserfindByUserId(paramsid)
if (user) {
userdelete()
ssage = User ${paramsid} deleted
redirect(action:list)
}
else {
ssage = User not found with id ${paramsid}
redirect(action:list)
}
}
def edit = {
// def user = Userget( paramsid )
def user = UserfindByUserId(paramsid)
if ( ! user) {
ssage = User not found with id ${paramsid}
redirect(action:list)
}
else {
return [ user : user ]
}
}
def update = {
// def user = Userget( paramsid )
def user = UserfindByUserId(paramsid)
if (user) {
userproperties = params
if (usersave()) {
// redirect(action:showid:userid)
redirect(action:showid:useruserId)
}
else {
render(view: edit model:[user:user])
}
}
else {
ssage = User not found with id ${paramsid}
redirect(action:editid:paramsid)
}
}
def create = {
def user = new User()
userproperties = params
return [ user :user]
}
def save = {
def user = new User()
userproperties = params
if (usersave()) {
// redirect(action:showid:userid)
redirect(action:showid:useruserId)
}
else {
render(view: create model:[user:user])
}
}
}
grailsapp\views\user\listgsp
< html >
< head >
< meta httpequiv =ContentType content =text/html; charset=UTF />
< meta name =layout content =main />
< title > User List </ title >
</ head >
< body >
< div class =nav >
< span class =menuButton >< a href =${createLinkTo(dir:)} >
Home </ a ></ span >
< span class =menuButton >< g:link action =create >
New User </ g:link ></ span >
</ div >
< div class =body >
< h > User List </ h >
< g:if test =${ssage} >
< div class =message >
${ssage}
</ div >
</ g:if >
< table >
< thead >
< tr >
<!
<g:sortableColumn property=id title=Id />
>
< g:sortableColumn property =userId title =User Id />
< g:sortableColumn property =password title =Password />
< th ></ th >
</ tr >
</ thead >
< tbody >
< g:each in =${userList} >
< tr >
<!
<td>${itid?encodeAsHTML()}</td>
>
< td > ${ituserId?encodeAsHTML()} </ td >
< td > ${itpassword?encodeAsHTML()} </ td >
< td class =actionButtons >
<!
<span class=actionButton><g:link action=show
id=${itid}>Show</g:link></span>
>
< span class =actionButton >< g:link action =show
id =${ituserId} > Show </ g:link ></ span >
</ td >
</ tr >
</ g:each >
</ tbody >
</ table >
< div class =paginateButtons >
< g:paginate total =${unt()} />
</ div >
</ div >
</ body >
</ html >
grailsapp\views\user\showgsp
< html >
< head >
< meta httpequiv =ContentType content =text/html; charset=UTF />
< meta name =layout content =main />
< title > Show User </ title >
</ head >
< body >
< div class =nav >
< span class =menuButton >< a href =${createLinkTo(dir:)} >
Home </ a ></ span >
< span class =menuButton >< g:link action =list > User List
</ g:link ></ span >
< span class =menuButton >< g:link action =create > New User
</ g:link ></ span >
</ div >
< div class =body >
< h > Show User </ h >
< g:if test =${ssage} >
< div class =message > ${ssage} </ div >
</ g:if >
< div class =dialog >
< table >
< tbody >
<!
<tr class=prop>
<td valign=top class=name>Id:</td>
<td valign=top class=value>${userid}</td>
</tr>
>
< tr class =prop >
< td valign =top class =name > User Id: </ td >
< td valign =top class =value > ${useruserId}
</ td >
</ tr >
< tr class =prop >
< td valign =top class =name > Password: </ td >
< td valign =top class =value > ${userpassword}
</ td >
</ tr >
</ tbody >
</ table >
</ div >
< div class =buttons >
< g:form controller =user >
<!
<input type=hidden name=id value=${user?id} />
>
< input type =hidden name =id value =${user?userId} />
< span class =button >< g:actionSubmit value =Edit /></ span >
< span class =button >< g:actionSubmit value =Delete /></ span >
</ g:form >
</ div >
</ div >
</ body >
</ html >
grailsapp\views\user\creategsp
< html >
< head >
< meta httpequiv =ContentType content =text/html; charset=UTF />
< meta name =layout content =main />
< title > Create User </ title >
</ head >
< body >
< div class =nav >
< span class =menuButton >< a href =${createLinkTo(dir:)} >
Home </ a ></ span >
< span class =menuButton >< g:link action =list > User List
</ g:link ></ span >
</ div >
< div class =body >
< h > Create User </ h >
< g:if test =${ssage} >
< div class =message > ${ssage} </ div >
</ g:if >
< g:hasErrors bean =${user} >
< div class =errors >
< g:renderErrors bean =${user} as =list />
</ div >
</ g:hasErrors >
< g:form action =save method =post >
< div class =dialog >
< table >
< tbody >
< tr class =prop >< td valign =top
class =name >< label for =userId > User Id: </ label ></ td >
< td valign =top class =value ${hasErrors(bean:userfield:userIderrors)} >
< input type =text name =userId value =${user?userId?encodeAsHTML()} />
</ td ></ tr >
< tr class =prop >
< td valign =top class =name >< label for =password > Password:
</ label ></ td >< td valign =top class =value ${hasErrors(bean:userfield:password
errors)} >< input type =text name =password
value =${user?password?encodeAsHTML()} /></ td ></ tr >
</ tbody >
</ table >
</ div >
< div class =buttons >
< span class =formButton >
< input type =submit value =Create ></ input >
</ span >
</ div >
</ g:form >
</ div >
</ body >
</ html >
grailsapp\views\user\editgsp
< html >
< head >
< meta httpequiv =ContentType content =text/html; charset=UTF />
< meta name =layout content =main />
< title > Edit User </ title >
</ head >
< body >
< div class =nav >
< span class =menuButton >< a href =${createLinkTo(dir:)} >
Home </ a ></ span >
< span class =menuButton >< g:link action =list >
User List </ g:link ></ span >
< span class =menuButton >< g:link action =create >
New User </ g:link ></ span >
</ div >
< div class =body >
< h > Edit User </ h >
< g:if test =${ssage} >
< div class =message > ${ssage} </ div >
</ g:if >
< g:hasErrors bean =${user} >
< div class =errors >
< g:renderErrors bean =${user} as =list />
</ div >
</ g:hasErrors >
<!
<div class=prop>
<span class=name>Id:</span>
<span class=value>${user?id}</span>
</div>
>
< g:form controller =user method =post >
<!
<input type=hidden name=id value=${user?id} />
>
< input type =hidden name =id value =${user?userId} />
< div class =dialog >
< table >
< tbody >
< tr class =prop >< td valign =top class =name >
< label for =userId > User Id: </ label ></ td >
< td valign =top class =value ${hasErrors(bean:userfield:userIderrors)} >
< input type =text name =userId value =${user?userId?encodeAsHTML()} />
</ td ></ tr >
< tr class =prop >< td valign =top class =name >
< label for =password > Password: </ label ></ td >
< td valign =top class =value ${hasErrors(bean:userfield:passworderrors)} >
< input type =text name =password value =${user?password?encodeAsHTML()} />
</ td ></ tr >
</ tbody >
</ table >
</ div >
< div class =buttons >
< span class =button >< g:actionSubmit value =Update /></ span >
< span class =button >< g:actionSubmit value =Delete /></ span >
</ div >
</ g:form >
</ div >
</ body >
</ html >
好了整個處理過程已經呈現給大家了希望對大家有用
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26589.html