Skip to content

Commit 486dd74

Browse files
author
Jarvis
committed
closed #96 ResultMap support constructor
1 parent 178a58e commit 486dd74

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.springframework.data.mybatis.mapping;
2+
3+
/**
4+
* @author Jarvis Song
5+
*/
6+
public class MybatisManyToManyAssociation extends MybatisOneToManyAssociation {
7+
8+
public MybatisManyToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
9+
super(inverse, obverse);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.springframework.data.mybatis.mapping;
2+
3+
/**
4+
* @author Jarvis Song
5+
*/
6+
public class MybatisOneToManyAssociation extends MybatisAssociation {
7+
8+
public MybatisOneToManyAssociation(MybatisPersistentProperty inverse, MybatisPersistentProperty obverse) {
9+
super(inverse, obverse);
10+
}
11+
}

src/main/java/org/springframework/data/mybatis/mapping/MybatisPersistentPropertyImpl.java

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ protected Association<MybatisPersistentProperty> createAssociation() {
9898
return new MybatisOneToOneAssociation(this, null);
9999
}
100100

101+
if (null != findAnnotation(OneToMany.class)) {
102+
return new MybatisOneToManyAssociation(this, null);
103+
}
104+
105+
if (null != findAnnotation(ManyToMany.class)) {
106+
return new MybatisManyToManyAssociation(this, null);
107+
}
108+
101109
return new MybatisAssociation(this, null);
102110

103111
}

src/main/java/org/springframework/data/mybatis/repository/support/MybatisSimpleRepositoryMapperGenerator.java

+71-5
Original file line numberDiff line numberDiff line change
@@ -787,24 +787,85 @@ public void doWithPersistentProperty(PersistentProperty<?> pp) {
787787

788788
private void buildInnerResultMap(final StringBuilder builder, final MybatisPersistentEntity<?> persistentEntity, final String prefix) {
789789

790+
final StringBuilder constructorBuilder = new StringBuilder();
791+
final StringBuilder resultBuilder = new StringBuilder();
792+
793+
PreferredConstructor<?, MybatisPersistentProperty> persistenceConstructor = persistentEntity.getPersistenceConstructor();
794+
if (null != persistenceConstructor && persistenceConstructor.hasParameters()) {
795+
constructorBuilder.append("<constructor>");
796+
}
797+
798+
790799
persistentEntity.doWithProperties(new SimplePropertyHandler() {
791800
@Override
792801
public void doWithPersistentProperty(PersistentProperty<?> pp) {
793802
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
803+
804+
if (persistentEntity.isConstructorArgument(property)) {
805+
if (property.isIdProperty()) {
806+
807+
if (property.isCompositeId()) {
808+
809+
MybatisPersistentEntityImpl<?> idEntity = context.getPersistentEntity(property.getActualType());
810+
if (null != idEntity) {
811+
idEntity.doWithProperties(new SimplePropertyHandler() {
812+
@Override
813+
public void doWithPersistentProperty(PersistentProperty<?> pp) {
814+
MybatisPersistentProperty property = (MybatisPersistentProperty) pp;
815+
builder.append(String.format("<idArg property=\"%s\" column=\"%s\" javaType=\"%s\" jdbcType=\"%s\"/>",
816+
property.getName() + "." + property.getName(),
817+
alias(prefix + property.getName()),
818+
property.getActualType().getName(),
819+
property.getJdbcType()
820+
));
821+
}
822+
});
823+
}
824+
825+
} else {
826+
builder.append(String.format("<idArg property=\"%s\" column=\"%s\" javaType=\"%s\" jdbcType=\"%s\"/>",
827+
property.getName(),
828+
alias(prefix + property.getName()),
829+
property.getActualType().getName(),
830+
property.getJdbcType()
831+
));
832+
}
833+
834+
835+
} else {
836+
constructorBuilder.append(String.format("<arg property=\"%s\" column=\"%s\" javaType=\"%s\" jdbcType=\"%s\""
837+
+ (null != property.getSpecifiedTypeHandler() ? (" typeHandler=\"" + property.getSpecifiedTypeHandler().getName() + "\"") : "")
838+
+ " />",
839+
property.getName(),
840+
alias(prefix + property.getName()),
841+
property.getActualType().getName(),
842+
property.getJdbcType()
843+
));
844+
}
845+
return;
846+
}
847+
848+
794849
if (property.isIdProperty()) {
795-
buildInnerResultMapId(builder, property, prefix);
850+
buildInnerResultMapId(resultBuilder, property, prefix);
796851
return;
797852
}
798-
builder.append(String.format("<result property=\"%s\" column=\"%s\" javaType=\"%s\" jdbcType=\"%s\"" + (
799-
null != property.getSpecifiedTypeHandler() ? (" typeHandler=\"" + property.getSpecifiedTypeHandler().getName() + "\"") : ""
800-
) + " />",
853+
resultBuilder.append(String.format("<result property=\"%s\" column=\"%s\" javaType=\"%s\" jdbcType=\"%s\""
854+
+ (null != property.getSpecifiedTypeHandler() ? (" typeHandler=\"" + property.getSpecifiedTypeHandler().getName() + "\"") : "")
855+
+ " />",
801856
property.getName(),
802857
alias(prefix + property.getName()),
803858
property.getActualType().getName(),
804859
property.getJdbcType()
805860
));
806861
}
807862
});
863+
864+
865+
if (null != persistenceConstructor && persistenceConstructor.hasParameters()) {
866+
constructorBuilder.append("</constructor>");
867+
}
868+
builder.append(constructorBuilder).append(resultBuilder);
808869
}
809870

810871
private void buildInnerToOneAssociationResultMap(final StringBuilder builder, final PersistentProperty<? extends PersistentProperty<?>> inverse) {
@@ -880,13 +941,16 @@ private void buildInnerToManyAssociationResultMap(final StringBuilder builder, P
880941
MybatisPersistentEntityImpl<?> inversePersistentEntity = context.getPersistentEntity(actualType);
881942
if (null != inversePersistentEntity) {
882943
buildInnerResultMap(builder, inversePersistentEntity, inverse.getName() + ".");
944+
945+
883946
}
884947
builder.append("</collection>");
885948
}
886949

887950
private void buildResultMap(final StringBuilder builder) {
888951
builder.append("<resultMap id=\"ResultMap\" type=\"" + domainClass.getName() + "\">");
889952

953+
890954
buildInnerResultMap(builder, persistentEntity, "");
891955

892956
persistentEntity.doWithAssociations(new AssociationHandler<MybatisPersistentProperty>() {
@@ -902,8 +966,10 @@ public void doWithAssociation(Association<MybatisPersistentProperty> ass) {
902966
buildInnerToOneAssociationResultMap(builder, inverse);
903967
return;
904968
}
905-
if (null != inverse.findAnnotation(OneToMany.class) || null != inverse.findAnnotation(ManyToMany.class)) {
969+
970+
if (ass instanceof MybatisOneToManyAssociation) {
906971
buildInnerToManyAssociationResultMap(builder, inverse);
972+
return;
907973
}
908974

909975
}

0 commit comments

Comments
 (0)