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

Cannot use object properties correctly if SPRING_NAMED_PARAMETER #216

Open
yu64 opened this issue Nov 11, 2023 · 0 comments
Open

Cannot use object properties correctly if SPRING_NAMED_PARAMETER #216

yu64 opened this issue Nov 11, 2023 · 0 comments

Comments

@yu64
Copy link

yu64 commented Nov 11, 2023

I using below version:

  1. org.mybatis.scripting:mybatis-thymeleaf:1.0.4
  2. org.springframework:spring-jdbc:6.0.9
  3. java 17

This issue occurs when using object properties with mb:p.

Probably because Spring JDBC does not allow you to specify properties of objects.

Exception thrown

java.lang.RuntimeException: org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied for the SQL parameter 'person.id': No value registered for key 'person.id'

This issue can be avoided by using mb:bind="person.id=${person.id}".

However, it cannot be used inside th:each. (Correctly, it will be overwritten by the value from the last loop.)

This causes the "Bulk insert" section of the document to fail.

The code is as follows.

Domain objects

public class Person {

        private String id;
        private String name;
        private String birthDate;

        public String getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public String getBirthDate() {
            return birthDate;
        }
}

Sql Template

INSERT INTO
    
    person

    VALUES(
        /*[# mb:p="person.id"/]*/,
        /*[# mb:p="person.name"/]*/,
        /*[# mb:p="person.birthDate"/]*/
    )

;

Creating SqlGenerator Method

public SqlGenerator createSqlGenerator() {

        SqlGeneratorConfig config = SqlGeneratorConfig.newInstanceWithCustomizer(c ->
            c.getDialect().setBindVariableRenderInstance(BindVariableRender.BuiltIn.SPRING_NAMED_PARAMETER)
        );

        return new SqlGenerator(config);
}

Run query Method

public int insert(String sqlTemplate, SqlGenerator gen, NamedParameterJdbcTemplate jdbc) {
        final Person person = new Person();
        person.id = "ID-001";
        person.name = "John Doe";
        person.birthDate = "2001-01-01"; 

        final Map<String, Object> params = new HashMap<>();
        params.put("person", person);

        System.out.println(params);

        final String jdbcSql = gen.generate(sqlTemplate, params, params::put);
        
        System.out.println(jdbcSql);
        System.out.println(params);

        return jdbc.update(jdbcSql, params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant