【springmvc+mybatis项目实战】杰信商贸-8.生产厂家修改

来源:互联网 发布:centos lnmp 编辑:程序博客网 时间:2024/03/29 05:21
回顾一下我们的FactoryMapper.xml:
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.   
  6.   
  7. <mapper namespace="cn.hpu.jk.mapper.FactoryMapper">  
  8.     <!-- resultMap映射 -->  
  9.     <resultMap type="cn.hpu.jk.domain.Factory" id="factoryRM">  
  10.         <!-- 主键 -->  
  11.         <id property="id" column="FACTORY_ID"/>  
  12.         <!-- 一般属性 -->  
  13.         <result property="fullName" column="FULL_NAME"/>  
  14.         <result property="factoryName" column="FACTORY_NAME"/>  
  15.         <result property="contacts" column="CONTACTS"/>  
  16.         <result property="phone" column="PHONE"/>  
  17.         <result property="mobile" column="MOBILE"/>  
  18.         <result property="fax" column="FAX"/>  
  19.         <result property="cnote" column="CNOTE"/>  
  20.         <result property="inspector" column="INSPECTOR"/>  
  21.         <result property="orderNo" column="ORDER_NO"/>  
  22.           
  23.         <result property="createBy" column="CREATE_BY"/>  
  24.         <result property="creatDept" column="CREATE_DEPT"/>  
  25.         <result property="creatTime" column="CREATE_TIME"/>  
  26.       
  27.     </resultMap>  
  28.       
  29.     <!-- 查询 -->  
  30.     <select id="find" parameterType="map" resultMap="factoryRM">  
  31.         select * from factory_c  
  32.         where 1=1  
  33.     </select>  
  34.       
  35.     <!-- 新增 oracle jbdc驱动当这个值为null时,必须告诉它当前字段  
  36.     默认值的类型jdbcType=VARCHAR(MyBatis定义),Mysql不用写-->  
  37.     <insert id="insert" parameterType="cn.hpu.jk.domain.Factory">  
  38.         insert into factory_c  
  39.         (FACTORY_ID,FULL_NAME,FACTORY_NAME,CONTACTS,PHONE,MOBILE,FAX,CNOTE,  
  40.         INSPECTOR,ORDER_NO,CREATE_BY,CREATE_DEPT,CREATE_TIME)  
  41.         values  
  42.         (   #{id},  
  43.             #{fullName,jdbcType=VARCHAR},  
  44.             #{factoryName,jdbcType=VARCHAR},  
  45.             #{contacts,jdbcType=VARCHAR},  
  46.             #{phone,jdbcType=VARCHAR},  
  47.             #{mobile,jdbcType=VARCHAR},  
  48.             #{fax,jdbcType=VARCHAR},  
  49.             #{cnote,jdbcType=VARCHAR},  
  50.             #{inspector,jdbcType=VARCHAR},  
  51.             #{orderNo,jdbcType=INTEGER},  
  52.             #{createBy,jdbcType=VARCHAR},  
  53.             #{creatDept,jdbcType=VARCHAR},  
  54.             #{creatTime,jdbcType=TIMESTAMP}  
  55.         )  
  56.     </insert>  
  57.       
  58. </mapper>  

可以看到,我们的添加模块并没有添加动态SQL语句(判定各种条件等),我们在写insert的时候我们一般都是所有字段新增,所以我们没有用动态SQL语句。但是“修改”并不是所有字段都修改的,所以我们的修改配置语句需要使用动态SQL语句。

