iTranslated by AI
Mapping Java List to PostgreSQL ARRAY with MyBatis
While MyBatis provides org.apache.ibatis.type.ArrayTypeHandler to map Java arrays to database ARRAY types, it cannot be used to map Java Lists.
When writing code, you often want to use a List (e.g., List<String>) rather than an array (e.g., String[]), so I will explain how to map a List.
The versions used for confirmation are as follows:
- PostgreSQL 15.3
- Java 17
- Spring Boot 3.1.1
- MyBatis 3.5.13
Table Definition
For this example, we will define a table like the following:
CREATE TABLE list_records (
id serial,
booleans boolean[],
shorts smallint[],
integers integer[],
longs bigint[],
floats real[],
doubles double precision[],
strings text[]
);
Entity
We will define an entity class matching the table as follows:
package com.github.onozaty.mybatis.pg.example.domain;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ListRecord {
private int id;
private List<Boolean> booleans;
private List<Short> shorts;
private List<Integer> integers;
private List<Long> longs;
private List<Float> floats;
private List<Double> doubles;
private List<String> strings;
}
Repository
This is the repository.
Since it's a List, or rather a generic type, common mapping cannot be specified for each type parameter, so you must specify a TypeHandler individually.
package com.github.onozaty.mybatis.pg.example.repository;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.github.onozaty.mybatis.pg.example.domain.ListRecord;
import com.github.onozaty.mybatis.pg.type.list.BooleanListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.DoubleListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.FloatListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.IntegerListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.LongListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.ShortListTypeHandler;
import com.github.onozaty.mybatis.pg.type.list.StringListTypeHandler;
@Mapper
public interface ListRepository {
@Select("""
INSERT INTO list_records (
booleans,
shorts,
integers,
longs,
floats,
doubles,
strings
)
VALUES (
#{booleans, typeHandler=booleanListTypeHandler},
#{shorts, typeHandler=shortListTypeHandler},
#{integers, typeHandler=integerListTypeHandler},
#{longs, typeHandler=longListTypeHandler},
#{floats, typeHandler=floatListTypeHandler},
#{doubles, typeHandler=doubleListTypeHandler},
#{strings, typeHandler=stringListTypeHandler}
)
RETURNING *
""")
@Results({
@Result(property = "booleans", column = "booleans", typeHandler = BooleanListTypeHandler.class),
@Result(property = "shorts", column = "shorts", typeHandler = ShortListTypeHandler.class),
@Result(property = "integers", column = "integers", typeHandler = IntegerListTypeHandler.class),
@Result(property = "longs", column = "longs", typeHandler = LongListTypeHandler.class),
@Result(property = "floats", column = "floats", typeHandler = FloatListTypeHandler.class),
@Result(property = "doubles", column = "doubles", typeHandler = DoubleListTypeHandler.class),
@Result(property = "strings", column = "strings", typeHandler = StringListTypeHandler.class)
})
ListRecord insert(ListRecord record);
@Select("""
SELECT * FROM list_records
""")
@Results({
@Result(property = "booleans", column = "booleans", typeHandler = BooleanListTypeHandler.class),
@Result(property = "shorts", column = "shorts", typeHandler = ShortListTypeHandler.class),
@Result(property = "integers", column = "integers", typeHandler = IntegerListTypeHandler.class),
@Result(property = "longs", column = "longs", typeHandler = LongListTypeHandler.class),
@Result(property = "floats", column = "floats", typeHandler = FloatListTypeHandler.class),
@Result(property = "doubles", column = "doubles", typeHandler = DoubleListTypeHandler.class),
@Result(property = "strings", column = "strings", typeHandler = StringListTypeHandler.class)
})
List<ListRecord> selectAll();
@Select("""
SELECT * FROM list_records WHERE id = #{id}
""")
@Results({
@Result(property = "booleans", column = "booleans", typeHandler = BooleanListTypeHandler.class),
@Result(property = "shorts", column = "shorts", typeHandler = ShortListTypeHandler.class),
@Result(property = "integers", column = "integers", typeHandler = IntegerListTypeHandler.class),
@Result(property = "longs", column = "longs", typeHandler = LongListTypeHandler.class),
@Result(property = "floats", column = "floats", typeHandler = FloatListTypeHandler.class),
@Result(property = "doubles", column = "doubles", typeHandler = DoubleListTypeHandler.class),
@Result(property = "strings", column = "strings", typeHandler = StringListTypeHandler.class)
})
ListRecord select(int id);
@Update("""
UPDATE list_records
SET
id = #{id},
booleans = #{booleans, typeHandler=booleanListTypeHandler},
shorts = #{shorts, typeHandler=shortListTypeHandler},
integers = #{integers, typeHandler=integerListTypeHandler},
longs = #{longs, typeHandler=longListTypeHandler},
floats = #{floats, typeHandler=floatListTypeHandler},
doubles = #{doubles, typeHandler=doubleListTypeHandler},
strings = #{strings, typeHandler=stringListTypeHandler}
WHERE
id = #{id}
""")
void update(ListRecord record);
}
TypeHandler Configuration
As mentioned at the beginning, MyBatis's ArrayTypeHandler cannot map them, so we will use the TypeHandlers provided by the following library:
Add the dependency. (Since I'm using Gradle, add it to build.gradle.)
implementation 'com.github.onozaty:mybatis-postgresql-typehandlers:1.0.2'
When writing TypeHandler mappings in the repository, it is tedious to write the fully qualified class names including the package name, so we specify aliases.
By specifying the package name in mybatis.type-aliases-package in Spring Boot's application.properties, aliases are set for the TypeHandlers in that package with the first letter of the class name in lowercase.
mybatis.type-aliases-package=com.github.onozaty.mybatis.pg.type.list
Complete Code
The full code can be found in the project below.
In addition to Lists, it includes code for mapping Java enums to PostgreSQL enums and Java objects to PostgreSQL JSONB.
Discussion