本文主要是介绍【SSH项目实战】国税协同平台-29.投诉受理查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
现在我们要对建好的表使用“逆向工程”来创建相应的实体类以及映射文件。我们的“逆向工程”在第五阶段“5.逆向工程.txt”章中已经详细阐述过,这里的过程就不再赘述。
首先我们还是调出我们的DB Browser,然后找到我们的数据库连接,找到要逆向工程的两张表,点击右键,选择“hibernate Reverse Engineering...”之后就会弹出以下对话框:
做出相应的操作之后,我们的实体类以及映射文件就自动生成完毕了:
其中我们实体集以及映射文件的详细内容如下:
Complain.java:
- public class Complain implements java.io.Serializable {
- // Fields
- private String compId;
- private String compCompany;
- private String compName;
- private String compMobile;
- private Boolean isNm;
- private Timestamp compTime;
- private String compTitle;
- private String toCompName;
- private String toCompDept;
- private String compContent;
- private String state;
- private Set complainReplies = new HashSet(0);
- // Constructors
- /** default constructor */
- public Complain() {
- }
- /** minimal constructor */
- public Complain(String compTitle) {
- this.compTitle = compTitle;
- }
- /** full constructor */
- public Complain(String compCompany, String compName, String compMobile, Boolean isNm, Timestamp compTime, String compTitle, String toCompName, String toCompDept, String compContent, String state,
- Set complainReplies) {
- this.compCompany = compCompany;
- this.compName = compName;
- this.compMobile = compMobile;
- this.isNm = isNm;
- this.compTime = compTime;
- this.compTitle = compTitle;
- this.toCompName = toCompName;
- this.toCompDept = toCompDept;
- this.compContent = compContent;
- this.state = state;
- this.complainReplies = complainReplies;
- }
- // 下面是get和set方法,这里不再赘述
- }
Complain.hbm.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="cn.edu.hpu.tax.complain.entity.Complain" table="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="isNm" type="java.lang.Boolean">
- <column name="is_NM" />
- </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="20" />
- </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" cascade="save-update,delete" lazy="false" order-by="reply_time">
- <key>
- <column name="comp_id" length="32" not-null="true" />
- </key>
- <one-to-many class="cn.edu.hpu.tax.complain.entity.ComplainReply" />
- </set>
- </class>
- </hibernate-mapping>
ComplainReply.java:
- package cn.edu.hpu.tax.complain.entity;
- import java.sql.Timestamp;
- /**
- * ComplainReply 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;
- }
- // 下面是get和set方法,这里不再赘述
- }
ComplainReply.hbm.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="cn.edu.hpu.tax.complain.entity.ComplainReply" table="complain_reply">
- <id name="replyId" type="java.lang.String">
- <column name="reply_id" length="32" />
- <generator class="uuid.hex" />
- </id>
- <many-to-one name="complain" class="cn.edu.hpu.tax.complain.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>
可以看到我们正确生成了实体集以及映射文件。
然后我们的Complain有一些状态,我们来为它添加这些状态(就在Complain.java中):
- //状态
- 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, "已失效");
- }
实体集写完,之后是Dao层:
- package cn.edu.hpu.tax.complain.dao;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.core.dao.BaseDao;
- public interface ComplainDao extends BaseDao<Complain> {
- }
- package cn.edu.hpu.tax.complain.dao.impl;
- import cn.edu.hpu.tax.complain.dao.ComplainDao;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.core.dao.impl.BaseDaoImpl;
- public class ComplainDaoImpl extends BaseDaoImpl<Complain> implements ComplainDao{
- }
然后是Service层:
- package cn.edu.hpu.tax.complain.service;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.core.service.BaseService;
- public interface ComplainService extends BaseService<Complain> {
- }
- package cn.edu.hpu.tax.complain.service.impl;
- import javax.annotation.Resource;
- import org.springframework.stereotype.Service;
- import cn.edu.hpu.tax.complain.dao.ComplainDao;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.complain.service.ComplainService;
- import cn.edu.hpu.tax.core.service.impl.BaseServiceImpl;
- @Service("complainService")
- public class ComplainServiceImpl extends BaseServiceImpl<Complain> implements
- ComplainService {
- private ComplainDao complainDao;
- @Resource
- public void setComplainDao(ComplainDao complainDao) {
- super.setBaseDao(complainDao);
- this.complainDao = complainDao;
- }
- }
之后使我们的Action:
- package cn.edu.hpu.tax.complain.action;
- import javax.annotation.Resource;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.complain.service.ComplainService;
- import cn.edu.hpu.tax.core.action.BaseAction;
- import cn.edu.hpu.tax.core.util.QueryHelper;
- public class ComplainAction extends BaseAction {
- @Resource
- private ComplainService complainService;
- private Complain complain;
- //列表
- public String listUI(){
- //加载状态集合
- ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP);
- QueryHelper queryHelper=new QueryHelper(Complain.class,"c");
- pageResult=complainService.getPageResult(queryHelper, getPageNo(), getPageSize());
- return "listUI";
- }
- public Complain getComplain() {
- return complain;
- }
- public void setComplain(Complain complain) {
- this.complain = complain;
- }
- }
然后使我们的配置文件:
complain-spring.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 继承了注入sessionFactory的抽象类,不用反复出入sessionFactory -->
- <bean id="complainDao" class="cn.edu.hpu.tax.complain.dao.impl.ComplainDaoImpl" parent="xDao"></bean>
- <!-- 扫描Service -->
- <context:component-scan base-package="cn.edu.hpu.tax.complain.service.impl"></context:component-scan>
- </beans>
complain-struts.xml:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="complain-action" namespace="/tax" extends="base-default">
- <action name="complain_*" class="cn.edu.hpu.tax.complain.action.ComplainAction" method="{1}">
- <result name="{1}">/WEB-INF/jsp/tax/complain/{1}.jsp</result>
- <result name="list" type="redirectAction">
- <param name="actionName">complain_listUI</param>
- <param name="complain.name">${strTitle}</param>
- <param name="pageNo">${pageNo}</param>
- <param name="encode">true</param><!-- 需要编码 -->
- </result>
- </action>
- </package>
- </struts>
然后将complain-spring.xml加入总配置文件中:
- <!-- 投诉受理的struts配置文件 -->
- <include file="cn/edu/hpu/tax/complain/conf/complain-struts.xml"/>
然后将我们美工做的前台页面加入到WEB-INF/jsp/tax/complain文件夹下:
首先是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 doSearch(){
- //重置页号
- $("#pageNo").val(1);
- document.forms[0].action = list_url;
- document.forms[0].submit();
- }
- //受理
- function doDeal(compId){
- document.forms[0].action = "${basePath}tax/complain_dealUI.action?complain.compId=" + compId;
- 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="complain.compTitle" 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="complain.state" list="#complainStateMap" headerKey="" headerValue="全部"/>
- </li>
- <li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li>
- <li style="float:right;">
- <input type="button" value="统计" class="s_button" onclick="doAnnualStatistic()"/>
- </li>
- </div>
- <div class="t_list" style="margin:0px; border:0px none;">
- <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>
- <jsp:include page="/common/pageNavigator.jsp"/>
- </div>
- </div>
- </form>
- </body>
- </html>
然后在侧边栏left.jsp中添加功能链接:
- <dl class="">
- <dt><a class="tssl" href="${ctx }tax/complain_listUI.action" target="mainFrame"><b></b>投诉受理管理<s lass="down"></s> </a>
- </dt>
- </dl>
我们添加几个测试数据(complain表):
重启服务器测试:
测试成功!我们接下来完成它的条件查询功能。我们从“标题”、“时间段”和“状态”三个条件去查询,所以我们的查询功能和以往单一查询条件不太一样,我们要做到“条件合并查询”和“防止查询条件和当前页码被覆盖”这两个重要过程。
(PS:我们的时间段是这么理解的:
投诉时间 【开始时间】 - 【结束时间】
查询开始时间之后的投诉记录
查询结束时间之前的投诉记录
)
我们下面来完善我们的Action,使其能够进行正常的查询功能(我们添加了开始时间startTime、结束时间endTime条件的查询,以及排序):
- package cn.edu.hpu.tax.complain.action;
- import java.net.URLDecoder;
- import javax.annotation.Resource;
- import org.apache.commons.lang.time.DateUtils;
- import org.apache.commons.lang3.StringUtils;
- import cn.edu.hpu.tax.complain.entity.Complain;
- import cn.edu.hpu.tax.complain.service.ComplainService;
- import cn.edu.hpu.tax.core.action.BaseAction;
- import cn.edu.hpu.tax.core.util.QueryHelper;
- import com.opensymphony.xwork2.ActionContext;
- public class ComplainAction extends BaseAction {
- @Resource
- private ComplainService complainService;
- private Complain complain;
- private String startTime;
- private String endTime;
- //列表
- public String listUI() throws Exception{
- //加载状态集合
- ActionContext.getContext().getContextMap().put("complainStateMap", Complain.COMPLAIN_STATE_MAP);
- QueryHelper queryHelper=new QueryHelper(Complain.class,"c");
- if(StringUtils.isNoneBlank(startTime)){//查询开始时间之后的投诉数据
- startTime=URLDecoder.decode(startTime,"utf-8");
- queryHelper.addCondition("c.compTime > ?",DateUtils.parseDate(startTime, new String[]{"yyyy-MM-dd HH:mm"}));
- }
- if(StringUtils.isNoneBlank(endTime)){//查询结束时间之前的投诉数据
- endTime=URLDecoder.decode(endTime,"utf-8");
- queryHelper.addCondition("c.compTime < ?",DateUtils.parseDate(endTime, new String[]{"yyyy-MM-dd HH:mm"}));
- }
- if(complain!=null){
- if(StringUtils.isNoneBlank(complain.getState())){
- queryHelper.addCondition("c.state=?", complain.getState());
- }
- if(StringUtils.isNoneBlank(complain.getCompTitle())){
- complain.setCompTitle(URLDecoder.decode(complain.getCompTitle(),"utf-8"));
- queryHelper.addCondition("c.compTitle like ?", "%"+complain.getCompTitle()+"%");
- }
- }
- //按照状态升序排序(待受理排前,已受理和过期在后)
- queryHelper.addOrderByProperty("c.state", QueryHelper.ORDER_BY_ASC);
- //按照投诉时间升序排序
- queryHelper.addOrderByProperty("c.compTime", QueryHelper.ORDER_BY_ASC);
- pageResult=complainService.getPageResult(queryHelper, getPageNo(), getPageSize());
- return "listUI";
- }
- 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;
- }
- }
虽然我们暂时还没有做回显,但是我们可以先测试一下我们的分段多条件查询是否成功:
我们的分段多条件查询功能编写成功
这篇关于【SSH项目实战】国税协同平台-29.投诉受理查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!