我们在FactoryMapper.xml继续来添加“修改”的配置语句:
[html] view plaincopy
  1. <!-- 修改语句 -->  
  2. <update id="update" parameterType="cn.hpu.jk.domain.Factory">  
  3.     update factory_c  
  4.     <set>  
  5.        <if test="fullName != null">FULL_NAME=#{fullName,jdbcType=VARCHAR},</if>  
  6.        <if test="factoryName != null">FACTORY_NAME=#{factoryName,jdbcType=VARCHAR},</if>  
  7.        <if test="contacts != null">CONTACTS=#{contacts,jdbcType=VARCHAR},</if>  
  8.        <if test="phone != null">PHONE=#{phone,jdbcType=VARCHAR},</if>  
  9.        <if test="mobile != null">MOBILE=#{mobile,jdbcType=VARCHAR},</if>  
  10.        <if test="fax != null">FAX=#{fax,jdbcType=VARCHAR},</if>  
  11.        <if test="cnote != null">CNOTE=#{cnote,jdbcType=VARCHAR},</if>  
  12.        <if test="inspector != null">INSPECTOR=#{inspector,jdbcType=VARCHAR},</if>  
  13.        <if test="orderNo != null">ORDER_NO=#{orderNo,jdbcType=INTEGER},</if>  
  14.     </set>  
  15.     where FACTORY_ID=#{id}  
  16. </update>  
  17.   
  18.   
  19. <!-- 查询一个 -->  
  20. <select id="get" parameterType="string" resultMap="factoryRM">  
  21.     select * from factory_c  
  22.     where factory_id=#{id}  
  23. </select>  

我们的Mapper映射文件写完,接下来我们开始写dao层
这里我们依然不用写dao,因为我们继承了BaseDaoImpl,它已经写好了update方法了,回顾一下:
[java] view plaincopy
  1. package cn.hpu.jk.dao.impl;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7.   
  8. import org.apache.ibatis.session.SqlSessionFactory;  
  9. import org.mybatis.spring.support.SqlSessionDaoSupport;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11.   
  12.   
  13. import cn.hpu.jk.dao.BaseDao;  
  14. import cn.hpu.jk.pagination.Page;  
  15.   
  16.   
  17. public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>{  
  18.     @Autowired  
  19.     //mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。  
  20.     public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){  
  21.         super.setSqlSessionFactory(sqlSessionFactory);  
  22.     }  
  23.       
  24.     private String ns;      //命名空间  
  25.     public String getNs() {  
  26.         return ns;  
  27.     }  
  28.     public void setNs(String ns) {  
  29.         this.ns = ns;  
  30.     }  
  31.       
  32.     public List<T> findPage(Page page){  
  33.         List<T> oList = this.getSqlSession().selectList(ns + ".findPage", page);  
  34.         return oList;  
  35.     }  
  36.   
  37.   
  38.     public List<T> find(Map map) {  
  39.         List<T> oList = this.getSqlSession().selectList(ns + ".find", map);  
  40.         return oList;  
  41.     }  
  42.     public T get(Serializable id) {  
  43.         return this.getSqlSession().selectOne(ns + ".get", id);  
  44.     }  
  45.   
  46.   
  47.     public void insert(T entity) {  
  48.         this.getSqlSession().insert(ns + ".insert", entity);  
  49.     }  
  50.   
  51.   
  52.     public void update(T entity) {  
  53.         this.getSqlSession().update(ns + ".update", entity);  
  54.     }  
  55.   
  56.   
  57.     public void deleteById(Serializable id) {  
  58.         this.getSqlSession().delete(ns + ".deleteById", id);  
  59.     }  
  60.   
  61.   
  62.     public void delete(Serializable[] ids) {  
  63.         this.getSqlSession().delete(ns + ".delete", ids);  
  64.     }  
  65. }  

我们下面来写service层:
在FactoryService中我们加入以下方法
[java] view plaincopy
  1. @Override  
  2. public void update(Factory factory) {  
  3.           
  4.     factoryDao.update(factory);  
  5. }  
  6.   
  7.   
  8. @Override  
  9. public Factory get(Serializable id) {  
  10.     return factoryDao.get(id);  
  11. }  

下面来写Controller层,在FactoryController中添加下面的方法:
[java] view plaincopy
  1. //转向修改界面  
  2. @RequestMapping("/basicinfo/factory/toupdate.action")  
  3. public String toupdate(String id,Model model){  
  4.     Factory obj=factoryService.get(id);  
  5.     model.addAttribute("obj", obj);  
  6.     return "/baseinfo/factory/jFactoryUpdate.jsp";  
  7. }  
  8.       
  9.       
  10. //修改保存  
  11. @RequestMapping("/basicinfo/factory/update.action")  
  12. public String update(Factory factory){  
  13.     factoryService.update(factory);  
  14.     return "redirect:/basicinfo/factory/list.action";  
  15. }  

我们的jsp视图里面使用的是常规的标签,如C标签和JSTL标签,原因是如果我们要项目复用,但是用的框架不一样,如果我们之前用的struts2的框架,jsp中使用的是struts2的s标签,这个时候转成springmvc框架就要换标签,不太好,所以这里我们使用通用的标签。
修改页面jFactoryUpdate.jsp:
[html] view plaincopy
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ include file="../../base.jsp"%>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <title>修改厂家信息</title>  
  6. </head>  
  7. <body>  
  8.     <form method="post">  
  9.     <input type="hidden" name="id" value="${obj.id}"/>  
  10.     <div id="menubar">  
  11.         <div id="middleMenubar">  
  12.             <div id="innerMenubar">  
  13.                 <div id="navMenubar">  
  14.                     <ul>  
  15.                     <li id="save"><a href="#" onclick="formSubmit('update.action','_self');">确定</a></li>  
  16.                     <li id="back"><a href="list.action">返回</a></li>  
  17.                     </ul>  
  18.                 </div>  
  19.             </div>  
  20.         </div>  
  21.     </div>  
  22.            
  23.     <div class="textbox" id="centerTextbox">  
  24.           
  25.         <div class="textbox-header">  
  26.         <div class="textbox-inner-header">  
  27.         <div class="textbox-title">  
  28.             修改生产厂家信息  
  29.         </div>   
  30.         </div>  
  31.         </div>  
  32.     <div>  
  33.         <div>  
  34.             <table class="commonTable" cellspacing="1">  
  35.                 <tr>  
  36.                     <td class="columnTitle_mustbe">厂家名称:</td>  
  37.                     <td class="tableContent"><input type="text" name="fullName" value="${obj.fullName }"/></td>  
  38.                     <td class="columnTitle_mustbe">简称:</td>  
  39.                     <td class="tableContent"><input type="text" name="factoryName" value="${obj.factoryName }"/></td>  
  40.                 </tr>  
  41.                   
  42.                 <tr>  
  43.                     <td class="columnTitle_mustbe">联系人:</td>  
  44.                     <td class="tableContent"><input type="text" name="contacts" value="${obj.contacts }"/></td>  
  45.                     <td class="columnTitle_mustbe">电话:</td>  
  46.                     <td class="tableContent"><input type="text" name="phone" value="${obj.phone }"/></td>  
  47.                 </tr>  
  48.                   
  49.                 <tr>  
  50.                     <td class="columnTitle_mustbe">手机:</td>  
  51.                     <td class="tableContent"><input type="text" name="mobile" value="${obj.mobile }"/></td>  
  52.                     <td class="columnTitle_mustbe">传真:</td>  
  53.                     <td class="tableContent"><input type="text" name="fax" value="${obj.fax }"/></td>  
  54.                 </tr>  
  55.                   
  56.                  <tr>  
  57.                     <td class="columnTitle_mustbe">检验员:</td>  
  58.                     <td class="tableContent"><input type="text" name="inspector" value="${obj.inspector }"/></td>  
  59.                     <td class="columnTitle_mustbe">排序号:</td>  
  60.                     <td class="tableContent"><input type="text" name="orderNo" value="${obj.orderNo }"/></td>  
  61.                 </tr>  
  62.                   
  63.                 <tr>  
  64.                     <td class="columnTitle_mustbe">备注:</td>  
  65.                     <td class="tableContent"><textarea  name="cnote" style="height:200px;width: 400px">${obj.cnote }</textarea></td>  
  66.                 </tr>  
  67.             </table>  
  68.         </div>  
  69.     </div>  
  70.        
  71.     </form>  
  72. </body>  
  73. </html>  

我们在jFactoryList.jsp页面加一个“修改”的按钮:
[html] view plaincopy
  1. <li id="update"><a href="#" onclick="formSubmit('toupdate.action','_self');this.blur();">修改</a></li>  

下面我们来测试:
点击修改按钮,如



然后修改某个信息,这里我们修改联系人(把4改为444),如



点击确定,在列表中查看我们修改后的结果,如

发现修改成功!我们的修改功能编写完毕!

0 0
原创粉丝点击