CRM第五天:联系人管理,修改联系人,删除联系人,按条件查询联系人和通用Dao的抽取

目录

1.CRM联系人管理-修改联系人

1.1编写修改页面

1.2编写action

1.3编写service

1.4编写Dao

2.CRM联系人管理-删除联系人

2.1编写jsp页面

2.2编写action

2.3编写service

2.4编写Dao

3.CRM联系人管理-条件查询

3.1页面修改:

3.2Action中findAll方法修改

4.CRM联系人管理-级联问题解决

5.CRM联系人管理-抽取通用Dao

5.1定义通用Dao接口

5.2通过泛型的反射抽取通用的DAO

5.3使用通用的Dao


 

 

1.CRM联系人管理-修改联系人

1.1编写修改页面

Html:

<td><input type="button" name="button"

                                               class="btn btn82 btn_add" id="modifyBtn"

                                               onclick="toUpdate('<s:property value="lkm_id"/>')" value="修改">

                                               <input type="button" name="button" class="btn btn82 btn_del"

                                               onclick="toDelete('<s:property value="lkm_id"/>')" value="删除"></td>

<!-- 修改联系人窗口 -->

<div id="updateBox" class="update" style="display: none;">

         <form

              action="${pageContext.request.contextPath }/linkMan_update.action"

              method="post" enctype="multipart/form-data" class="jqtransform">

              <input type="hidden" name="lkm_id" id="lkm_id"> <input

                   type="hidden" name="cust_image" id="cust_image">

              <table class="form_table pt15 pb15" width="100%" border="0"

                   cellpadding="0" cellspacing="0">

                   <tr>

                       <td class="td_right">所属客户:</td>

                       <td class=""><span class="fl">

                                 <div class="select_border">

                                     <div class="select_containers ">

                                          <select name="customer.cust_id" id="lkm_cust" class="select">

                                               <option value="">请选择</option>

                                          </select>

                                     </div>

                                 </div>

                       </span></td>

                   </tr>



                   <tr>

                       <td class="td_right">联系人名称:</td>

                       <td class=""><input type="text" name="lkm_name" id="lkm_name"

                            class="input-text lh30" required="required"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人性别:</td>

                       <td class=""><input type="radio" name="lkm_gender" value="1">

                            男 <input type="radio" name="lkm_gender" value="2">女</td>

                   </tr>

                   <tr>

                       <td class="td_right">联系人电话:</td>

                       <td class=""><input type="number" name="lkm_phone"

                            id="lkm_phone" class="input-text lh30"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人手机:</td>

                       <td class=""><input type="number" name="lkm_mobile"

                            id="lkm_mobile" class="input-text lh30"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人邮箱:</td>

                       <td class=""><input type="email" name="lkm_email"

                            id="lkm_email" class="input-text lh30"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人QQ:</td>

                       <td class=""><input type="number" name="lkm_qq" id="lkm_qq"

                            class="input-text lh30"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人职位:</td>

                       <td class=""><input type="text" name="lkm_position"

                            id="lkm_position" class="input-text lh30"></td>



                   </tr>

                   <tr>

                       <td class="td_right">联系人备注:</td>

                       <td class=""><textarea name="lkm_memo" id="lkm_memo" cols="30"

                                 rows="10" class="textarea" style="width: 200px; height: 93px;"></textarea>

                       </td>

                   </tr>









                   <tr>

                       <td class="td_right">&nbsp;</td>

                       <td class=""><input id="updateForm" type="submit"

                            name="button" class="btn btn82 btn_save2" value="保存"> <input

                            type="reset" name="button" class="btn btn82 btn_res" value="重置"></td>

                   </tr>

              </table>

         </form>



     </div>

JS:

