Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProviderSqlSource#createSqlSource passed a wrong type to languageDriver#createSqlSource #3412

Closed
mindfn opened this issue Feb 18, 2025 · 3 comments

Comments

@mindfn
Copy link

mindfn commented Feb 18, 2025

mybatis: latest

After I use mybaits-generator to generate dynamic-sql
so it will use ProviderSqlSource, the parameterObject will always be xxxProvider;

When parsing sql with RawSqlSource, the ParameterMapping#JavaType will always be java.lang.Object;

I'm wondering if there is any way to make sure the ParameterMapping to get the proper type;

Because in the current scenario; if I register a JsonTypeHanlder like the following; the clazz here will become Java.lang.Object

public class JsonTypeHandler<T> extends BaseTypeHandler<T> {
    
    private final Class<T> clazz;

    public JsonTypeHandler(Class<T> clazz) {
        // Java.lang.Object
        this.clazz = clazz;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {    
    }
}
@jeffgbutler
Copy link
Member

The type handler will be calculated based on the type of the object in the parameter map - it won't be xxxProvider. But this sometimes fails, so it is best to explicitly set the type handler. The generator allows you to do this, and it will carry into the generated code.

In the generator configuration, do something like this:

<table tableName="some_table">
  <columnOverride column="some_column" typeHandler="foo.JsonTypeHandler"/>
</tableName>

@mindfn
Copy link
Author

mindfn commented Feb 20, 2025

thanks for your reply.

In fact, I am using org.mybatis.dynamic.sql.util.mybatis3#insert to save the data, So parameterObject is DefaultInsertStatementProvider;
Image
Therefore, the MetaClass obtained by parsing parameterType (DefaultInsertStatementProvider) cannot find "row.xxx";
(SqlSourceBuilder#buildParameterMapping), then it will continue to try to get the JavaType by "originSql";
only if JavaType is missing from "original Sql", Object.class will be used;
So we need to add 'forceJavaTypeIntoMapping' in the generator configuration to avoid this situation

<table tableName="some_table">
  <columnOverride column="some_column" typeHandler="foo.JsonTypeHandler">
     <property name="forceJavaTypeIntoMapping" value="true"/>
  </columnOverride>
</tableName>

https://mybatis.org/generator/configreference/columnOverride.html

@jeffgbutler
Copy link
Member

Glad you found the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants