Skip to content

Commit

Permalink
增加参数enableBaseResultMapFlag 是否自动处理 Mapper 接口中其他手写指定 resultType 的返回结果类…
Browse files Browse the repository at this point in the history
…型为 resultMap。
  • Loading branch information
abel533 committed Nov 4, 2024
1 parent 1936590 commit 8e3e5a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.Reader;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
* @author liuzh
Expand All @@ -21,6 +22,7 @@ public class RawResultMapTest extends BaseTest {
protected Config getConfig() {
Config config = super.getConfig();
config.setStyle(Style.normal);
config.setEnableBaseResultMapFlag(true);
return config;
}

Expand Down Expand Up @@ -70,7 +72,7 @@ public void testSelect() {
users.forEach(u -> {
System.out.println(u);
Assert.assertNotNull(u.getUname());
Assert.assertNotNull(u.getAge());
Assert.assertNull(u.getAge());
Assert.assertNotNull(u.getCreateTime());
Assert.assertNotNull(u.getEmail());
});
Expand All @@ -88,10 +90,12 @@ public void testSelect() {
System.out.println("------------");

System.out.println("------getMapUser------");
System.out.println(mapper.getMapUser());
Map<String, Object> mapUser = mapper.getMapUser();
System.out.println(mapUser);
System.out.println("------------");

System.out.println(mapper.selectCount2());
Integer x = mapper.selectCount2();
System.out.println(x);
} finally {
sqlSession.close();
}
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/tk/mybatis/mapper/entity/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public class Config {
* 是否设置 javaType
*/
private boolean useJavaType;
/**
* 是否自动处理 Mapper 接口中其他手写指定 resultType 的返回结果类型为 resultMap
*/
private boolean enableBaseResultMapFlag;

public String getCatalog() {
return catalog;
Expand Down Expand Up @@ -316,6 +320,14 @@ public void setUseJavaType(boolean useJavaType) {
this.useJavaType = useJavaType;
}

public boolean isEnableBaseResultMapFlag() {
return enableBaseResultMapFlag;
}

public void setEnableBaseResultMapFlag(boolean enableBaseResultMapFlag) {
this.enableBaseResultMapFlag = enableBaseResultMapFlag;
}

/**
* 配置属性
*
Expand Down
24 changes: 14 additions & 10 deletions core/src/main/java/tk/mybatis/mapper/mapperhelper/MapperHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ public void processMappedStatement(MappedStatement ms) {
}

// 如果是原生mybatisSqlSource的查询,添加ResultMap
if (ms.getSqlSource() instanceof RawSqlSource
if (config.isEnableBaseResultMapFlag()
&& ms.getSqlSource() instanceof RawSqlSource
&& ms.getSqlCommandType() == SqlCommandType.SELECT) {
if (ms.getResultMaps() != null && !ms.getResultMaps().isEmpty()) {
setRawSqlSourceMapper(ms);
Expand Down Expand Up @@ -402,15 +403,18 @@ public void setSqlSource(MappedStatement ms, MapperTemplate mapperTemplate) {
* JPA的注解优先级将高于mybatis自动映射
*/
public void setRawSqlSourceMapper(MappedStatement ms) {

EntityTable entityTable = EntityHelper.getEntityTableOrNull(ms.getResultMaps().get(0).getType());
if (entityTable != null) {
List<ResultMap> resultMaps = new ArrayList<>();
ResultMap resultMap = entityTable.getResultMap(ms.getConfiguration());
if (resultMap != null) {
resultMaps.add(resultMap);
MetaObject metaObject = MetaObjectUtil.forObject(ms);
metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
ResultMap rm = ms.getResultMaps().get(0);
//不处理已经配置映射的查询
if (rm.getResultMappings().isEmpty()) {
EntityTable entityTable = EntityHelper.getEntityTableOrNull(rm.getType());
if (entityTable != null) {
List<ResultMap> resultMaps = new ArrayList<>();
ResultMap resultMap = entityTable.getResultMap(ms.getConfiguration());
if (resultMap != null) {
resultMaps.add(resultMap);
MetaObject metaObject = MetaObjectUtil.forObject(ms);
metaObject.setValue("resultMaps", Collections.unmodifiableList(resultMaps));
}
}
}
}
Expand Down

0 comments on commit 8e3e5a8

Please # to comment.