欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis学习(一):XML配置-objectFactory

程序员文章站 2022-07-15 10:24:16
...

对象工厂(objectFactory)

MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认构造方法,要么在参数映射存在的时候通过参数构造方法来实例化。 如果想覆盖对象工厂的默认行为,则可以通过创建自己的对象工厂来实现 。

  1. 继承org.apache.ibatis.reflection.factory.DefaultObjectFactory
package com.zm.config;

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

import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

/**
 * @Auther: zhangmo
 * @Email : [email protected]
 * @Date: 2019/12/16 20:15
 * @Description:
 */
public class ExampleObjectFactory extends DefaultObjectFactory {
    @Override
    public Object create(Class type) {
        System.out.println("create(Class type) : " + type);
        return super.create(type);
    }

    @Override
    public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
        System.out.println(type);
        return super.create(type, constructorArgTypes, constructorArgs);
    }

    @Override
    public void setProperties(Properties properties) {
        System.out.println("properties size :" + properties.size());
        Enumeration<String> enumeration = (Enumeration<String>) properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            String value = properties.getProperty(key);
            System.out.println(key + " :" + value);
        }
        super.setProperties(properties);
    }

    public <T> boolean isCollection(Class<T> type) {
        System.out.println(type);
        return Collection.class.isAssignableFrom(type);
    }
}

  • Object create(Class type)

  • T create(Class type, List<Class<?>> constructorArgTypes, List constructorArgs)

  • void setProperties

  • boolean isCollection(Class type)

    create方法主要用于创建返回类型对象

    setproperties方法主要是对配置中标签下的值进行赋值操作

    isCollection方法判断对象是不是集合类型的对象。

    1. mybatis-config.xml

      将ExampleObjectFactory类设置进来,还可以设置值

      <objectFactory type="com.zm.config.ExampleObjectFactory">
              <property name="a" value="500"/>
              <property name="b" value="1000"/>
       </objectFactory>
      
    2. 测试类

    @Test
    public void test5() {
        SqlSession session = sqlSessionFactory.openSession();
        DateBeanMapper DateBeanMapper = session.getMapper(DateBeanMapper.class);
    
         DateBean dateBean = DateBeanMapper.selectByID(9L);
         System.out.println(dateBean);
    }
    
    1. 运行结果

      properties size :2
      b :1000
      a :500
      DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
      DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
      DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
      DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
      class com.zm.entity.DateBean
      DEBUG [main] - Opening JDBC Connection
      DEBUG [main] - Created connection 961409111.
      DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]]
      DEBUG [main] - ==>  Preparing: select id,time1,time2,time3 from datebean where id = ? 
      DEBUG [main] - ==> Parameters: 9(Long)
      create(Class type) : interface java.util.List
      interface java.util.List
      create(Class type) : class com.zm.entity.DateBean
      class com.zm.entity.DateBean
      DEBUG [main] - <==      Total: 1
      DateBean{id=9, time1=Fri Dec 13 08:00:00 CST 2019, time2='2019-12-13', time3=Fri Dec 13 22:37:49 CST 2019}
      
      

      分析结果:

      1.运行test5()方法。

      2.直接进入到ExampleObjectFactory类中的setProperties方法。

      3.然后再执行test5()中第一行代码,执行到DateBeanMapper.selectByID时,进入isCollection方法,判断返回值类型是否为集合类型。

      4.多次调用create方法创建返回值对象。

相关标签: mybatis