2016-06-03 2 views
0

나는 다음과 같은 추상 클래스가 있습니다주석을 사용하여 bind를 사용할 수 있습니까?

public AbstractClass{ 
    public abstract String getCode(); 
} 

그리고 다음과 같은 구체적인 클래스 : 이제

public ConcreteClass extends AbstractClass{ 
    public String getCode(){ 
     return "EHY"; 
    } 
} 

을, 나는 MyBatis로 쿼리에 대한 매개 변수로 클래스의 출력 싶습니다 :

@Select(value="select* from Table where code= #{what here?}" 
public List<Something> getFromTable(ConcreteClass param); 

가능합니까? 나는 MyBatis로 바인드 주석을 통해 변수에 메소드의 출력을 결합 할 수 있습니다 알고 ...

<select id="selectBlogsLike" resultType="Blog"> 
    <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" /> 
    SELECT * FROM BLOG 
    WHERE title LIKE #{pattern} 
</select> 

하지만 여기에 내가 주석을 사용하고 있는데 내가 어떤 @Bind 하나를 찾을 수 없습니다

+0

일반적으로 EL이 속성을 지원하기 때문에 # {param.getTitle()} (또는'# {param.title}')을 사용해 보셨습니까? – chrylis

+0

# {코드}을 사용해보세요. – andy

+0

이 흥미로운 링크에서 https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions 그들은 SpEL을 사용하여'와 같은 것을 할 수있다 : # {# param.code}' – RubioRic

답변

1

시도 #{param.getTitle()} 또는 다른 SQL 빌더를 구축 할 수 있습니다.

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") 
List<User> getUsersByName(String name); 

class UserSqlBuilder { 
    public String buildGetUsersByName(final String name) { 
    return new SQL(){{ 
     SELECT("*"); 
     FROM("users"); 
     if (name != null) { 
     WHERE("name like #{value} || '%'"); 
     } 
     ORDER_BY("id"); 
    }}.toString(); 
    } 
} 

mutilple 매개 변수가있는 경우 다음과 같이 정의 할 수 있습니다.

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") 
List<User> getUsersByName(
    @Param("name") String name, @Param("orderByColumn") String orderByColumn); 

class UserSqlBuilder { 

    // If not use @Param, you should be define same arguments with mapper method 
    public String buildGetUsersByName(
     final String name, final String orderByColumn) { 
    return new SQL(){{ 
     SELECT("*"); 
     FROM("users"); 
     WHERE("name like #{name} || '%'"); 
     ORDER_BY(orderByColumn); 
    }}.toString(); 
    } 

    // If use @Param, you can define only arguments to be used 
    public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) { 
    return new SQL(){{ 
     SELECT("*"); 
     FROM("users"); 
     WHERE("name like #{name} || '%'"); 
     ORDER_BY(orderByColumn); 
    }}.toString(); 
    } 
}