web-dev-qa-db-ja.com

MyBatisでアノテーション付きの動的SQLクエリを使用する方法(selectProviderの使用方法)?

Mybatis3でマッパーを定義するための追加のxmlを避けようとしています。注釈はぴったりです。

@ SelectProvider/@ InsertProvider/etcの使用法に少し混乱しています。これを通して私を導く多くのリソースがオンラインにあるとは思わないでください。

基本的に、mybatis3で代替の注釈バージョンを見つけたいと思います。

たとえば、xmlマッパーがあり、アノテーションを使用するように変換したい

<select ...>
  <where>
    <if cause.....>
    </if>
    <if cause......>
    </if>
  </where>
</select>

誰かがコードを含む具体的な答え/解決策を提供できますか?

前もって感謝します!

17
ligerdave

別の解決策は次のとおりです。

追加 <script> @ annotationの先頭

@Update("<script>
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</script>")

さらに、プロジェクトで.groovyを.classにコンパイルするため、上記のように@annotationでSQLを記述できます。

22
farmer1992
  1. マッパーインターフェイス:

    @SelectProvider(type=MyClass.class, method="myMethod")
    public Object selectById(int id);
    
  2. 私のクラスで:

    public static String myMethod() {
        return "select * from MyTable where id=#{id}"; 
    }
    
8
acala