001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.mybatis; 017 018import com.mybatisflex.core.row.Row; 019import com.mybatisflex.core.table.TableInfo; 020import com.mybatisflex.core.table.TableInfoFactory; 021import com.mybatisflex.core.util.StringUtil; 022import org.apache.ibatis.reflection.MetaObject; 023import org.apache.ibatis.reflection.property.PropertyTokenizer; 024import org.apache.ibatis.reflection.wrapper.BeanWrapper; 025import org.apache.ibatis.reflection.wrapper.MapWrapper; 026import org.apache.ibatis.reflection.wrapper.ObjectWrapper; 027import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory; 028 029import java.util.Collection; 030import java.util.Map; 031 032/** 033 * @author michael 034 */ 035public class FlexWrapperFactory implements ObjectWrapperFactory { 036 037 @Override 038 public boolean hasWrapperFor(Object object) { 039 Class<?> objectClass = object.getClass(); 040 if (Collection.class.isAssignableFrom(objectClass)) { 041 return false; 042 } else if (Map.class.isAssignableFrom(objectClass)) { 043 return true; 044 } 045 return TableInfoFactory.ofEntityClass(objectClass) != null; 046 } 047 048 @SuppressWarnings("unchecked") 049 @Override 050 public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) { 051 if (Map.class.isAssignableFrom(object.getClass())) { 052 if (object.getClass() == Row.class){ 053 //取消 row 的 user_name 转换为 userName,否则再次保存时无法进行保存 054 //https://github.com/mybatis-flex/mybatis-flex/issues/244 055 return new MapWrapper(metaObject, (Map<String, Object>) object); 056 } 057 return new FlexMapWrapper(metaObject, (Map<String, Object>) object); 058 } else { 059 return new FlexBeanWrapper(metaObject, object); 060 } 061 } 062 063 static class FlexBeanWrapper extends BeanWrapper { 064 065 private final Object entity; 066 private final TableInfo tableInfo; 067 068 public FlexBeanWrapper(MetaObject metaObject, Object object) { 069 super(metaObject, object); 070 this.entity = object; 071 this.tableInfo = TableInfoFactory.ofEntityClass(object.getClass()); 072 } 073 074 @Override 075 public void set(PropertyTokenizer prop, Object value) { 076 Object v = tableInfo.invokeOnSetListener(entity, prop.getName(), value); 077 super.set(prop, v); 078 } 079 } 080 081 082 static class FlexMapWrapper extends MapWrapper { 083 084 public FlexMapWrapper(MetaObject metaObject, Map<String, Object> map) { 085 super(metaObject, map); 086 } 087 088 @Override 089 public String findProperty(String name, boolean useCamelCaseMapping) { 090 return useCamelCaseMapping && (Character.isUpperCase(name.charAt(0)) || name.contains("_")) ? StringUtil.underlineToCamel(name) : name; 091 } 092 } 093 094}