-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Passing multiple parameters on @Many annotation #230
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
Comments
You can specify multiple columns in the "column" attribute (separated by commas) for a @many association. But is there a specific reason you're not using configuration properties to specify the schema, e.g. |
Passing multiple columns from the master query to the association query doesn't work.
customerId and accountId comes from the select columsn of the master query. |
Multiple columns must be specified as follows. column="{param1=column1,param2=column2, ...}" Please see this test case as an example. |
@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
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. |
You can pass the parameters to the final selection in the select block as constants and then pass them to the desired method, which is used for the one/many association My example: @Mapper
public interface ResolutionStatMBRepository {
@Select({"<script>",
"SELECT",
" organization_id,",
" tenant_id,",
" territory_id,",
// others columns
" #{start}::timestamp as start,", // <-- set param as constants
" #{end}::timestamp as end ", // <-- set param as constants
"FROM resolution_stat ",
"WHERE ",
" id IS NOT NULL",
// other where conditions
" AND order_date BETWEEN #{start} AND #{end}",
"</script>"})
@Results({
// other mapping
@Result(property = "services", column = "{organizationId=organization_id, tenantId=tenant_id, territoryId=territory_id, start=start, end=end}", // specify the list of passed params
javaType = List.class, many = @Many(select = "getRegisteredServices"))
})
@Options(resultSetType = ResultSetType.DEFAULT)
List<ResolutionStatResponseDto> findAllBetweenOrder(UUID organizationId,
String tenantId,
LocalDate start,
LocalDate end,
EnumSet<OrderTypeEnum> orderTypeEnums);
@Select({"<script>",
"SELECT",
" organization_id as organizationId,",
" territory_id as territoryId,",
// other columns
" income ",
"FROM registered_service ",
"WHERE ",
" id IS NOT NULL",
" <if test='tenantId != null'>and tenant_id = #{tenantId}</if>",
" AND organization_id = #{organizationId} ",
" AND territory_id = #{territoryId} ",
" AND order_date BETWEEN #{start} AND #{end}",
"</script>"})
List<RegisteredServiceResponseDto> getRegisteredServices(UUID organizationId,
String tenantId,
UUID territoryId,
LocalDate start,
LocalDate end);
} |
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<String, Object> 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;
}
In My Test:
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)
The text was updated successfully, but these errors were encountered: