-
|
I defined and registered a TypeHandler for the java type "java.nio.file.Path" to convert a Path to a string, like this (I’m writing compactly because it’s very straightforward): @MappedTypes({Path.class})
@MappedJdbcTypes({JdbcType.NULL, JdbcType.VARCHAR, JdbcType.CHAR})
public class PathTypeHandler extends BaseTypeHandler<Path> {
@Override public void setNonNullParameter(PreparedStatement ps, int i, Path parameter, JdbcType jdbcType)
{ps.setString(i, parameter.normalize().toString());}
@Override public Path getNullableResult(ResultSet rs, String columnName) throws SQLException
{return this.convertPath(rs.getString(columnName));}
@Override public Path getNullableResult(ResultSet rs, int columnIndex) throws SQLException
{return this.convertPath(rs.getString(columnIndex));}
@Override public Path getNullableResult(CallableStatement cs, int columnIndex) throws SQLException
{return this.convertPath(cs.getString(columnIndex));}
private Path convertPath(String string)
{return Path.of(Optional.ofNullable(string).orElse(""));}
}I found that when there is only one parameter in the method, the type of the parameter is correctly inferred as Path, and call the PathTypeHandler I have defined, like this: @Mapper
public interface CodeClassStorage {
@Select("select * from code_class where path = #{path}")
CodeClass getByPath(@Param("path") Path path);
}
//CodeClass Definition
@AllArgsConstructor
@Getter
abstract class CodeClass {
private String id;
private Path path;
}But when there is more than one parameter in the method, the type is not correctly inferred, like this: public interface CodeClassStorage {
@Select("select * from code_class where id = #{id} and path = #{path}")
CodeClass getByIdAndPath(@Param("id") String id, @Param("path") Path path);
}The error message is as follows: Error querying database.
Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='path', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}.
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.
Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.
Cause: java.sql.SQLException: Cannot convert class sun.nio.fs.UnixPath to SQL type requested due to com.mysql.cj.exceptions.WrongArgumentException - Invalid argument value: java.io.NotSerializableExceptionIt seems that the parameter 'path' is inferred as an Object, which makes it doesn't call the PathTypeHandler. public interface CodeClassStorage {
@Select("select * from code_class where id = #{id} and path = #{path,javaType=java.nio.file.Path}")
CodeClass getByIdAndPath(@Param("id") String id, @Param("path") Path path);
}Or this: public interface CodeClassStorage {
@Select("select * from code_class where id = 1 and path = #{path}")
CodeClass getByIdAndPath(@Param("path") Path path);
}The version of the environment like this, which is the newest one of mybatis integrated SpringBoot: <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>Based on the above test, I believe the issue lies with the automatic inference of the method parameter type. I just want to know whether it’s a bug, a feature, or if I’m doing something wrong. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hello @TanHonN , This probably is the same issue as #1445 . If your problem reproduces with the latest 3.6.0-SNAPSHOT, please create a small repro project and share it on your GitHub repo. |
Beta Was this translation helpful? Give feedback.
Hello @TanHonN ,
This probably is the same issue as #1445 .
The fix has already been committed and will be included in the upcoming version 3.6.0.
As there are a lot of major changes in 3.6.0, you should test your solution with the latest 3.6.0-SNAPSHOT.
If your problem reproduces with the latest 3.6.0-SNAPSHOT, please create a small repro project and share it on your GitHub repo.
Here are some templates and examples: https://github.com/harawata/mybatis-issues