diff --git a/src/main/java/org/apache/ibatis/builder/ParameterMappingTokenHandler.java b/src/main/java/org/apache/ibatis/builder/ParameterMappingTokenHandler.java index 84ee5d74628..d1d1c5b3b82 100644 --- a/src/main/java/org/apache/ibatis/builder/ParameterMappingTokenHandler.java +++ b/src/main/java/org/apache/ibatis/builder/ParameterMappingTokenHandler.java @@ -76,6 +76,8 @@ public List getParameterMappings() { @Override public String handleToken(String content) { + genericType = null; + typeHandler = null; parameterMappings.add(buildParameterMapping(content)); return "?"; } diff --git a/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/EnumTypeHandlerUsingAnnotationTest.java b/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/EnumTypeHandlerUsingAnnotationTest.java index 14a119a6306..8ae74434e11 100644 --- a/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/EnumTypeHandlerUsingAnnotationTest.java +++ b/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/EnumTypeHandlerUsingAnnotationTest.java @@ -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. @@ -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; @@ -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 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); + } + } + } diff --git a/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/PersonMapper.java b/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/PersonMapper.java index 652dbd27121..1bf7e329f3f 100644 --- a/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/PersonMapper.java +++ b/src/test/java/org/apache/ibatis/submitted/enumtypehandler_on_annotation/PersonMapper.java @@ -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. @@ -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; @@ -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 parameters); + }