MyBatis: 自定义ObjectFactory

1.创建DefaultObjectFactory的子类

package factory;


import java.util.List;
import java.util.Properties;

import org.apache.ibatis.reflection.factory.DefaultObjectFactory;

import tju.MyBtsBasic.domain.Book;

public class AmadeusObjectFactory extends DefaultObjectFactory{
	String author;
	
	static
	{
		System.out.println("in my diy object factory......");
	}
	
	@Override
	public Object create(Class type) {
	    return super.create(type);
	  }

	  public Object create(Class type, List constructorArgTypes, List constructorArgs) {
	    Class classToCreate = resolveInterface(type);
	    @SuppressWarnings("unchecked")
	    // we know types are assignable
	    Object created = super.create(classToCreate, constructorArgTypes, constructorArgs);
	    advice(created);
	    return created;
	  }

	  public void setProperties(Properties properties) {
	    author=properties.getProperty("author");
	  }
	  
	  public Object advice(Object object)
	  {
		  if(Book.class.isAssignableFrom(object.getClass()))
				  {
			        Book b=(Book)object;
			        b.setAuthor(author);
			        
			        
				  }
		  return object;
		  
	  }
	
	

}

2.配置自定义ObjectFactory



  

	
	
	
	
	
	
	
		
		
		
		
		
		
	
	
	
	
	
	
	
	
    
             
             
                 
                 
                 
                
                 
                 
                 
             
         
  
  
  
  	
  	
  
  

你可能感兴趣的:(MyBatis)