-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use param names for constructor auto-mapping #2192
Comments
There are two steps in constructor auto-mapping, basically.
If I understand correctly, Kotlin's data class has only one constructor, so step 1 should not be a problem [1]. Regarding step 2, MyBatis currently relies on the order of the result. [1] When there are multiple constructors, you may have to use |
data class User(
val id: Long,
val name: String
) The structure of table When I use |
I only have a vague plan at this point, but yes, it should work. |
Hi @GoldSubmarine , I have submitted a PR #2196 . |
// mapper
@Select("select name userName from users where id = #{id}")
User2 selectUserIdAndUserName(Integer id);
// Do
public class User2 {
private Integer userId;
private String name;
public User2(@Param("userId") Integer userId, @Param("userName") String name) {
super();
this.userId = userId;
this.name = name;
}
// ...getter setter
} I added a unit test, should User2 be built successfully? From my point of view this should be supported and Have you considered primitive types yet. // kotlin
data class User(
val id: Long,
val name: String
)
// User.class
public class User {
private long userId;
private String name;
public User2(long userId, String name) {
this.userId = userId;
this.name = name;
}
// ...getter setter
} If the |
Thank you for the reply, @GoldSubmarine ! The first case won't work. And there should be no problem with primitives. You should check out the branch and do various tests yourself. |
I have tested the first case locally, it's not what I expected and I just asked, but thank you for teaching me to test through GitHub CLI.
I know this is more rigorous, but I just want to build the object as much as possible based on the result, and it should be null without being selected, which I think is in line with expectations, manually specifying it every time is too cumbersome. I think the problem is the same as whether JSON has a key or not when it's value is null. Being too strict can make it difficult to use. Is it possible to provide a configuration to adjust the strict? |
I've been thinking about it, but it seems like a clear error case to me if a constructor argument of an immutable object is missing. Let's keep this open and see what other devs (and users) think. We can add a new config switch to control the behavior, but only if it is absolutely necessary. Cc: @h3adache @kazuki43zoo @eddumelendez @jeffgbutler @emacarron |
I had a same problem on Kotlin, and I solved it with Show the details in here. |
@Cenibee Yes, but non-null check does not take effect if not use the constructor with parameter. |
@GoldSubmarine However, on the one hand, in the DAO class (especially Kotlin's class and data class), I think that non-null attributes that does not exist in the mapper have default values or are derived from another attributes. |
@GoldSubmarine , |
#2196 is merged. Just to reiterate, when there is no matching column in the result set, an exception is thrown. Thank you all for your contributions! |
I want MyBatis to map automatically with the constructor by argument names without defining
<constructor>
...</constructor>
or@ConstructorArgs
.I am using kotlin's data class, I want mybatis to be automatically mapped, just like I did through the empty constructor and getter setter in java.
The text was updated successfully, but these errors were encountered: