码迷,mamicode.com
首页 > 其他好文 > 详细

005杰信-factory删除数据

时间:2016-09-11 00:10:14      阅读:604      评论:0      收藏:0      [点我收藏+]

标签:

factory表的删除分为两种:单行删除,以及批量删除。

过程:在jFactoryCreate.jsp页面上两个按钮,单行删除以及批量删除。

技术分享

选中好序号后,进入action请求,然后进入Controller控制器(FactoryController.java),进入到FactoryController.java里面的public String deleteById(String id)方法或者public String delete(@RequestParam("id")String[] ids)方法。然后进入里面的Service层,然后进入到Dao层,然后调用Mybatis的FactoyMapper.xml里面的方法。

 

具体做法:

项目结构:

技术分享

 

jFactoryList.jsp代码:

<%@ page language="java" pageEncoding="UTF-8"%>
<hr>
    <font color="blue"><b>引入CSS、JS区域</b></font>
<hr>

<%@ include file="../../baselist.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>

<body>
<form name="icform" method="post">

<div id="menubar">
<div id="middleMenubar">
<div id="innerMenubar">
  <div id="navMenubar">
<ul>

<hr>
    <font color="blue"><b>按钮区域</b></font>
<hr>

<li id="view"><a href="#" onclick="formSubmit(‘toview.action‘,‘_self‘);this.blur();">查看</a></li>
 <li id="new"><a href="#" onclick="formSubmit(‘factorycreate.action‘,‘_self‘);this.blur();">新增</a></li>
  <li id="update"><a href="#" onclick="formSubmit(‘jfactoryupdate.action‘,‘_self‘);this.blur();">修改</a></li>
   <li id="delete"><a href="#" onclick="formSubmit(‘deleteById.action‘,‘_self‘);this.blur();">删除</a></li>
    <li id="delete"><a href="#" onclick="formSubmit(‘delete.action‘,‘_self‘);this.blur();">批量删除</a></li>
</ul>
  </div>
</div>
</div>
</div>
   
<!-- 页面主体部分(列表等) -->  
<div class="textbox" id="centerTextbox">
  <div class="textbox-header">
  <div class="textbox-inner-header">
  <div class="textbox-title">

<hr>
    <font color="blue"><b>标题</b></font>
<hr>

    生产厂家列表
  </div> 
  </div>
  </div>
  
<div>
    
<hr>
    <font color="blue"><b>列表区域</b></font>
<hr>

<div class="eXtremeTable" >
<table id="ec_table" class="tableRegion" width="98%" >
    <thead>
    <tr>
        <td class="tableHeader"><input type="checkbox" name="selid" onclick="checkAll(‘id‘,this)"></td>
        <td class="tableHeader">序号</td>
        <td class="tableHeader">厂家全称</td>
        <td class="tableHeader">缩写</td>
        <td class="tableHeader">联系人</td>
        <td class="tableHeader">电话</td>
        <td class="tableHeader">手机</td>
        <td class="tableHeader">传真</td>
        <td class="tableHeader">验货员</td>
        
        
    </tr>
    </thead>
    <tbody class="tableBody" >
    <!--var="o"的意思是给datalist取个别名,之后用起来就简单了-->
    <c:forEach items="${datalist}" var="o" varStatus="status">
    <tr class="odd" onmouseover="this.className=‘highlight‘" onmouseout="this.className=‘odd‘" >
    <!-- 做了一个选择框 -->
        <td><input type="checkbox" name="id" value="${o.id}"/></td>
        <td>${status.index+1}</td>
        <td><a href="toview.action?id=${o.id}">${o.fullName}</a></td>
        <td>${o.factoryName}</td>
        <td>${o.contacts}</td>
        <td>${o.phone }</td>
         <td>${o.mobile }</td>
         <td>${o.fax }</td>
         <td>${o.inspector }</td>
      
    </tr>
    </c:forEach>
    
    </tbody>
</table>
</div>
 
</div>
 
 
</form>
</body>
</html>

点击删除按钮,执行formSubmit(‘jfactoryupdate.action‘,‘_self‘)请求。来到FactoryContrall.java。

FactoryContrall.java代码如下:

