mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 修复返回 map 时,配置 map-underscore-to-camel-case 不起作用的问题
This commit is contained in:
parent
1e3476fc18
commit
465fbde56c
@ -24,7 +24,6 @@ import com.mybatisflex.core.mybatis.executor.FlexBatchExecutor;
|
|||||||
import com.mybatisflex.core.mybatis.executor.FlexReuseExecutor;
|
import com.mybatisflex.core.mybatis.executor.FlexReuseExecutor;
|
||||||
import com.mybatisflex.core.mybatis.executor.FlexSimpleExecutor;
|
import com.mybatisflex.core.mybatis.executor.FlexSimpleExecutor;
|
||||||
import com.mybatisflex.core.row.RowMapper;
|
import com.mybatisflex.core.row.RowMapper;
|
||||||
import com.mybatisflex.core.table.EntityWrapperFactory;
|
|
||||||
import com.mybatisflex.core.table.TableInfo;
|
import com.mybatisflex.core.table.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
@ -58,13 +57,13 @@ public class FlexConfiguration extends Configuration {
|
|||||||
public FlexConfiguration(Environment environment) {
|
public FlexConfiguration(Environment environment) {
|
||||||
super(environment);
|
super(environment);
|
||||||
setMapUnderscoreToCamelCase(true);
|
setMapUnderscoreToCamelCase(true);
|
||||||
setObjectWrapperFactory(new EntityWrapperFactory());
|
setObjectWrapperFactory(new FlexWrapperFactory());
|
||||||
initDefaultMappers();
|
initDefaultMappers();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlexConfiguration() {
|
public FlexConfiguration() {
|
||||||
setMapUnderscoreToCamelCase(true);
|
setMapUnderscoreToCamelCase(true);
|
||||||
setObjectWrapperFactory(new EntityWrapperFactory());
|
setObjectWrapperFactory(new FlexWrapperFactory());
|
||||||
initDefaultMappers();
|
initDefaultMappers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,33 +13,45 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.MetaObject;
|
||||||
import org.apache.ibatis.reflection.property.PropertyTokenizer;
|
import org.apache.ibatis.reflection.property.PropertyTokenizer;
|
||||||
import org.apache.ibatis.reflection.wrapper.BeanWrapper;
|
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.ObjectWrapper;
|
||||||
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
|
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class EntityWrapperFactory implements ObjectWrapperFactory {
|
/**
|
||||||
|
* @author michael
|
||||||
|
*/
|
||||||
|
public class FlexWrapperFactory implements ObjectWrapperFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasWrapperFor(Object object) {
|
public boolean hasWrapperFor(Object object) {
|
||||||
Class<?> objectClass = object.getClass();
|
Class<?> objectClass = object.getClass();
|
||||||
if (Map.class.isAssignableFrom(objectClass) ||
|
if (Collection.class.isAssignableFrom(objectClass)) {
|
||||||
Collection.class.isAssignableFrom(objectClass)) {
|
|
||||||
return false;
|
return false;
|
||||||
|
} else if (Map.class.isAssignableFrom(objectClass)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return TableInfoFactory.ofEntityClass(objectClass) != null;
|
return TableInfoFactory.ofEntityClass(objectClass) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
|
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object) {
|
||||||
|
if (Map.class.isAssignableFrom(object.getClass())) {
|
||||||
|
return new FlexMapWrapper(metaObject, (Map<String, Object>) object);
|
||||||
|
} else {
|
||||||
return new FlexBeanWrapper(metaObject, object);
|
return new FlexBeanWrapper(metaObject, object);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class FlexBeanWrapper extends BeanWrapper {
|
static class FlexBeanWrapper extends BeanWrapper {
|
||||||
|
|
||||||
@ -57,7 +69,19 @@ public class EntityWrapperFactory implements ObjectWrapperFactory {
|
|||||||
Object v = tableInfo.invokeOnSetListener(entity, prop.getName(), value);
|
Object v = tableInfo.invokeOnSetListener(entity, prop.getName(), value);
|
||||||
super.set(prop, v);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@UseDataSource("ds1")
|
@UseDataSource("ds1")
|
||||||
@ -152,4 +153,12 @@ public class AccountController {
|
|||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/map/{id}")
|
||||||
|
// @Transactional
|
||||||
|
public Map map(@PathVariable("id") Long id) {
|
||||||
|
return myAccountMapper.selectMapById(id);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,6 +20,8 @@ import com.mybatisflex.test.model.Account;
|
|||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public interface MyAccountMapper extends AccountMapper {
|
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}")
|
@Select("select * from tb_account where id = #{id} and id =#{id}")
|
||||||
Account selectById(@Param("id") Object id);
|
Account selectById(@Param("id") Object id);
|
||||||
|
|
||||||
|
@Select("select * from tb_account where id = #{id}")
|
||||||
|
Map selectMapById(@Param("id") Object id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ spring:
|
|||||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
# driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://localhost:3306/flex_test
|
url: jdbc:mysql://localhost:3306/flex_test
|
||||||
username: root
|
username: root
|
||||||
password: 12345678
|
password: 123456
|
||||||
# driver-class-name:
|
# driver-class-name:
|
||||||
# datasource:
|
# datasource:
|
||||||
# driver-class-name: org.h2.Driver
|
# driver-class-name: org.h2.Driver
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user