zoukankan      html  css  js  c++  java
  • 增删改查

    /**
    	 * 在修改Bean操作之前进行的逻辑验证(本方法子类一般需要重写)
    	 * @param bean 实体Bean
    	 * @return 验证成功返回StatusType.DEFAULT,如果失败则返回自定义的状态码(小于0)
    	 * @throws Exception 1.逻辑验证发生异常
    	 */
    	protected int validateEdit(Object bean) throws Exception{
    		return StatusType.DEFAULT ;
    	}
    	
    	/**
    	 * 在添加Bean操作之前进行的逻辑验证(本方法子类一般需要重写)
    	 * @param bean 实体Bean
    	 * @return 验证成功返回StatusType.DEFAULT,如果失败则返回自定义的状态码(小于0)
    	 * @throws Exception 1.逻辑验证发生异常
    	 */
    	protected int validateAdd(Object bean ) throws Exception{
    		return StatusType.DEFAULT;
    	}
    	
    	/**
    	 * 在删除(逻辑删除或物理删除)Bean操作之前进行的逻辑验证
    	 * @param bean 实体Bean
    	 * @return 验证成功返回StatusType.DEFAULT,如果失败则返回自定义的状态码(小于0)
    	 * @throws Exception 1.逻辑验证发生异常
    	 */
    	protected int validateDelete(Object bean) throws Exception{
    		return StatusType.DEFAULT ;
    	}
    	
    	public int check(Object bean , Map<String,Object> modelMap) throws Exception{
    		int result = 0 ;
    		ValidatePersistence persistence = this.validater(bean);
    		result = persistence.check( this.daoProvider() );
    		
    		if( result < 0 ){
    			String text = persistence.getText();
    			modelMap.put(ContextArgument.PROMPT_ERROR, text);
    		}
    		
    		return result ;
    	}
    	
    	public int add(Object bean) throws Exception {        
            int result = StatusType.DEFAULT ;   
            result = this.validateAdd(bean);
            
            if(result == StatusType.DEFAULT ){
                result = this.daoProvider().insert(bean);
            }
            
            return result ;
        }
    
        public int modify(Object bean) throws Exception {
        	 int result = StatusType.DEFAULT ;        
        	 result = this.validateEdit(bean);
        	 
             if(result == StatusType.DEFAULT ){
                 result = this.daoProvider().modify(bean);
             }
             
             return result ;
        }
        
        public int delete(Object bean) throws Exception {
        	int result = StatusType.DEFAULT ;      
        	result = this.validateDelete(bean);
        	 
            if(result == StatusType.DEFAULT ){
                result = this.daoProvider().delete(bean);
            }
             
            return result ;
        }
    
        public int batchDelete(List<?> beans) throws Exception {
            int result = StatusType.DEFAULT ;
            
            if( this.daoProvider().batchDelete(beans) ){
                result = StatusType.SUCCESS ;
            }      
            return result;
        }
        
        public int batchDelete(Object bean , String ids ) throws Exception{
    		int result = StatusType.DEFAULT ;
    		List<?> beans = Persistence.beans(bean.getClass(), ids);
    		
    		if( !NoneType.isNullOrEmpty(beans) ){
    			result = this.batchDelete(beans);
    		}
    		
    		return result ;
    	}
        
    	public <T> T bean(Object bean) throws Exception { 
    		T result = this.daoProvider().fectchObject(bean);
            return result ;
        } 
    
    	public <T> T bean(Object bean,List<Class<?>> parentBeanTypes) throws Exception {    
        	T result = null ;
        	List<T> beans = this.daoProvider().relatedQueryObject(bean, parentBeanTypes);
    
    		if (!NoneType.isNullOrEmpty(beans)) {
    			result = beans.get(0);    
            }
            
            return result ;
        }
        
        public <T> List<T> beans(Object bean)throws Exception {
    		List<T> result = this.daoProvider().queryObject(bean) ;
    		return result ;
    	}
        
        public <T> List<T> beans(Object bean, String orderBy, DataPage dataPage)throws Exception {
            List<T> result = new ArrayList<T>() ;     
            int count = this.daoProvider().sum(bean);
            
            if( count == 0 ){
            	 return result;
            }
            
            if( dataPage != null ){
            	dataPage.setCount(count);
            }
            
            SqlCriteria criteria = new SqlCriteria();
            criteria.setOrderBy(orderBy);   
            criteria.setPager(dataPage);
            result = this.daoProvider().queryObject(bean, criteria);    
            return result;
        }
        
        public <T> List<T> beans(Class<?> sqlClass, Object bean, String orderBy, DataPage dataPage)throws Exception {
            List<T> result = null ;     
            Field sqlField = sqlClass.getField("beans");
        	String sql = "";
        	
        	if( sqlField != null ){
        		sql = sqlField.get(null).toString();
        		SqlCommand command = new SqlCommand(sql);
            	command.getCriteria().setOrderBy(orderBy);
            	command.getCriteria().setPager(dataPage);
            	result = this.daoProvider().queryObject(command, bean);
        	}
        	
            return result;
        }
        
        public DataTable table(Object bean)throws Exception {
        	DataTable result = this.daoProvider().query(bean) ;
    		return result ;
    	}
    
        public DataTable table(Object bean, String orderBy, DataPage dataPage) throws Exception{
    		DataTable result = null;
    		int count = this.daoProvider().sum(bean);
    
    		if (count == 0) {
    			result = new DataTable();
    			return result;
    		}
    
    		if (dataPage != null) {
    			dataPage.setCount(count);
    		}
    		
    		SqlCriteria criteria = new SqlCriteria();
    		criteria.setOrderBy(orderBy);
    	    criteria.setPager(dataPage);
    		result = this.daoProvider().query(bean, criteria);
    		return result;
    	}
        
        public DataTable table(Class<?> sqlClass, Object bean, String orderBy, DataPage dataPage)throws Exception {
        	DataTable result = null ;
        	Field sqlField = sqlClass.getField("table");
        	String sql = "";
        	
        	if( sqlField != null ){
        		sql = sqlField.get(null).toString();
        		SqlCommand command = new SqlCommand(sql);
            	command.getCriteria().setOrderBy(orderBy);
            	command.getCriteria().setPager(dataPage);
            	result = this.daoProvider().query(command, bean);	
        	}
        	
        	return result ;
        }
    

      

    -------------------------------------------------------------------------
    ## 极客时间全网最便宜最优惠购买方式,优惠券返现 百度网盘 微信关注公众号“选门好课”
    扫描下方二维码关注我的公众号"选门好课",与我一起交流知识
  • 相关阅读:
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
    UVA10071 Back to High School Physics
    UVA10071 Back to High School Physics
    UVA10055 Hashmat the Brave Warrior
    UVA10055 Hashmat the Brave Warrior
    UVA458 The Decoder
    UVA458 The Decoder
    HDU2054 A == B ?
    HDU2054 A == B ?
    POJ3414 Pots
  • 原文地址:https://www.cnblogs.com/singworld/p/9803851.html
Copyright © 2011-2022 走看看