Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
Expand Down Expand Up @@ -126,4 +128,25 @@ void forTypeDiscriminator() {
}
}

@Test
void insertDynamic() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
String insertStatement = "insert into person (id, firstName, lastName, personType) values("
+ "#{parameters.p1,jdbcType=INTEGER}," + "#{parameters.p2,jdbcType=VARCHAR},"
+ "#{parameters.p3,jdbcType=VARCHAR}," + "#{parameters.p4,jdbcType=INTEGER,"
+ "javaType=org.apache.ibatis.submitted.enumtypehandler_on_annotation.Person$PersonType,"
+ "typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler})";

Map<String, Object> params = new HashMap<>();
params.put("p1", 100);
params.put("p2", "Fred");
params.put("p3", "Flintstone");
params.put("p4", Person.PersonType.PERSON);

int rows = personMapper.insertDynamic(insertStatement, params);
assertThat(rows).isEqualTo(1);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,13 @@
*/
package org.apache.ibatis.submitted.enumtypehandler_on_annotation;

import java.util.Map;

import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.Case;
import org.apache.ibatis.annotations.ConstructorArgs;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
Expand Down Expand Up @@ -56,11 +60,16 @@ public interface PersonMapper {
column = "personType", javaType = PersonType.class, typeHandler = EnumOrdinalTypeHandler.class,
// Switch using enum constant name(PERSON or EMPLOYEE) at cases attribute
cases = {
@Case(value = "PERSON", type = Person.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
, @Case(value = "EMPLOYEE", type = Employee.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
@Case(value = "PERSON", type = Person.class, results = {@Result(property = "personType",
column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
, @Case(value = "EMPLOYEE", type = Employee.class, results = {@Result(property = "personType",
column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
})
// @formatter:on
@Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
Person findOneUsingTypeDiscriminator(int id);

@Insert("${sql}")
int insertDynamic(@Param("sql") String sql, @Param("parameters") Map<String, Object> parameters);

}
Loading