// 修改联系人弹窗

     function toUpdate(id) {

         linkManInfo(id);



         var content = $('#updateBox').html();



         var btnFn = function() {

              $("input#updateForm").click();

              return false;

         };



         easyDialog.open({

              container : {

                   header : '修改用户信息',

                   content : content,

                   yesFn : btnFn,

                   noFn : true

              }

         });

         // $('.easyDialog_wrapper').css('width','400px');

         $('.easyDialog_text').css('height', '350px');



     };

     //异步请求加载要修改的联系人的信息

     function linkManInfo(id) {

         // 加载联系人信息

         $.post("${pageContext.request.contextPath }/linkMan_edit.action", {

              "lkm_id" : id

         }, function(data) {

              //遍历JOSN类型的数据

              $.each(data, function(name, value) {

                   if (name == "lkm_gender") {

                       //回显性别

                       $("input:radio[name=" + name + "][value=" + value + "]")

                                 .attr("checked", true);

                   }

                   $("select#" + name + " option[value='" + value + "']").prop(

                            "selected", "selected");

                   $("input#" + name).val(value);

                   $("textarea#" + name).val(value);



              });



         }, "json");

     }

     //页面加载,异步查询所有客户

     //页面加载函数就会执行:

     $(function() {

         // 加载客户

         $

                   .post(

                            "${pageContext.request.contextPath }/linkMan_findByCustomer.action",

                            {

                                 "" : ""

                            }, function(data) {

                                 //遍历JOSN类型的数据

                                 $(data).each(

                                          function(i, n) {

                                               $("select#lkm_cust").append(

                                                        "<option value='"+n.cust_id+"'>"

                                                                 + n.cust_name

                                                                 + "</option>");

                                          });

                            }, "json");



     });

1.2编写action

// 联系人修改异步查询信息的方法

     public String edit() {

         linkMan = linkManService.findById(linkMan.getLkm_id());

         // 把需要传送的数据封装成map集合

         Map map = new TreeMap();

         map.put("lkm_id", linkMan.getLkm_id());

         map.put("lkm_name", linkMan.getLkm_name());

         map.put("lkm_cust", linkMan.getCustomer().getCust_id());

         map.put("lkm_gender", linkMan.getLkm_gender());

         map.put("lkm_phone", linkMan.getLkm_phone());

         map.put("lkm_mobile", linkMan.getLkm_mobile());

         map.put("lkm_email", linkMan.getLkm_email());

         map.put("lkm_qq", linkMan.getLkm_qq());

         map.put("lkm_position", linkMan.getLkm_position());

         map.put("lkm_memo", linkMan.getLkm_memo());



         // 转成json

         JSONObject jsonObject = JSONObject.fromObject(map);



         // System.out.println(jsonObject);

         // 将JSON数据传到页面

         try {

              ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");

              ServletActionContext.getResponse().getWriter().println(jsonObject.toString());

         } catch (IOException e) {

              e.printStackTrace();

         }

         return NONE;

     }



     // 联系人修改的方法

     public String update() {

         // 调用业务层

         linkManService.update(linkMan);

         return "updateSuccess";

     }



     // 联系人修改异步加载客户信息

     public String findByCustomer() {

         // 调用业务层查询方法

         List<Customer> cust_list = customerService.findAll();

         List<Customer> list=new ArrayList<Customer>();

         for (Customer customer : cust_list) {

              Customer c =new Customer();

              c.setCust_id(customer.getCust_id());

              c.setCust_name(customer.getCust_name());

              list.add(c);

         }

        

         JSONArray jsonArray = JSONArray.fromObject(list);

         // System.out.println(jsonArray.toString());

         // 将JSON数据传到页面

         try {

              ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");

              ServletActionContext.getResponse().getWriter().println(jsonArray.toString());

         } catch (IOException e) {

              e.printStackTrace();

         }



         return NONE;

     }

配置action:

<!-- 联系人管理Action -->

         <action name="linkMan_*" class="linkManAction" method="{1}">

              <result name="findAll">/linkman/list.jsp</result>

              <result name="saveUI">/linkman/add.jsp</result>

              <result name="saveSuccess">/linkman/list.jsp</result>

              <result name="updateSuccess" type="redirect">/linkMan_findAll.action</result>

         </action>

1.3编写service

//根据id查询联系人

     @Override

     public LinkMan findById(Long lkm_id) {

         return linkManDao.findById(lkm_id);

     }

  

    //修改联系人

     @Override

     public void update(LinkMan linkMan) {

         linkManDao.update(linkMan);

    }

