mybatis3.4.1源代码 - MetaObject、MetaClass
作者:互联网
MetaObject:用于访问对象信息,如:读取对象属性、设置对象属性
MetaClass:用于访问类信息,如:获取gettter方法、获取setter方法
package cn.java.demo.ibatis.internal;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetWrapper;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.invoker.Invoker;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
*/
@Slf4j
public class ReflectorFactoryTest {
public static void main(String[] args) throws SQLException {
MetaObjectDemo metaObjectDemo = new MetaObjectDemo();
// MetaObject
{
metaObjectDemo.getRowValue(null, null);
}
// MetaClass
{
OrderEntity orderEntity = new OrderEntity();
MetaObject metaObject = metaObjectDemo.newMetaObject(orderEntity);
MetaClassDemo metaClassDemo = new MetaClassDemo(metaObject, orderEntity);
metaClassDemo.doSetInvoker();
Object result = metaClassDemo.doGetInvoker();
log.info("result = {}", result);
log.info("orderEntity = {}", orderEntity);
}
}
/**
* @see org.apache.ibatis.reflection.wrapper.BeanWrapper#BeanWrapper(org.apache.ibatis.reflection.MetaObject, java.lang.Object)
*/
static class MetaClassDemo {
MetaClass metaClass;
private Object object;
protected static final Object[] NO_ARGUMENTS = new Object[0];
public MetaClassDemo(MetaObject metaObject, Object object) {
this.object = object;
this.metaClass = MetaClass.forClass(object.getClass(), metaObject.getReflectorFactory());
}
/**
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
* @see org.apache.ibatis.reflection.wrapper.BeanWrapper# setBeanProperty(org.apache.ibatis.reflection.property.PropertyTokenizer, java.lang.Object, java.lang.Object)
*/
private Object doSetInvoker() {
try {
Invoker method = metaClass.getSetInvoker("orderNo");
Object[] params = {"orderNo_00"};
method.invoke(object, params);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return null;
}
/**
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
* @see org.apache.ibatis.reflection.wrapper.BeanWrapper# getBeanProperty(org.apache.ibatis.reflection.property.PropertyTokenizer, java.lang.Object)
*/
private Object doGetInvoker() {
try {
Invoker method = metaClass.getGetInvoker("orderNo");
return method.invoke(object, NO_ARGUMENTS);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
static class MetaObjectDemo {
/**
* 反射器工厂
*/
private ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
/**
* 对象工厂
* objectFactory.create(resultType);
*/
private ObjectFactory objectFactory = new DefaultObjectFactory();
/**
* 对象包装工厂
* objectWrapperFactory.getWrapperFor(this, object);
* <p>
* org.apache.ibatis.reflection.wrapper.MapWrapper
* org.apache.ibatis.reflection.wrapper.CollectionWrapper
* org.apache.ibatis.reflection.wrapper.BeanWrapper
*/
private ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
/**
* @param rsw
* @param resultMap
* @return
* @throws SQLException
* @see org.apache.ibatis.executor.resultset.DefaultResultSetHandler# getRowValue(org.apache.ibatis.executor.resultset.ResultSetWrapper, org.apache.ibatis.mapping.ResultMap)
*/
public Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
// OrderEntity
{
Object resultObject = createResultObject(OrderEntity.class);
// 元对象
final MetaObject metaObject = newMetaObject(resultObject);
// 设置值
metaObject.setValue("orderNo", "orderNo_000");
// 打印
log.info("resultObject = {}", resultObject);
}
// OrderEntity
if (false) {
Object resultObject = createResultObject(List.class);
if (resultObject instanceof ArrayList) {
log.info("resultObject instanceof ArrayList");
}
final MetaObject metaObject = newMetaObject(resultObject);
log.info("resultObject = {}", resultObject);
}
return null;
}
/**
* @param object
* @return
* @see org.apache.ibatis.session.Configuration#newMetaObject(java.lang.Object)
*/
public MetaObject newMetaObject(Object object) {
return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
}
/**
* @param resultType
* @return
* @see org.apache.ibatis.executor.resultset.DefaultResultSetHandler# createResultObject(org.apache.ibatis.executor.resultset.ResultSetWrapper, org.apache.ibatis.mapping.ResultMap, java.util.List, java.util.List, java.lang.String)
*/
private Object createResultObject(final Class<?> resultType) {
MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory);
if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
return objectFactory.create(resultType);
}
return null;
}
}
@Data
static class OrderEntity {
private Long id;
private String orderNo;
private Long userId;
private BigDecimal payAmount;
}
}
标签:reflection,Object,ibatis,mybatis3.4,import,apache,org,源代码,MetaClass 来源: https://blog.csdn.net/zhouzhiande/article/details/113639699