package cn.itcast.jk.controller.basicinfo.factory;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.jk.controller.BaseController;
import cn.itcast.jk.domain.Factory;
import cn.itcast.jk.service.FactoryService;
@Controller
public class FactoryController extends BaseController {
  @Resource
    FactoryService factoryService;
  
    //列表
    @RequestMapping("/basicinfo/factory/list.action")
    public String list(Model model)
    {
        
        List<Factory> datalist=factoryService.find(null);
        model.addAttribute("datalist", datalist);//将我的数据传递到页面;
        
        return "/basicinfo/factory/jFactoryList.jsp";
        
    }

    
    //删除一个
    @RequestMapping("/basicinfo/factory/deleteById.action")
    public String deleteById(String id)
    {
        
        factoryService.deleteById(id);
         return "redirect:/basicinfo/factory/list.action";
        
    }
    
    //删除多个
    @RequestMapping("/basicinfo/factory/delete.action")
    //@RequestParam("id")String[] ids 表示在页面上叫做id,但是我封装的时候封装成ids
    //因为声明的是数组所以会自动的拆分 这里不同的话参考Springmvc专辑的文章23各种参数绑定方式
    public String delete(@RequestParam("id")String[] ids)
    {
        
        factoryService.delete(ids);
         return "redirect:/basicinfo/factory/list.action";
        
    }
     
    
}

然后执行里面的factoryService的方法。

FactoryServiceImpl的代码如下:

package cn.itcast.jk.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.itcast.jk.dao.FactoryDao;
import cn.itcast.jk.domain.Factory;
import cn.itcast.jk.pagination.Page;
import cn.itcast.jk.service.FactoryService;


@Service
public class FactoryServiceImpl implements FactoryService{

    @Resource
    FactoryDao factoryDao;
    

    
    @Override
    public List<Factory> findPage(Page page) {
        return null;
    }

    
    @Override
    public List<Factory> find(Map paraMap) {
        return factoryDao.find(paraMap);
        
    }


    @Override
    public Factory get(Serializable id) {
        return factoryDao.get(id);
    }

    
    @Override
    public void insert(Factory factory) {
        factory.setId(UUID.randomUUID().toString());
        factoryDao.insert(factory);
        
    }

    
    @Override
    public void update(Factory factory) {
        factoryDao.update(factory);
        
    }

    //删除一条记录
    @Override
    public void deleteById(Serializable id) {
        factoryDao.deleteById(id);
    }

    //删除多条记录
    @Override
    public void delete(Serializable[] ids) {
        factoryDao.delete(ids);
    }

}

 然后执行factoryDao里面的方法。

而通过spring,其实factoryDao就是FactoryDaoImpl的实例。

我们看一下FactoryDaoImpl的代码:

package cn.itcast.jk.dao.impl;

import org.springframework.stereotype.Repository;

import cn.itcast.jk.dao.FactoryDao;
import cn.itcast.jk.domain.Factory;


/*
 * @Repository这注册了才能在包扫面的时候把这个实例注册进bean容器。
 * 
 */
@Repository
public class FactoryDaoImpl extends BaseDaoImpl<Factory> implements FactoryDao {
/**
 * 
 */
public FactoryDaoImpl() {
//设置命名空间
super.setNs("cn.itcast.jk.mapper");
}
}

 

我们再来看BaseDaoImpl的方法:

 

package cn.itcast.jk.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.jk.dao.BaseDao;
import cn.itcast.jk.pagination.Page;

public abstract class BaseDaoImpl<T> extends SqlSessionDaoSupport implements BaseDao<T>{
    @Autowired
    //mybatis-spring 1.0无需此方法;mybatis-spring1.2必须注入。
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }          
     private String ns;        //命名空间
    public String getNs() {
        return ns;
    }
    public void setNs(String ns) {
        this.ns = ns;
    }
    


    public void deleteById(Serializable id) {
        this.getSqlSession().delete(ns + ".deleteById", id);
    }

    public void delete(Serializable[] ids) {
        this.getSqlSession().delete(ns + ".delete", ids);
    }
}

 

