<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    First they ignore you
    then they ridicule you
    then they fight you
    then you win
        -- Mahatma Gandhi
    Chinese => English     英文 => 中文             
    隨筆-221  評論-1047  文章-0  trackbacks-0
    由于在過去一段時間內,已有多位朋友向我詢問如何用Grails處理遺留數據庫,為了回答這個問題。我給出一個實例,并適當講解,不足之處,敬請諒解。

    在Grails0.6+中,配置稍有不同,詳見 朝花夕拾——Groovy & Grails

    我使用的數據庫為MySQL5,其中存在一個名為legacy_dev的schema,legacy_dev中有一張表叫user:



    1,創建Grails應用程序,我將它命名為legacy:grails create-app legacy
    2,創建一個域類User:grails create-domain-class User
    3,修改grails-app\domain\User.groovy的內容,如下所示:
    class ?User?{?
    ????String?userId
    ????String?password
    ????
    ????
    static ?constraints? = ?{
    ????????userId(blank:
    false ,?maxSize: 16 )????
    ????????password(blank:
    false ,?maxSize: 45 )
    ????}
    }????

    4,生成與域類User相關的所有Grails應用程序工件(artifact):grails generate-all User
    5,將grails-app\conf\DevelopmentDataSource.groovy的內容改為:

    class ?DevelopmentDataSource?{
    ???
    boolean ?pooling? = ? true
    ???
    // ?將這行注釋掉
    ???
    // String?dbCreate?=?'update'? // ?one?of?'create',?'create-drop','update'
    ???
    // ?url和driver要正確
    ???String?url? = ? " jdbc:mysql://localhost:3306/legacy_dev "
    ???String?driverClassName?
    = ? " com.mysql.jdbc.Driver "
    ???String?username?
    = ? " root "
    ???String?password?
    = ? "" ? // ?這里為您的密碼?:)
    }

    6,自行配置Hibernate:
    hibernate.cfg.xml
    <? xml?version='1.0'?encoding='UTF-8' ?>
    <! DOCTYPE?hibernate-configuration?PUBLIC
    ??????????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"
    ??????????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
    >

    < hibernate-configuration >
    ????
    < session-factory >
    ????????
    < property? name ="connection.driver_class" > com.mysql.jdbc.Driver </ property >
    ????????
    < property? name ="connection.url" > jdbc:mysql://localhost:3306/legacy_dev </ property >
    ????????
    < property? name ="connection.username" > root </ property >
    ????????
    < property? name ="connection.password" ></ property >
    ????????
    < property? name ="connection.pool_size" > 1 </ property >
    ????????
    < property? name ="dialect" > org.hibernate.dialect.MySQLDialect </ property >
    ????????
    < property? name ="current_session_context_class" > thread </ property >
    ????????
    < property? name ="cache.provider_class" > org.hibernate.cache.NoCacheProvider </ property >
    ????????
    < property? name ="show_sql" > true </ property >
    ????????
    < property? name ="hbm2ddl.auto" > validate </ property >
    ????????
    < mapping? resource ="User.hbm.xml" />
    ????
    </ session-factory >
    </ hibernate-configuration >

    User.hbm.xml
    <? xml?version="1.0" ?>
    <! DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
    >

    < hibernate-mapping >
    ????
    < class? name ="User" ?table ="user" >
    ????????
    < id? name ="userId" ?column ="user_id" ?type ="java.lang.String" ?length ="16" >
    ??????????
    ?? <generator?class="assigned"?/>?
    ????????
    </id>
    ????????
    <property?name="password"?column="password"?type="java.lang.String"?length="45"?/>?
    ????
    </class>
    </hibernate-mapping>

    最后,別忘了修改grails-app\controllers\UserController.groovy以及各GSP的代碼
    (試驗代碼時,請不要在Edit User頁面中更新用戶的userId,否則出發生異常,因為‘主鍵’不可更改。
    在自己的應用程序中,可以disable掉Edit User頁面中的User Id文本域)

    UserController.groovy
    ????????????
    class ?UserController?{
    ????def?index?
    = ?{?redirect(action:list,params:params)?}

    ????
    // ?the?delete,?save?and?update?actions?only
    ????
    // ?accept?POST?requests
    ????def?allowedMethods? = ?[delete: ' POST ' ,
    ??????????????????????????save:
    ' POST ' ,
    ??????????????????????????update:
    ' POST ' ]

    ????def?list?
    = ?{
    ????????
    if ( ! params.max)params.max? = ? 10
    ????????[?userList:?User.list(?params?)?]
    ????}

    ????def?show?
    = ?{
    ????????
    // [?user?:?User.get(?params.id?)?]
    ????????[?user?:?User.findByUserId(params.id)?]
    ????}

    ????def?delete?
    = ?{
    ????????
    // def?user?=?User.get(?params.id?)
    ????????def?user? = ?User.findByUserId(params.id)
    ????????
    if (user)?{
    ????????????user.delete()
    ????????????flash.message?
    = ? " User?${params.id}?deleted. "
    ????????????redirect(action:list)
    ????????}
    ????????
    else ?{
    ????????????flash.message?
    = ? " User?not?found?with?id?${params.id} "
    ????????????redirect(action:list)
    ????????}
    ????}

    ????def?edit?
    = ?{
    ????????
    // def?user?=?User.get(?params.id?)
    ????????def?user? = ?User.findByUserId(params.id)
    ????????
    if ( ! user)?{
    ????????????????flash.message?
    = ? " User?not?found?with?id?${params.id} "
    ????????????????redirect(action:list)
    ????????}
    ????????
    else ?{
    ????????????
    return ?[?user?:?user?]
    ????????}
    ????}


    ????def?update?
    = ?{
    ????????
    // def?user?=?User.get(?params.id?)
    ????????def?user? = ?User.findByUserId(params.id)
    ????????
    if (user)?{
    ????????????user.properties?
    = ?params
    ????????????
    if (user.save())?{
    ????????????????
    // redirect(action:show,id:user.id)
    ????????????????redirect(action:show,id:user.userId)
    ????????????}
    ????????????
    else ?{
    ????????????????render(view:
    ' edit ' ,model:[user:user])
    ????????????}
    ????????}
    ????????
    else ?{
    ????????????flash.message?
    = ? " User?not?found?with?id?${params.id} "
    ????????????redirect(action:edit,id:params.id)
    ????????}
    ????}

    ????def?create?
    = ?{
    ????????def?user?
    = ? new ?User()
    ????????user.properties?
    = ?params
    ????????
    return ?[ ' user ' :user]
    ????}

    ????def?save?
    = ?{
    ????????def?user?
    = ? new ?User()
    ????????user.properties?
    = ?params
    ????????
    if (user.save())?{
    ????????????
    // redirect(action:show,id:user.id)
    ????????????redirect(action:show,id:user.userId)
    ????????}
    ????????
    else ?{
    ????????????render(view:
    ' create ' ,model:[user:user])
    ????????}
    ????}

    }

    grails-app\views\user\list.gsp
    ??
    < html >
    ????
    < head >
    ?????????
    < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" />
    ?????????
    < 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" >
    ???????????
    < h1 > User?List </ h1 >
    ????????????
    < g:if? test ="${flash.message}" >
    ?????????????????
    < div? class ="message" >
    ???????????????????????${flash.message}
    ?????????????????
    </ 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>${it.id?.encodeAsHTML()}</td>
    ????????????
    -->
    ???????????????????????
    ????????????????????????????
    < td > ${it.userId?.encodeAsHTML()} </ td >
    ???????????????????????
    ????????????????????????????
    < td > ${it.password?.encodeAsHTML()} </ td >
    ???????????????????????
    ???????????????????????
    < td? class ="actionButtons" >
    ????????????????
    <!--
    ????????????????????????????<span?class="actionButton"><g:link?action="show"?id="${it.id}">Show</g:link></span>
    ????????????????
    -->
    ????????????????
    < span? class ="actionButton" >< g:link? action ="show" ?id ="${it.userId}" > Show </ g:link ></ span >
    ???????????????????????
    </ td >
    ????????????????????
    </ tr >
    ???????????????
    </ g:each >
    ?????????????
    </ tbody >
    ???????????
    </ table >
    ???????????????
    < div? class ="paginateButtons" >
    ???????????????????
    < g:paginate? total ="${User.count()}" ? />
    ???????????????
    </ div >
    ????????
    </ div >
    ????
    </ body >
    </ html >

    grails-app\views\user\show.gsp
    ??
    < html >
    ????
    < head >
    ?????????
    < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" />
    ??????????
    < 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" >
    ???????????
    < h1 > Show?User </ h1 >
    ???????????
    < g:if? test ="${flash.message}" >
    ?????????????????
    < div? class ="message" > ${flash.message} </ div >
    ???????????
    </ g:if >
    ???????????
    < div? class ="dialog" >
    ?????????????????
    < table >
    ???????????????????
    ???????????????????
    < tbody >
    ???????????????????
    <!--
    ????????????????????????<tr?class="prop">
    ??????????????????????????????<td?valign="top"?class="name">Id:</td>
    ??????????????????????????????
    ????????????????????????????????????<td?valign="top"?class="value">${user.id}</td>
    ??????????????????????????????
    ????????????????????????</tr>
    ???????????????????
    -->
    ????????????????????????
    < tr? class ="prop" >
    ??????????????????????????????
    < td? valign ="top" ?class ="name" > User?Id: </ td >
    ??????????????????????????????
    ????????????????????????????????????
    < td? valign ="top" ?class ="value" > ${user.userId} </ td >
    ??????????????????????????????
    ????????????????????????
    </ tr >
    ???????????????????
    ????????????????????????
    < tr? class ="prop" >
    ??????????????????????????????
    < td? valign ="top" ?class ="name" > Password: </ td >
    ??????????????????????????????
    ????????????????????????????????????
    < td? valign ="top" ?class ="value" > ${user.password} </ 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 >

    grails-app\views\user\create.gsp
    ??
    < html >
    ????
    < head >
    ?????????
    < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" />
    ?????????
    < 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" >
    ???????????
    < h1 > Create?User </ h1 >
    ???????????
    < g:if? test ="${flash.message}" >
    ?????????????????
    < div? class ="message" > ${flash.message} </ 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:user,field:'userId','errors')}' >< 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:user,field:'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 >

    grails-app\views\user\edit.gsp
    ??
    < html >
    ????
    < head >
    ?????????
    < meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" />
    ?????????
    < 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" >
    ???????????
    < h1 > Edit?User </ h1 >
    ???????????
    < g:if? test ="${flash.message}" >
    ?????????????????
    < div? class ="message" > ${flash.message} </ 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:user,field:'userId','errors')}' >< 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:user,field:'password','errors')}' >< 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 >


    好了,整個處理過程已經呈現給大家了,希望對大家有用 :)

    附:朝花夕拾——Groovy & Grails
    posted on 2007-06-06 19:45 山風小子 閱讀(4120) 評論(5)  編輯  收藏 所屬分類: Groovy & Grails
    主站蜘蛛池模板: 精品少妇人妻AV免费久久洗澡| 好看的亚洲黄色经典| 一本到卡二卡三卡免费高 | 欧美大尺寸SUV免费| 亚洲av日韩综合一区久热| 亚洲乱色熟女一区二区三区丝袜| 亚洲人成免费网站| 免费很黄无遮挡的视频毛片| 亚洲国产精品一区| 国产一级淫片视频免费看| 污视频在线免费观看| 国产成人综合亚洲绿色| 91大神亚洲影视在线| 日韩精品电影一区亚洲| 免费人成在线观看69式小视频| 免费一区二区无码视频在线播放| 亚洲综合免费视频| 亚洲免费日韩无码系列| 国产福利在线免费| 国内精品一级毛片免费看| 国产成人高清亚洲一区91| 亚洲国产电影在线观看| 久久影院亚洲一区| 日韩高清免费观看| 可以免费看黄的网站| 久热免费在线视频| 一级特黄录像免费播放中文版| 亚洲中文字幕无码爆乳app| 99久久亚洲精品无码毛片| 亚洲免费视频一区二区三区| 日韩免费一区二区三区| 无码区日韩特区永久免费系列| 最近中文字幕免费大全| 免费一级全黄少妇性色生活片 | 亚洲第一成年网站大全亚洲| 亚洲中久无码不卡永久在线观看| 女性无套免费网站在线看| 69视频在线是免费观看| 在线观看片免费人成视频播放| 污视频网站免费在线观看| 亚洲熟妇无码一区二区三区导航|