我有以下抽象类:
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 :(得分:1)
尝试#{param.getTitle()}
,否则您可以构建自己的SQL Builder。
@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();
}
}
如果你有多个参数,你可以定义如下。
@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();
}
}