由于在过去一段时间内
已有多位朋友向我询问如何用Grails处理遗留数据库
为了回答这个问题
我给出一个实例
并适当讲解
不足之处
敬请谅解
我使用的数据库为MySQL其中存在一个名为legacy_dev的schemalegacy_dev中有一张表叫user
创建Grails应用程序我将它命名为legacygrails createapp legacy
创建一个域类Usergrails createdomainclass User
修改grailsapp\domain\Usergroovy的内容如下所示
class User {String userIdString passwordstatic 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 //ENconfigurationdtd >< 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 //ENmappingdtd >< 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 requestsdef 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 = paramsif (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 = paramsreturn [ user :user]}def save = {def user = new User()userproperties = paramsif (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 >
好了整个处理过程已经呈现给大家了希望对大家有用