Mybatis-3: Passing multiple parameters on @Many annotation

Created on 22 Jun 2014  ·  6Comments  ·  Source: mybatis/mybatis-3

In My Mapper:

@SelectProvider(type = CustomerSqlProvider.class, method = "getCustomerById")
@Results(value = {
@Result(property="parters",javaType=List.class,column="id",
many=@Many(select="getParterByCustomerId"))
})
public Customer getCustomerById(@Param("dbSuffix") String dbSuffix,@Param("customerId") Long customerId);


In My Provider:
public String getCustomerById(Map parameter) {
final String dbSuffix = (String) parameter.get("dbSuffix");
String sql= new SQL() {
{ SELECT("id,userName,firstName,lastName,middleName,cellPhone,phone")
FROM(dbSuffix+TABLE_NAME);
WHERE("id=#{customerId}");
}
}.toString();
return sql;
}

public String getParterByCustomerId(Map<String, Object> parameter) {
    final String dbSuffix = (String) parameter.get("dbSuffix");
    String sql = new SQL() {
        {
SELECT("sex,firstName,lastName,birthDay,passPortNum,relation,phone");
            FROM(dbSuffix+".t_parter");
            WHERE("customerId=#{customerId}");
        }
    }.toString();
    return sql;
}

In My Test:

   @Test
public void testGet() throws Exception {
    Customer result = customerMapper.getCustomerById(dbSuffix,id);
    System.out.println(result);
}

The Trace:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (com.kideasoft.data.sqlprovider.CustomerSqlProvider.getParterByCustomerId). Cause: java.lang.IllegalArgumentException: argument type mismatch
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy35.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
at com.sun.proxy.$Proxy57.getCustomerById(Unknown Source)
at com.kideasoft.service.CustomerService.getCustomerById(CustomerService.java:46)
at CustomerTest.testGet(CustomerTest.java:33)

Most helpful comment

Multiple columns must be specified as follows.

column="{param1=column1,param2=column2, ...}"

Please see this test case as an example.
And please make sure to use 3.4.1 or later because there was a bug prevented this from working (#649).

All 6 comments

You can specify multiple columns in the "column" attribute (separated by commas) for a @Many association.
However, these columns have to be results from the master query and can't be input parameters from the master query (unless you modify the query to return the parameter as a column).

But is there a specific reason you're not using configuration properties to specify the schema, e.g.
FROM("${mySchema}.t_parter");

How to send the input parameters from master query as the parameter is not present in the table.Thats why I cannot return the parameter as a column and I have to use parameter and column both in the @Many and @One association

Passing multiple columns from the master query to the association query doesn't work.

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'CustomerId' not found. Available parameters are []


@SelectProvider(type = CustomerProvider.class, method = "fetchAllById")
@Results({
        @Result(property="accounts",
                column="customerId, accountId",
                javaType=Account.class,
                one = @One(select="com.deeshank.dao.AccountMapper.fetchAccounts",
                        fetchType = FetchType.EAGER))
})
List<Customer> fetchAllById(CustomerQueryRequest queryRequest);

customerId and accountId comes from the select columsn of the master query.
Any help would be much appreciated.

Multiple columns must be specified as follows.

column="{param1=column1,param2=column2, ...}"

Please see this test case as an example.
And please make sure to use 3.4.1 or later because there was a bug prevented this from working (#649).

@harawata Thank you the quick response. I did try the approach you had mentioned like key-value pair but it doesn't work. By the way I am using

  <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

Anyways I got it fixed once i added the 3.4.1 org.mybatis to my pom.xml.

Thank you @harawata

mybatis-spring-boot 1.1.1 depends on MyBatis 3.4.0, so you may need to add mybatis 3.4.1 dependency explicitly (the feature is provided by MyBatis core, not by mybatis-spring-boot).

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.1</version>
</dependency>

if it does not work, please try 1.1.2-SNAPSHOT of mybatis-spring-boot-starter which depends on 3.4.1 by default.
HTH

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hanwooram picture hanwooram  ·  3Comments

FelixFly picture FelixFly  ·  4Comments

sogoagain picture sogoagain  ·  5Comments

FuriousPws002 picture FuriousPws002  ·  6Comments

prlei picture prlei  ·  6Comments