2018-03-04 web用户项目:批量删除 ----- 事务处理

笔记如下

1.单个删除


4.png
  • web层(DeleteOneCustomersServlet)
//获得id
        String id = request.getParameter("id");
        
        //调用业务层去删除,将id带过去
        CustomerService cs = new CustomerService();
        
        cs.deleteOneCustomerById(id);
        //删除成功后再次查询 ---- 重定向到查询页面
        
        response.sendRedirect(request.getContextPath() + "/findall");
  • 业务层(CustomerService)

    //根据客户的id好去删除信息
    public void deleteOneCustomerById(String id) {
        // TODO Auto-generated method stub
        
        
        cdao.deleteOneById(id);
        
        
    }
  • dao层(CustomerDaoImpl)
//单个删除
    /* (non-Javadoc)
     * @see com.chen.customers.dao.CustomerDao#deleteOneById(java.lang.String)
     */
    @Override
    public void deleteOneById(String id) {
        // TODO Auto-generated method stub
        
        QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            runner.update("delete from customers where id=?", new Object[] {id});
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }

2.全删除

  • jsp







                
设置状态 客户姓名 客户性别 客户生日 客户邮箱 客户手机 客户爱好 客户类型 客户描述 操作
${customer.name} ${customer.gender} ${customer.birthday} ${customer.email} ${customer.cellphone} ${customer.preference} ${customer.type} ${customer.description} 删除 更新
2018-03-04 web用户项目:批量删除 ----- 事务处理_第1张图片
4.png
  • web层(DeteleBatchServlet)
//获得批量id
        String[] ids = request.getParameterValues("ids");
        
        ThreadLocal t;
        
        //传递ids给业务层,去删除
        CustomerService cs  = new CustomerService();
        cs.deleteBatch(ids);
        
        //重定向
        response.sendRedirect(request.getContextPath() + "/findall");
  • 业务层(CustomerService)
//批量删除:批量删除,要么全部失败,要么全部成功-----事务
    //解决数据耦合 ----- ThreadLocal类
    public void deleteBatch(String[] ids) {
        // TODO Auto-generated method stub
        
        
        try {
            
            //开启事务 ----- set(conn)(ThreadLocal类)
            TransactionUtil.startTransaction();
            
            for (String id : ids) {
                
                //不能用cdao.deleteOneById(id)
                cdao.deleteOneByIdInTransaction(id);
            }
//          //提交事务
//          conn.commit();
        
            TransactionUtil.commit();
        } catch (Exception e) {
            //有异常就回滚
            TransactionUtil.rollback();
            
        }finally {
            //释放资源
            TransactionUtil.relase();
            
        }
        
        
    }
  • dao层(CustomerDaoImpl)
//在事务中的单个删除 
    @Override
    public void deleteOneByIdInTransaction(String id) throws SQLException {
        // TODO Auto-generated method stub
        
        QueryRunner runner = new QueryRunner();
        
        runner.update(TransactionUtil.getConnection(),"delete from customers where id=?", new Object[] {id});
        
        
    }
  • 工具类(TransactionUtil)
//把得到连接及事务有关的方法写到此类中
public class TransactionUtil {
    
    // 内部是维护了 一个 map , 这个map 的key 始终 都是 当前 的线程 
    private static ThreadLocal tl = new ThreadLocal();
    
    private static DataSource ds = JdbcUtils.getDataSource();
    
    public static DataSource getDataSource(){
        return ds;
    }
    
    //  这里, 获得一个 connection  对象 
    public static Connection getConnection(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                //从数据连接池 中 取 一个连接 出来 
                conn = ds.getConnection();
                
                //将 取出来  connection 放到 tl中去
                tl.set(conn);
            }
            return conn;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 开启 事务  
    // 结果 就是 返回了 一个 connection对象, 并且 将 返回的 connection放到了 threadlocal中 , 
    public static void startTransaction(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void rollback(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.rollback();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void commit(){
        try {
            Connection conn = tl.get();
            if(conn==null){
                conn = getConnection();
//              tl.set(conn);
            }
            conn.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void relase(){
        try {
            Connection conn = tl.get();
            if(conn!=null){
                conn.close();
                tl.remove();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

你可能感兴趣的:(2018-03-04 web用户项目:批量删除 ----- 事务处理)