fix: 修复返回 map 时,配置 map-underscore-to-camel-case 不起作用的问题

This commit is contained in:
开源海哥 2023-08-23 15:02:33 +08:00
parent 1e3476fc18
commit 465fbde56c
5 changed files with 46 additions and 9 deletions

View File

@ -24,7 +24,6 @@ import com.mybatisflex.core.mybatis.executor.FlexBatchExecutor;
import com.mybatisflex.core.mybatis.executor.FlexReuseExecutor;
import com.mybatisflex.core.mybatis.executor.FlexSimpleExecutor;
import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.table.EntityWrapperFactory;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.StringUtil;
@ -58,13 +57,13 @@ public class FlexConfiguration extends Configuration {
public FlexConfiguration(Environment environment) {
super(environment);
setMapUnderscoreToCamelCase(true);
setObjectWrapperFactory(new EntityWrapperFactory());
setObjectWrapperFactory(new FlexWrapperFactory());
initDefaultMappers();
}
public FlexConfiguration() {
setMapUnderscoreToCamelCase(true);
setObjectWrapperFactory(new EntityWrapperFactory());
setObjectWrapperFactory(new FlexWrapperFactory());
initDefaultMappers();
}

View File

@ -13,32 +13,44 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.table;
package com.mybatisflex.core.mybatis;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.reflection.wrapper.BeanWrapper;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import java.util.Collection;
import java.util.Map;
public class EntityWrapperFactory implements ObjectWrapperFactory {
/**
* @author michael
*/
public class FlexWrapperFactory implements ObjectWrapperFactory {
@Override
public boolean hasWrapperFor(Object object) {
Class<?> objectClass = object.getClass();
if (Map.class.isAssignableFrom(objectClass) ||
Collection.class.isAssignableFrom(objectClass)) {
if (Collection.class.isAssignableFrom(objectClass)) {
return false;
} else if (Map.class.isAssignableFrom(objectClass)) {
return true;
}
return TableInfoFactory.ofEntityClass(objectClass) != null;
}
@Override
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
return new FlexBeanWrapper(metaObject, object);
if (Map.class.isAssignableFrom(object.getClass())) {
return new FlexMapWrapper(metaObject, (Map<String, Object>) object);
} else {
return new FlexBeanWrapper(metaObject, object);
}
}
static class FlexBeanWrapper extends BeanWrapper {
@ -57,7 +69,19 @@ public class EntityWrapperFactory implements ObjectWrapperFactory {
Object v = tableInfo.invokeOnSetListener(entity, prop.getName(), value);
super.set(prop, v);
}
}
static class FlexMapWrapper extends MapWrapper {
public FlexMapWrapper(MetaObject metaObject, Map<String, Object> map) {
super(metaObject, map);
}
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
return useCamelCaseMapping ? StringUtil.underlineToCamel(name) : name;
}
}
}

View File

@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@RestController
@UseDataSource("ds1")
@ -152,4 +153,12 @@ public class AccountController {
return "ok";
}
@GetMapping("/map/{id}")
// @Transactional
public Map map(@PathVariable("id") Long id) {
return myAccountMapper.selectMapById(id);
}
}

View File

@ -20,6 +20,8 @@ import com.mybatisflex.test.model.Account;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.Map;
public interface MyAccountMapper extends AccountMapper {
@ -28,4 +30,7 @@ public interface MyAccountMapper extends AccountMapper {
@Select("select * from tb_account where id = #{id} and id =#{id}")
Account selectById(@Param("id") Object id);
@Select("select * from tb_account where id = #{id}")
Map selectMapById(@Param("id") Object id);
}

View File

@ -7,7 +7,7 @@ spring:
# driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678
password: 123456
# driver-class-name:
# datasource:
# driver-class-name: org.h2.Driver