1.4编写Dao

//根据ID查询联系人

     @Override

     public LinkMan findById(Long lkm_id) {

         return this.getHibernateTemplate().get(LinkMan.class, lkm_id);

     }

    

     //修改联系人

     @Override

     public void update(LinkMan linkMan) {

         this.getHibernateTemplate().update(linkMan);

     }

 

2.CRM联系人管理-删除联系人

2.1编写jsp页面

Html:

<td><input type="button" name="button"

                                               class="btn btn82 btn_add" id="modifyBtn"

                                               onclick="toUpdate('<s:property value="lkm_id"/>')" value="修改">

                                               <input type="button" name="button" class="btn btn82 btn_del"

                                               onclick="toDelete('<s:property value="lkm_id"/>')" value="删除"></td>

JS:

//删除联系人弹窗

     function toDelete(id) {



         var btnFn = function() {

              location.href = "${pageContext.request.contextPath}/linkMan_delete.action?lkm_id="

                       + id;

              return false;

         };



         easyDialog.open({

              container : {

                   header : '确认删除',

                   content : '您确定要删除该条数据吗?',

                   yesFn : btnFn,

                   noFn : true

              }

         });

     };

2.2编写action

// 删除联系人

     public String delete() {

         // 调用业务层查找联系人

         linkMan = linkManService.findById(linkMan.getLkm_id());

         // 调用业务层删除联系人

         linkManService.delete(linkMan);

         // 跳转到查询页面

         findAll();

         return "deleteSuccess";

     }

配置action:

<!-- 联系人管理Action -->

         <action name="linkMan_*" class="linkManAction" method="{1}">

              <result name="findAll">/linkman/list.jsp</result>

              <result name="saveUI">/linkman/add.jsp</result>

              <result name="saveSuccess">/linkman/list.jsp</result>

              <result name="deleteSuccess">/linkman/list.jsp</result>

              <result name="updateSuccess" type="redirect">/linkMan_findAll.action</result>

         </action>

2.3编写service

//删除联系人

     @Override

     public void delete(LinkMan linkMan) {

         linkManDao.delete(linkMan);

     }

 

2.4编写Dao

//删除联系人

     @Override

     public void delete(LinkMan linkMan) {

         this.getHibernateTemplate().delete(linkMan);

     }

3.CRM联系人管理-条件查询

3.1页面修改:

<div id="search_bar" class="mt10">

                       <div class="box">

                            <div class="box_border">

                                 <div class="box_top">

                                     <b class="pl15">搜索</b>

                                 </div>

                                 <div class="box_center pt10 pb10">

                                     <table class="form_table" cellspacing="0" cellpadding="0"

                                          border="0">

                                          <tbody>

                                               <tr>

                                                   <td>联系人名称:</td>

                                                   <td><input type="text" name="lkm_name"

                                                        value="<s:property value="lkm_name" />"

                                                        class="input-text lh25" size="10"></td>

                                                   <td>联系人性别:</td>

                                                   <td class=""><input type="radio" name="lkm_gender"

                                                        value="1" <s:if test="lkm_gender==1">checked="checked"</s:if>> 男 <input type="radio" name="lkm_gender"

                                                        value="2" <s:if test="lkm_gender==2">checked="checked"</s:if> >女</td>









                                                   <td><input type="submit" name="button"

                                                        class="btn btn82 btn_search" value="查询"></td>

                                               </tr>



                                          </tbody>

                                     </table>

                                 </div>



                            </div>

                       </div>

                   </div>

 

3.2Action中findAll方法修改

// 查询所有联系人的方法

     public String findAll() {

         DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LinkMan.class);

         // 设置条件

        if(linkMan.getLkm_name()!=null){

        //设置按名称查找的条件

        detachedCriteria.add(Restrictions.like("lkm_name", "%"+linkMan.getLkm_name()+"%"));

        }

        //设置性别条件

        if(linkMan.getLkm_gender()!=null&&!linkMan.getLkm_gender().trim().equals("")){

        //设置按名称查找的条件

        detachedCriteria.add(Restrictions.like("lkm_gender", "%"+linkMan.getLkm_gender()+"%"));

        }

         // 调用业务层查询

         PageBean<LinkMan> pageBean = linkManService.findByPage(detachedCriteria, page, pageSize);

         ActionContext.getContext().getValueStack().push(pageBean);



         return "findAll";

     }

4.CRM联系人管理-级联问题解决

  1. 级联删除,在客户删除的时候,先查询再删除
  2. 在客户的映射进行配置

  1. 因为在修改客户的时候,没有查询联系人的集合,当点击修改,修改客户(修改客户所关联联系人)因为联系人的集合是空,所以将外键置为null。

5.CRM联系人管理-抽取通用Dao

5.1定义通用Dao接口

 

public interface BaseDao<T> {

   public void save(T t);

   public void update(T t);

   public void delete(T t);

   public T findById(Serializable id);

   //查询所有

   public List<T> findAll();

   //统计个数

   public Integer findCount(DetachedCriteria detachedCriteria);

   //分页查询

   public List<T> findByPage(DetachedCriteria detachedCriteria,Integer begin,Integer pageSize);

}

5.2通过泛型的反射抽取通用的DAO

  1. 回顾一下泛型:
    1. 泛型         :通用的类型。
    2. <>              :念为  typeof
    3. List<E>     :E称为类型参数变量
    4. ArrayList<Integer>  :Integer称为是实际类型参数
    5. ArrayList<Integer>  ArrayList<Integer>称为参数化类型
  2. 在父类的构造方法中获得子类继承父类上的参数化类型中的实际类型参数
  3. 泛型反射的步骤:

第一步:获得代表子类对象的Class

第二步:查看API

 

 

Type[] getGenericInterfaces();         :获得带有泛型的接口,可以实现多个接口。

Type getGenericSuperclass();           :获得带有泛型的父类,继承一个类。

第三步:获得带有泛型的父类

第四步:将带有泛型的父类的类型转成具体参数化的类型

第五步:通过参数化类型的方法获得实际类型参数

代码实现:

public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

     private Class clazz;



     /*

      * 提供无参的构造方法,获得具体类型Class 具体类型的Class是参数类型中的实际类型参数

      */

     public BaseDaoImpl() {

         // 反射:第一步获得Class

         Class clazz = this.getClass();

         // 得到参数化类型:例如BaseDaoImpl<Customer>,BaseDaoImpl<LinkMan>

         Type type = clazz.getGenericSuperclass();

         // 得到的type就是一个参数化的类型,将type强转成参数化类型

         ParameterizedType ptype = (ParameterizedType) type;

         // 通过参数化类型获得实际类型参数:得到的是一个数组

         Type[] types = ptype.getActualTypeArguments();

         // 获得第一个实际类型参数

         this.clazz = (Class) types[0];

     }



     @Override

     public void save(T t) {

         this.getHibernateTemplate().save(t);

     }



     @Override

     public void update(T t) {

         this.getHibernateTemplate().update(t);

     }



     @Override

     public void delete(T t) {

         this.getHibernateTemplate().delete(t);

     }

    //通过ID查询

     @Override

     public T findById(Serializable id) {

         return (T) this.getHibernateTemplate().get(clazz, id);

     }

    

     // 查询所有

     @Override

     public List<T> findAll() {

         return (List<T>) this.getHibernateTemplate().find("from " + clazz.getSimpleName());

     }



     // 统计个数

     @Override

     public Integer findCount(DetachedCriteria detachedCriteria) {

         detachedCriteria.setProjection(Projections.rowCount());

         List<Long> count = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);

         if (count.size() > 0) {

              return count.get(0).intValue();

         }

         return null;

     }



     // 分页查询

     @Override

     public List<T> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {

         detachedCriteria.setProjection(null);

        return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);

     }



    



}

5.3使用通用的Dao

CustomerDao接口:

public interface CustomerDao extends BaseDao<Customer> {



}

CustomerDao实现类

public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao {

    

}

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__0809 返回首页