最后来看Mybatis的配置文件FactoryMapper.xml的代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper  namespace="cn.itcast.jk.mapper">
<!-- factory_c表的映射 -->
 <resultMap type="cn.itcast.jk.domain.Factory" id="factoryRM" >
    <id property="id" column="FACTORY_ID"/>
    <result property="fullName" column="FULL_NAME"/>
      <result property="factoryName" column="FACTORY_NAME"/>
      <result property="contacts" column="CONTACTS"/>
      <result property="phone" column="PHONE"/>
      <result property="mobile" column="MOBILE"/>
      <result property="fax" column="FAX"/>
      <result property="cnote" column="CNOTE"/>
      <result property="inspector" column="INSPECTOR"/>
      <result property="orderNo" column="ORDER_NO"/>
      <result property="createBy" column="CREATE_BY"/>
      <result property="createDept" column="CREATE_DEPT"/>
      <result property="createTime" column="CREATE_TIME"/>
      
 </resultMap>
 <select id="find" parameterType="map"  resultMap="factoryRM"  >
 
 
 select * from factory_c where 1=1
 </select>


<!-- 
当数据库是Oracle时,如果插入的数据是空时,必须是要指定默认的参数。所以这里的设置要这么写:jdbcType=VARCHAR。
-->
<insert id="insert" parameterType="cn.itcast.jk.domain.Factory">

insert into factory_c(FACTORY_ID,FULL_NAME,FACTORY_NAME,CONTACTS,PHONE,MOBILE,FAX,CNOTE,INSPECTOR,ORDER_NO,CREATE_BY,CREATE_DEPT,CREATE_TIME) values(
#{id,jdbcType=VARCHAR},
#{fullName,jdbcType=VARCHAR},
#{factoryName,jdbcType=VARCHAR},
#{contacts,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{mobile,jdbcType=VARCHAR},
#{fax,jdbcType=VARCHAR},
#{cnote,jdbcType=VARCHAR},
#{inspector,jdbcType=VARCHAR},
#{orderNo,jdbcType=INTEGER},
#{createBy,jdbcType=VARCHAR},
#{createDept,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}
)
</insert>
<!-- 修改 动态的sql语句-->
<update id="update" parameterType="cn.itcast.jk.domain.Factory">
update factory_c
<set>
<!-- 如果 fullName不为空,就修改。-->
<if test="fullName!=null">FULL_NAME=#{fullName,jdbcType=VARCHAR},</if>
<if test="factoryName!=null">FACTORY_NAME=#{factoryName,jdbcType=VARCHAR},</if>
<if test="contacts!=null">CONTACTS=#{contacts,jdbcType=VARCHAR},</if>
<if test="phone!=null">PHONE=#{phone,jdbcType=VARCHAR},</if>
<if test="mobile!=null">MOBILE=#{mobile,jdbcType=VARCHAR},</if>
<if test="fax!=null">FAX=#{fax,jdbcType=VARCHAR},</if>
<if test="cnote!=null">CNOTE=#{cnote,jdbcType=VARCHAR},</if>
<if test="inspector!=null">INSPECTOR=#{inspector,jdbcType=VARCHAR},</if>
<if test="orderNo!=null">ORDER_NO=#{orderNo,jdbcType=INTEGER},</if>
<if test="createBy!=null">CREATE_BY=#{createBy,jdbcType=VARCHAR},</if>
<if test="createDept!=null">CREATE_DEPT=#{createDept,jdbcType=VARCHAR},</if>
<if test="createTime!=null">CREATE_TIME=#{createTime,jdbcType=TIMESTAMP},</if>
</set>
where FACTORY_ID=#{id}
</update>


<!-- 查询一个数据 -->
<select id="get" parameterType="string" resultMap="factoryRM">
<!-- 
注意我这里写了一个factory_id=#{pid},我这么写主要是为了说明这里要传入的值并不是
factory这个类里面的属性,可以是随意的,只要是String就好。
 -->
select * from  factory_c where factory_id=#{pid}
</select>

<!-- 删除一条的 -->

<delete id="deleteById" parameterType="string">
delete  from factory_c where FACTORY_ID=#{pid}

</delete>

<!-- 删除多条的记录 -->
<delete id="delete"  parameterType="string">
delete from factory_c 
where FACTORY_ID in 
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
</mapper>

 

到这里就结束了。

执行OK.

005杰信-factory删除数据

标签:

原文地址:http://www.cnblogs.com/shenxiaoquan/p/5860625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!