您好,登錄后才能下訂單哦!
1、Hibernate逆向工程
1.1、CDM概念模型
這里雖然用了T_Complain,但是不建議這樣做,因為后面生成JavaBean類時,會生成TComplain.java類,而不是Complain.java。
1.2、物理模型
1.3、生成的實體類和Hibernate映射文件
Complain.java
package com.rk.tax.entity; import java.sql.Timestamp; import java.util.HashSet; import java.util.Set; import java.util.Map; import java.util.HashMap; /** * TComplain entity. @author MyEclipse Persistence Tools */ public class Complain implements java.io.Serializable { // Fields private String compId; private String compCompany; private String compName; private String compMobile; private Boolean isAnonymous; private Timestamp compTime; private String compTitle; private String toCompName; private String toCompDept; private String compContent; private String state; private Set complainreplies = new HashSet(0); //狀態 public static String COMPLAIN_STATE_UNDONE = "0"; public static String COMPLAIN_STATE_DONE = "1"; public static String COMPLAIN_STATE_INVALID = "2"; public static Map<String, String> COMPLAIN_STATE_MAP; static { COMPLAIN_STATE_MAP = new HashMap<String, String>(); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_UNDONE, "待受理"); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_DONE, "已受理"); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_INVALID, "已失效"); } // Constructors /** default constructor */ public Complain() { } public String getCompId() { return compId; } public void setCompId(String compId) { this.compId = compId; } public String getCompCompany() { return compCompany; } public void setCompCompany(String compCompany) { this.compCompany = compCompany; } public String getCompName() { return compName; } public void setCompName(String compName) { this.compName = compName; } public String getCompMobile() { return compMobile; } public void setCompMobile(String compMobile) { this.compMobile = compMobile; } public Boolean getIsAnonymous() { return isAnonymous; } public void setIsAnonymous(Boolean isAnonymous) { this.isAnonymous = isAnonymous; } public Timestamp getCompTime() { return compTime; } public void setCompTime(Timestamp compTime) { this.compTime = compTime; } public String getCompTitle() { return compTitle; } public void setCompTitle(String compTitle) { this.compTitle = compTitle; } public String getToCompName() { return toCompName; } public void setToCompName(String toCompName) { this.toCompName = toCompName; } public String getToCompDept() { return toCompDept; } public void setToCompDept(String toCompDept) { this.toCompDept = toCompDept; } public String getCompContent() { return compContent; } public void setCompContent(String compContent) { this.compContent = compContent; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Set getComplainreplies() { return complainreplies; } public void setComplainreplies(Set complainreplies) { this.complainreplies = complainreplies; } }
知識點(1):
//狀態 public static String COMPLAIN_STATE_UNDONE = "0"; public static String COMPLAIN_STATE_DONE = "1"; public static String COMPLAIN_STATE_INVALID = "2"; public static Map<String, String> COMPLAIN_STATE_MAP; static { COMPLAIN_STATE_MAP = new HashMap<String, String>(); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_UNDONE, "待受理"); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_DONE, "已受理"); COMPLAIN_STATE_MAP.put(COMPLAIN_STATE_INVALID, "已失效"); }
Complain.hbm.xml
注意:在生成的映射文件中去掉 catalog=”數據庫名稱”。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.rk.tax.entity.Complain" table="t_complain"> <id name="compId" type="java.lang.String"> <column name="comp_id" length="32" /> <generator class="uuid.hex" /> </id> <property name="compCompany" type="java.lang.String"> <column name="comp_company" length="100" /> </property> <property name="compName" type="java.lang.String"> <column name="comp_name" length="20" /> </property> <property name="compMobile" type="java.lang.String"> <column name="comp_mobile" length="20" /> </property> <property name="isAnonymous" type="java.lang.Boolean"> <column name="is_anonymous" /> </property> <property name="compTime" type="java.sql.Timestamp"> <column name="comp_time" length="19" /> </property> <property name="compTitle" type="java.lang.String"> <column name="comp_title" length="200" not-null="true" /> </property> <property name="toCompName" type="java.lang.String"> <column name="to_comp_name" length="32" /> </property> <property name="toCompDept" type="java.lang.String"> <column name="to_comp_dept" length="100" /> </property> <property name="compContent" type="text"> <column name="comp_content"/> </property> <property name="state" type="java.lang.String"> <column name="state" length="1" /> </property> <set name="complainreplies" inverse="true" lazy="false" cascade="save-update" order-by="reply_time"> <key> <column name="comp_id" length="32" not-null="true" /> </key> <one-to-many class="com.rk.tax.entity.Complainreply" /> </set> </class> </hibernate-mapping>
知識點(1):
<property name="isAnonymous" type="java.lang.Boolean"> <column name="is_anonymous" /> </property>
知識點(2):
<property name="compTime" type="java.sql.Timestamp"> <column name="comp_time" length="19" /> </property>
知識點(3):
<property name="compContent" type="text"> <column name="comp_content"/> </property>
對應的類字段
private Boolean isAnonymous; private Timestamp compTime; private String compContent;
知識點(4):
<set name="complainreplies" inverse="true" lazy="false" cascade="save-update" order-by="reply_time"> <key> <column name="comp_id" length="32" not-null="true" /> </key> <one-to-many class="com.rk.tax.entity.Complainreply" /> </set>
其中
lazy="false" cascade="save-update" order-by="reply_time"
lazy="false"表示不使用懶加載,
cascade="save-update"表示級聯保存、更新
order-by="reply-time"是指取出的數據按照reply-time排序。注意:reply_time是數據庫的列的名稱,這里也可以類的字段replyTime,但是類的字段replyTime與數據庫的列名reply_time相比,類的字段replyTime可能在某些版本中存在問題,而數據庫的列名reply_time更具有通用性。
<set name="complainreplies" inverse="true" lazy="false" cascade="save-update" order-by="reply_time desc">
另外,也可以對reply-time進行排序,如使用order-by="reply-time desc",它的默認值是asc,可以省略。
Complainreply.java
package com.rk.tax.entity; import java.sql.Timestamp; /** * TComplainreply entity. @author MyEclipse Persistence Tools */ public class Complainreply implements java.io.Serializable { // Fields private String replyId; private Complain complain; private String replyer; private String replyDept; private Timestamp replyTime; private String replyContent; // Constructors /** default constructor */ public Complainreply() { } /** minimal constructor */ public Complainreply(Complain complain) { this.complain = complain; } /** full constructor */ public Complainreply(Complain complain, String replyer, String replyDept, Timestamp replyTime, String replyContent) { this.complain = complain; this.replyer = replyer; this.replyDept = replyDept; this.replyTime = replyTime; this.replyContent = replyContent; } public String getReplyId() { return replyId; } public void setReplyId(String replyId) { this.replyId = replyId; } public Complain getComplain() { return complain; } public void setComplain(Complain complain) { this.complain = complain; } public String getReplyer() { return replyer; } public void setReplyer(String replyer) { this.replyer = replyer; } public String getReplyDept() { return replyDept; } public void setReplyDept(String replyDept) { this.replyDept = replyDept; } public Timestamp getReplyTime() { return replyTime; } public void setReplyTime(Timestamp replyTime) { this.replyTime = replyTime; } public String getReplyContent() { return replyContent; } public void setReplyContent(String replyContent) { this.replyContent = replyContent; } }
Complainreply.hbm.xml
注意:在生成的映射文件中去掉 catalog=”數據庫名稱”。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.rk.tax.entity.Complainreply" table="t_complainreply"> <id name="replyId" type="java.lang.String"> <column name="reply_id" length="32" /> <generator class="uuid.hex" /> </id> <many-to-one name="complain" class="com.rk.tax.entity.Complain" fetch="select"> <column name="comp_id" length="32" not-null="true" /> </many-to-one> <property name="replyer" type="java.lang.String"> <column name="replyer" length="20" /> </property> <property name="replyDept" type="java.lang.String"> <column name="reply_dept" length="100" /> </property> <property name="replyTime" type="java.sql.Timestamp"> <column name="reply_time" length="19" /> </property> <property name="replyContent" type="java.lang.String"> <column name="reply_content" length="300" /> </property> </class> </hibernate-mapping>
2、從dao到action
dao->service->action->config
2.1、dao層
ComplainDao.java
package com.rk.tax.dao; import com.rk.core.dao.BaseDao; import com.rk.tax.entity.Complain; public interface ComplainDao extends BaseDao<Complain> { }
ComplainDaoImpl.java
package com.rk.tax.dao.impl; import com.rk.core.dao.impl.BaseDaoImpl; import com.rk.tax.dao.ComplainDao; import com.rk.tax.entity.Complain; public class ComplainDaoImpl extends BaseDaoImpl<Complain> implements ComplainDao { }
2.2、service層
ComplainService.java
package com.rk.tax.service; import com.rk.core.service.BaseService; import com.rk.tax.entity.Complain; public interface ComplainService extends BaseService<Complain> { }
ComplainServiceImpl.java
package com.rk.tax.service.impl; import java.util.Calendar; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.rk.core.service.Impl.BaseServiceImpl; import com.rk.core.utils.QueryHelper; import com.rk.tax.dao.ComplainDao; import com.rk.tax.entity.Complain; import com.rk.tax.service.ComplainService; @Service("complainService") public class ComplainServiceImpl extends BaseServiceImpl<Complain> implements ComplainService { private ComplainDao complainDao; @Resource public void setComplainDao(ComplainDao complainDao) { setBaseDao(complainDao); this.complainDao = complainDao; } }
2.3、action層
ComplainAction.java
package com.rk.tax.action; import java.net.URLDecoder; import java.sql.Timestamp; import java.util.Date; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.rk.core.action.BaseAction; import com.rk.core.utils.QueryHelper; import com.rk.tax.entity.Complain; import com.rk.tax.entity.Complainreply; import com.rk.tax.service.ComplainService; @Controller("complainAction") @Scope("prototype") public class ComplainAction extends BaseAction { /*****1、業務數據*****/ private Complain complain; private String startTime; private String endTime; private String state; private Complainreply reply; /*****2、業務實現類*****/ @Resource private ComplainService complainService; /*****3、響應頁面操作*****/ //列表 public String listUI(){ //加載狀態集合 ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP); try { QueryHelper queryHelper = new QueryHelper(Complain.class, "c"); if(StringUtils.isNotBlank(startTime)){//查詢開始時間之后的投訴數據 startTime = URLDecoder.decode(startTime, "utf-8"); queryHelper.addCondition("c.compTime >= ?", DateUtils.parseDate(startTime+":00", "yyyy-MM-dd HH:mm:ss")); } if(StringUtils.isNotBlank(endTime)){//查詢結束時間之前的投訴數據 endTime = URLDecoder.decode(endTime, "utf-8"); queryHelper.addCondition("c.compTime <= ?", DateUtils.parseDate(endTime+":00", "yyyy-MM-dd HH:mm:ss")); } if(StringUtils.isNotBlank(searchContent)){ searchContent = URLDecoder.decode(searchContent, "utf-8"); queryHelper.addCondition("c.compTitle like ?", "%" + searchContent + "%"); } if(StringUtils.isNotBlank(state)){ queryHelper.addCondition("c.state=?", state); } //按照狀態升序排序 queryHelper.addOrderByProperty("c.state", QueryHelper.ORDER_BY_ASC); //按照投訴時間升序排序 queryHelper.addOrderByProperty("c.compTime", QueryHelper.ORDER_BY_ASC); pageResult = complainService.getPageResult(queryHelper, pageNo, pageSize); } catch (Exception e) { e.printStackTrace(); } return "listUI"; } //跳轉到受理頁面 public String dealUI(){ //加載狀態集合 ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP); if(complain != null && StringUtils.isNotBlank(complain.getCompId())){ complain = complainService.findById(complain.getCompId()); } return "dealUI"; } public String deal(){ if(complain != null && StringUtils.isNotBlank(complain.getCompId())){ Complain temp = complainService.findById(complain.getCompId()); //1、更新投訴的狀態為 已受理 if(!Complain.COMPLAIN_STATE_DONE.equals(temp.getState())){ temp.setState(Complain.COMPLAIN_STATE_DONE); } //2、保存回復信息 if(reply != null){ reply.setComplain(temp); /** * 在后臺設置時間,有兩點好處: * (1)在前臺JSP頁面,不需要再提供相應的表單域 * (2)有兩個時間:JSP前臺頁面生成的時間、服務器保存數據的時間,后者更準確一些 */ reply.setReplyTime(new Timestamp(new Date().getTime())); temp.getComplainreplies().add(reply); } complainService.update(temp); } return "list"; } // Properties public Complain getComplain() { return complain; } public void setComplain(Complain complain) { this.complain = complain; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public String getState() { return state; } public void setState(String state) { this.state = state; } public Complainreply getReply() { return reply; } public void setReply(Complainreply reply) { this.reply = reply; } }
知識點(1):級聯 保存、更新
在deal()方法中
Complain temp = complainService.findById(complain.getCompId()); //1、更新投訴的狀態為 已受理 if(!Complain.COMPLAIN_STATE_DONE.equals(temp.getState())){ temp.setState(Complain.COMPLAIN_STATE_DONE); } //2、保存回復信息 if(reply != null){ reply.setComplain(temp); /** * 在后臺設置時間,有兩點好處: * (1)在前臺JSP頁面,不需要再提供相應的表單域 * (2)有兩個時間:JSP前臺頁面生成的時間、服務器保存數據的時間,后者更準確一些 */ reply.setReplyTime(new Timestamp(new Date().getTime())); temp.getComplainreplies().add(reply); } complainService.update(temp);
最后只調用complainService.update(temp),而temp是Complain類型的變量,而真正想要保存的的reply變量,這是通過級聯保存(更新)實現的,Complain.hbm.xml中設置了cascade="save-update"
<set name="complainreplies" inverse="true" lazy="false" cascade="save-update" order-by="reply_time"> <key> <column name="comp_id" length="32" not-null="true" /> </key> <one-to-many class="com.rk.tax.entity.Complainreply" /> </set>
2.4、config
entity層的配置就是*.hbm.xml文件
dao層的配置是將dao類加入到Spring的IOC容器中
service層的配置是將service類加入到Spring的IOC容器中
action層的配置是將action類加入到Spring的IOC容器中,并在struts中對action進行url映射
最后記得將spring和struts的配置都匯總到applicationContext.xml和struts.xml文件中
3、JSP頁面
listUI.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%@include file="/common/header.jsp"%> <title>投訴受理管理</title> <script type="text/javascript" src="${basePath}/js/datepicker/WdatePicker.js"></script> <script type="text/javascript"> var list_url = "${basePath}/tax/complain_listUI.action"; function doAnnualStatistic(){ // TODO } function doDeal(id){ document.forms[0].action = "${basePath}/tax/complain_dealUI.action?complain.compId="+id; document.forms[0].submit(); } </script> </head> <body class="rightBody"> <form name="form1" action="" method="post"> <div class="p_d_1"> <div class="p_d_1_1"> <div class="content_info"> <div class="c_crumbs"><div><b></b><strong>投訴受理管理</strong></div> </div> <div class="search_art"> <li> 投訴標題:<s:textfield name="searchContent" cssClass="s_text" cssStyle="width:160px;"/> </li> <li> 投訴時間:<s:textfield id="startTime" name="startTime" cssClass="s_text" cssStyle="width:160px;" readonly="true" onfocus="WdatePicker({'skin':'whyGreen','dateFmt':'yyyy-MM-dd HH:mm'})"/> - <s:textfield id="endTime" name="endTime" cssClass="s_text" cssStyle="width:160px;" readonly="true" onfocus="WdatePicker({'skin':'whyGreen','dateFmt':'yyyy-MM-dd HH:mm'})"/> </li> <li> 狀態:<s:select name="state" list="#complainStateMap" headerKey="" headerValue="全部"/> </li> <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li> <li > <input type="button" value="統計" class="s_button" onclick="doAnnualStatistic()"/> </li> </div> <div class="t_list" > <table width="100%" border="0"> <tr class="t_tit"> <td align="center">投訴標題</td> <td width="120" align="center">被投訴部門</td> <td width="120" align="center">被投訴人</td> <td width="140" align="center">投訴時間</td> <td width="100" align="center">受理狀態</td> <td width="100" align="center">操作</td> </tr> <s:iterator value="pageResult.items" status="st"> <tr <s:if test="#st.odd"> bgcolor="f8f8f8" </s:if> > <td align="center"><s:property value="compTitle"/></td> <td align="center"><s:property value="toCompDept"/></td> <td align="center"><s:property value="toCompName"/></td> <td align="center"><s:date name="compTime" format="yyyy-MM-dd HH:mm"/></td> <td align="center"><s:property value="#complainStateMap[state]"/></td> <td align="center"> <a href="javascript:doDeal('<s:property value='compId'/>')">受理</a> </td> </tr> </s:iterator> </table> </div> </div> <%@include file="/common/pageNavigator.jsp" %> </div> </div> </form> </body> </html>
知識點(1):WDatePicker
<script type="text/javascript" src="${basePath}/js/datepicker/WdatePicker.js"></script>
<s:textfield id="startTime" name="startTime" cssClass="s_text" cssStyle="width:160px;" readonly="true" onfocus="WdatePicker({'skin':'whyGreen','dateFmt':'yyyy-MM-dd HH:mm'})"/>
知識點(2):<s:select>
<s:select name="state" list="#complainStateMap" headerKey="" headerValue="全部"/>
dealUI.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%@include file="/common/header.jsp"%> <title>投訴受理管理</title> </head> <body class="rightBody"> <form id="form" name="form" action="${basePath}/tax/complain_deal.action" method="post" enctype="multipart/form-data"> <div class="p_d_1"> <div class="p_d_1_1"> <div class="content_info"> <div class="c_crumbs"><div><b></b><strong>投訴受理管理</strong> - 投訴受理</div></div> <div class="tableH2">投訴詳細信息<span >(已受理)</span></div> <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0" > <tr><td colspan="2" align="center">投訴人信息</td></tr> <tr> <td class="tdBg" width="250px">是否匿名投訴:</td> <td><s:property value="complain.isAnonymous?'匿名投訴':'非匿名投訴'"/></td> </tr> <tr> <td class="tdBg">投訴人單位:</td> <td> <s:if test="!complain.isAnonymous"> <s:property value="complain.compCompany"/> </s:if> </td> </tr> <tr> <td class="tdBg">投訴人姓名:</td> <td> <s:if test="!complain.isAnonymous"> <s:property value="complain.compName"/> </s:if> </td> </tr> <tr> <td class="tdBg">投訴人手機:</td> <td> <s:if test="!complain.isAnonymous"> <s:property value="complain.compMobile"/> </s:if> <s:elseif test="complain.compMobile.length() > 10"> <s:property value="complain.compMobile.substring(0,3) + '****' + complain.compMobile.substring(7,11)"/> </s:elseif> <s:else> <s:property value="complain.compMobile"/> </s:else> </td> </tr> <tr><td colspan="2" align="center">投訴信息</td></tr> <tr> <td class="tdBg">投訴時間:</td> <td> <s:date name="complain.compTime" format="yyyy-MM-dd HH:mm"/> </td> </tr> <tr> <td class="tdBg">被投訴部門:</td> <td><s:property value="complain.toCompDept"/></td> </tr> <tr> <td class="tdBg">被投訴人:</td> <td><s:property value="complain.toCompName"/></td> </tr> <tr> <td class="tdBg">投訴標題:</td> <td><s:property value="complain.compTitle"/></td> </tr> <tr> <td class="tdBg">投訴內容:</td> <td><s:property value="complain.compContent" escape="false"/></td> </tr> <tr><td colspan="2" align="center">受理信息</td></tr> <tr> <td colspan="2"> <s:iterator value="complain.complainreplies" status="st"> <fieldset ><legend >回復<s:property value="#st.count"/> </legend> <div > 回復部門:<s:property value="replyDept"/> 回復人:<s:property value="replyer"/> 回復時間:<s:date name="replyTime" format="yyyy-MM-dd HH:mm"/> </div> <div > <s:property value="replyContent"/> </div> </fieldset> </s:iterator> </td> </tr> <tr><td colspan="2" align="center">受理操作</td></tr> <tr> <td class="tdBg">回復部門:</td> <td> <s:property value="#session.SYS_USER.dept"/> <s:hidden name="reply.replyDept" value="%{#session.SYS_USER.dept}"/> </td> </tr> <tr> <td class="tdBg">回復人:</td> <td> <s:property value="#session.SYS_USER.name"/> <s:hidden name="reply.replyer" value="%{#session.SYS_USER.name}"/> </td> </tr> <tr> <td class="tdBg" width="200px">回復內容:</td> <td><s:textarea name="reply.replyContent" cols="90" rows="8" /></td> </tr> </table> <s:hidden name="complain.compId"/> <div class="tc mt20"> <input type="submit" class="btnB2" value="保存" /> <input type="button" onclick="javascript:history.go(-1)" class="btnB2" value="返回" /> </div> </div></div></div> </form> </body> </html>
知識點(1):OGNL字符中長度判斷 和 拼接
<s:elseif test="complain.compMobile.length() > 10"> <s:property value="complain.compMobile.substring(0,3) + '****' + complain.compMobile.substring(7,11)"/> </s:elseif>
知識點(2):<s:date>
<s:date name="complain.compTime" format="yyyy-MM-dd HH:mm"/>
知識點(3):<s:property>和<s:hidden> 似乎對OGNL的支持不同
<s:property value="#session.SYS_USER.name"/> <s:hidden name="reply.replyer" value="%{#session.SYS_USER.name}"/>
知識點(4):escape
<td class="tdBg">投訴內容:</td> <td><s:property value="complain.compContent" escape="false"/></td>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。