简体   繁体   中英

MyBatis resultMap with a Builder in the POJO

Suppose I have a java class representing a Person which uses a builder pattern:

package com.xyz.domain;

public class Person {

    private final int id;
    private final String firstName;
    private final String middleName;
    private final String lastName;
    private final String address;
    private final String country;

    private Person(PersonBuilder builder) {
        this.id = id;
        this.firstName = builder.firstName;
        this.middleName = builder.middleName;
        this.lastName = builder.lastName;
        this.address = builder.address;
        this.country = builder.country;
    }

    // getters go here

    public static class PersonBuilder {

        private int id;
        private final String firstName;
        private final String lastName;

        private String middleName;
        private String address;
        private String country;

        public PersonBuilder(int id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public PersonBuilder withMiddleName(String middleName) {
            this.middleName = middleName;
            return this;
        }

        public PersonBuilder withAddress(String address) {
            this.address = address;
            return this;
        }

        public PersonBuilder withCountry(String country) {
            this.country = country;
            return this;
        }

        public Person build() {
            return new Person(this);
        }
    }
}

I'd like to use this as the type in a mybatis resultMap as so:

<resultMap id="Person" type="com.xyz.domain.Person">
    <id property="id" column="id" />
    <result property="firstName" column="first_name" />
    <result property="middleName" column="middle_name" />
    <result property="lastName" column="last_name" />
    <result property="address" column="address" />
    <result property="country" column="country" />
</resultMap>

This, however, yields a 500 error in my Spring app when I call an endpoint which uses this result map. I've also tried setting the type parameter in the resultMap to com.xyz.domain.Person.PersonBuilder and also get an error. Is there any way to make this work without sacrificing the builder pattern in the Person class?

Mark the POJO with @Alias("Person") annotation. And also make sure that the package com.xyz.domain is included to scan POJOs while configuring myBatis data connection.

I was able to resolve the issue by updating the result map like so

<resultMap id="Person" type="com.xyz.domain.Person$PersonBuilder">
    <constructor>
        <idArg name="id" column="id" />
        <arg name="firstName" column="first_name" />
        <arg name="lastName" column="last_name" />
    </constructor>
    <result property="middleName" column="middle_name" />
    <result property="address" column="address" />
    <result property="country" column="country" />
</resultMap>

This creates a List of PersonBuilder objects, but it was easy enough to iterate through that list and call the build() method on each list item in java: personBuilderList.stream().map(Person.PersonBuilder::build).collect(toList())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM