0

I have the following abstract class:

public AbstractClass{
   public abstract String getCode();
}

And the following concrete class:

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

Now, I'd like for the output of that class to be a parameter for a mybatis query:

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

is it possible? I know that mybatis allows to bind the output of a method to a variable through the bind annotation:

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

but here I'm using annotations and I cannot find any @Bind one...

Phate
  • 6,066
  • 15
  • 73
  • 138

1 Answers1

1

Try #{param.getTitle()} or else you can build your Own 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();
  }
}

If you have mutilple parameters you can define as follows.

@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();
  }
}
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68