Skip to content

Commit 5cf6136

Browse files
committed
Merge pull request #642 from kazuki43zoo/update-doc-for-sqlprovider
Update doc for support multiple arguments at sql provider method (related with #639)
2 parents 683c9fe + 494c02e commit 5cf6136

File tree

5 files changed

+252
-21
lines changed

5 files changed

+252
-21
lines changed

src/site/es/xdoc/java-api.xml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ try (SqlSession session = sqlSessionFactory.openSession()) {
436436
<li><code>&lt;select&gt;</code></li>
437437
</ul>
438438
</td>
439-
<td>Estas anotaciones SQL alternativas te permiten especificar un nombre de clases y un método que devolverán la SQL que debe ejecutarse. Cuando se ejecute el método MyBatis instanciará la clase y ejecutará el método especificados en el provider. El método puede opcionalmente recibir el objeto parámetro como su único parámetro o no recibir ningún parámetro. Atributos: type, method. El atributo type es el nombre completamente cualificado de una clase. El method es el nombre un método de dicha clase. Nota: A continuación hay una sección sobre la clase SelectBuilder, que puede ayudar a construir SQL dinámico de una forma más clara y sencilla de leer.</td>
439+
<td>Estas anotaciones SQL alternativas te permiten especificar un nombre de clases y un método que devolverán la SQL que debe ejecutarse. Cuando se ejecute el método MyBatis instanciará la clase y ejecutará el método especificados en el provider. El método puede opcionalmente recibir el objeto parámetro.(In MyBatis 3.4 or later, it's allow multiple parameters) Atributos: type, method. El atributo type es el nombre completamente cualificado de una clase. El method es el nombre un método de dicha clase. Nota: A continuación hay una sección sobre la clase, que puede ayudar a construir SQL dinámico de una forma más clara y sencilla de leer.</td>
440440
</tr>
441441
<tr>
442442
<td><code>@Param</code></td>
@@ -508,6 +508,53 @@ User getUserById(Integer id);
508508
})
509509
@Select("select * from company where id = #{id}")
510510
Company getCompanyById(Integer id);</source>
511+
512+
<p>This example shows solo parameter using the Sql Provider annotation:</p>
513+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
514+
List<User> getUsersByName(String name);
515+
516+
class UserSqlBuilder {
517+
public String buildGetUsersByName(final String name) {
518+
return new SQL(){{
519+
SELECT("*");
520+
FROM("users");
521+
if (name != null) {
522+
WHERE("name like #{value} || '%'");
523+
}
524+
ORDER_BY("id");
525+
}}.toString();
526+
}
527+
}]]></source>
528+
529+
<p>This example shows multiple parameters using the Sql Provider annotation:</p>
530+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
531+
List<User> getUsersByName(
532+
@Param("name") String name, @Param("orderByColumn") String orderByColumn);
533+
534+
class UserSqlBuilder {
535+
536+
// If not use @Param, you should be define same arguments with mapper method
537+
public String buildGetUsersByName(
538+
final String name, final String orderByColumn) {
539+
return new SQL(){{
540+
SELECT("*");
541+
FROM("users");
542+
WHERE("name like #{name} || '%'");
543+
ORDER_BY(orderByColumn);
544+
}}.toString();
545+
}
546+
547+
// If use @Param, you can define only arguments to be used
548+
public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
549+
return new SQL(){{
550+
SELECT("*");
551+
FROM("users");
552+
WHERE("name like #{name} || '%'");
553+
ORDER_BY(orderByColumn);
554+
}}.toString();
555+
}
556+
}]]></source>
557+
511558
</subsection>
512559

513560
</section>

src/site/ja/xdoc/java-api.xml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ try (SqlSession session = sqlSessionFactory.openSession()) {
456456
</ul>
457457
</td>
458458
<td>これらのアノテーションは動的 SQL を生成するためのものです。実行時に指定されたメソッドが呼び出され、メソッドから返された SQL ステートメントが実行されます。マップドステートメントを実行する際、プロバイダーによって指定したクラスのインスタンスが作成され、指定されたメソッドが実行されます。
459-
<u>メソッドに引数オブジェクトを渡すこともできますが、指定できる引数は一つだけです(または引数なし)。</u>
460-
キー: <code>type</code>, <code>method</code>. type は完全修飾クラス名、method はそのクラスのメソッド名です。 <span class="label important">NOTE</span> 次の章で、クリーンで可読性の高いコードで動的 SQL を構築するための SelectBuilder クラスについて説明します
459+
メソッドに引数を渡すことができます。(MyBatis 3.4以降では、複数の引数を渡すことができます)
460+
キー: <code>type</code>, <code>method</code>. <code>type</code> にはクラスオブジェクト、<code>method</code> にはメソッド名を指定します。 <span class="label important">NOTE</span> 次の章で、クリーンで可読性の高いコードで動的 SQL を構築するためのクラスについて説明します
461461
</td>
462462
</tr>
463463
<tr>
@@ -526,6 +526,53 @@ User getUserById(Integer id);
526526
})
527527
@Select("select * from company where id = #{id}")
528528
Company getCompanyById(Integer id);</source>
529+
530+
<p>次のコードは SQLプロバイダー用のアノテーションを使用して、パラメータをひとつ受け取る例です。</p>
531+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
532+
List<User> getUsersByName(String name);
533+
534+
class UserSqlBuilder {
535+
public String buildGetUsersByName(final String name) {
536+
return new SQL(){{
537+
SELECT("*");
538+
FROM("users");
539+
if (name != null) {
540+
WHERE("name like #{value} || '%'");
541+
}
542+
ORDER_BY("id");
543+
}}.toString();
544+
}
545+
}]]></source>
546+
547+
<p>次のコードは SQLプロバイダー用のアノテーションを使用して、複数パラメータをひとつ受け取る例です。</p>
548+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
549+
List<User> getUsersByName(
550+
@Param("name") String name, @Param("orderByColumn") String orderByColumn);
551+
552+
class UserSqlBuilder {
553+
554+
// @Paramを使わない場合は, Mapperメソッドと同じ引数で定義する必要があります。
555+
public String buildGetUsersByName(
556+
final String name, final String orderByColumn) {
557+
return new SQL(){{
558+
SELECT("*");
559+
FROM("users");
560+
WHERE("name like #{name} || '%'");
561+
ORDER_BY(orderByColumn);
562+
}}.toString();
563+
}
564+
565+
// @Paramを使う場合は, 必要な引数のみ定義することができます。
566+
public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
567+
return new SQL(){{
568+
SELECT("*");
569+
FROM("users");
570+
WHERE("name like #{name} || '%'");
571+
ORDER_BY(orderByColumn);
572+
}}.toString();
573+
}
574+
}]]></source>
575+
529576
</subsection>
530577

531578
</section>

src/site/ko/xdoc/java-api.xml

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,13 @@ try (SqlSession session = sqlSessionFactory.openSession()) {
571571
<li><code>&lt;select&gt;</code></li>
572572
</ul>
573573
</td>
574-
<td>실행시 SQL 을 리턴할 클래스명과 메소드명을 명시하도록 해주는 대체수단의 애노테이션이다.
574+
<td>실행시 SQL 을 리턴할 클래스 과 메소드명을 명시하도록 해주는 대체수단의 애노테이션이다.
575575
매핑된 구문을 실행할 때 마이바티스는 클래스의 인스턴스를 만들고 메소드를 실행한다.
576-
메소드는 파라미터 객체를 받을 수도 있다.
576+
메소드는 파라미터 객체를 받을 수도 있다.(In MyBatis 3.4 or later, it's allow multiple parameters)
577577
사용가능한 속성들 : type, method.
578-
type 속성은 클래스의 패키지 경로를 포함한 전체 이름이다.
579-
메소드는 클래스의 메소드명이다.
580-
Note: 이 섹션은 SelectBuilder 클래스에 대한 설명으로 동적 SQL 을 좀더 깔끔하고 읽기 쉽게 만드는데 도움이 될 수 있다.</td>
578+
type 속성은 클래스.
579+
method 속성은 메소드명이다.
580+
Note: 이 섹션은 클래스에 대한 설명으로 동적 SQL 을 좀더 깔끔하고 읽기 쉽게 만드는데 도움이 될 수 있다.</td>
581581
</tr>
582582
<tr>
583583
<td><code>@Param</code></td>
@@ -662,6 +662,53 @@ User getUserById(Integer id);
662662
})
663663
@Select("select * from company where id = #{id}")
664664
Company getCompanyById(Integer id);</source>
665+
666+
<p>This example shows solo parameter using the Sql Provider annotation:</p>
667+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
668+
List<User> getUsersByName(String name);
669+
670+
class UserSqlBuilder {
671+
public String buildGetUsersByName(final String name) {
672+
return new SQL(){{
673+
SELECT("*");
674+
FROM("users");
675+
if (name != null) {
676+
WHERE("name like #{value} || '%'");
677+
}
678+
ORDER_BY("id");
679+
}}.toString();
680+
}
681+
}]]></source>
682+
683+
<p>This example shows multiple parameters using the Sql Provider annotation:</p>
684+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
685+
List<User> getUsersByName(
686+
@Param("name") String name, @Param("orderByColumn") String orderByColumn);
687+
688+
class UserSqlBuilder {
689+
690+
// If not use @Param, you should be define same arguments with mapper method
691+
public String buildGetUsersByName(
692+
final String name, final String orderByColumn) {
693+
return new SQL(){{
694+
SELECT("*");
695+
FROM("users");
696+
WHERE("name like #{name} || '%'");
697+
ORDER_BY(orderByColumn);
698+
}}.toString();
699+
}
700+
701+
// If use @Param, you can define only arguments to be used
702+
public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
703+
return new SQL(){{
704+
SELECT("*");
705+
FROM("users");
706+
WHERE("name like #{name} || '%'");
707+
ORDER_BY(orderByColumn);
708+
}}.toString();
709+
}
710+
}]]></source>
711+
665712
</subsection>
666713

667714
</section>

src/site/xdoc/java-api.xml

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,13 @@ try (SqlSession session = sqlSessionFactory.openSession()) {
483483
<li><code>&lt;select&gt;</code></li>
484484
</ul>
485485
</td>
486-
<td>Allows for creation of dynamic SQL. These alternative SQL annotations allow you to specify a class name and
487-
a method that will return the SQL to run at execution time. Upon executing the mapped statement, MyBatis will
486+
<td>Allows for creation of dynamic SQL. These alternative SQL annotations allow you to specify a class and
487+
a method name that will return the SQL to run at execution time. Upon executing the mapped statement, MyBatis will
488488
instantiate the class, and execute the method, as specified by the provider.
489-
<u>The method can optionally accept the parameter object as its sole parameter, but must only specify that parameter, or no parameters</u>.
490-
Attributes: <code>type</code>, <code>method</code>. The type attribute is the fully qualified name of a class.
491-
The method is the name of the method on that class. <span class="label important">NOTE</span>
492-
Following this section is a discussion about the SelectBuilder class, which can help build dynamic SQL in a cleaner, easier to read way.</td>
489+
The method can optionally accept parameter objects.(In MyBatis 3.4 or later, it's allow multiple parameters)
490+
Attributes: <code>type</code>, <code>method</code>. The <code>type</code> attribute is a class.
491+
The <code>method</code> is the name of the method on that class. <span class="label important">NOTE</span>
492+
Following this section is a discussion about the class, which can help build dynamic SQL in a cleaner, easier to read way.</td>
493493
</tr>
494494
<tr>
495495
<td><code>@Param</code></td>
@@ -575,6 +575,52 @@ User getUserById(Integer id);
575575
})
576576
@Select("select * from company where id = #{id}")
577577
Company getCompanyById(Integer id);</source>
578+
579+
<p>This example shows solo parameter using the Sql Provider annotation:</p>
580+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
581+
List<User> getUsersByName(String name);
582+
583+
class UserSqlBuilder {
584+
public String buildGetUsersByName(final String name) {
585+
return new SQL(){{
586+
SELECT("*");
587+
FROM("users");
588+
if (name != null) {
589+
WHERE("name like #{value} || '%'");
590+
}
591+
ORDER_BY("id");
592+
}}.toString();
593+
}
594+
}]]></source>
595+
596+
<p>This example shows multiple parameters using the Sql Provider annotation:</p>
597+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
598+
List<User> getUsersByName(
599+
@Param("name") String name, @Param("orderByColumn") String orderByColumn);
600+
601+
class UserSqlBuilder {
602+
603+
// If not use @Param, you should be define same arguments with mapper method
604+
public String buildGetUsersByName(
605+
final String name, final String orderByColumn) {
606+
return new SQL(){{
607+
SELECT("*");
608+
FROM("users");
609+
WHERE("name like #{name} || '%'");
610+
ORDER_BY(orderByColumn);
611+
}}.toString();
612+
}
613+
614+
// If use @Param, you can define only arguments to be used
615+
public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
616+
return new SQL(){{
617+
SELECT("*");
618+
FROM("users");
619+
WHERE("name like #{name} || '%'");
620+
ORDER_BY(orderByColumn);
621+
}}.toString();
622+
}
623+
}]]></source>
578624
</subsection>
579625

580626
</section>

src/site/zh/xdoc/java-api.xml

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,11 @@ resultSets=””。
748748
基于执行的映射语句,
749749
MyBatis
750750
会实例化这个类,然后执行由 provider
751-
指定的方法. 这个方法可以选择性的接
752-
受参数对象作为它的唯一参数,
753-
但是必
754-
须只指定该参数或者没有参数。属性:
755-
type,method。type 属性是类的完全限
756-
定名。method 是该类中的那个方法名。
751+
指定的方法.
752+
该方法可以有选择地接受参数对象.(In MyBatis 3.4 or later, it's allow multiple parameters)
753+
属性: type,method。type 属性是类。method 属性是方法名。
757754
注意:
758-
这节之后是对 SelectBuilder 类的
755+
这节之后是对 类的
759756
讨论,它可以帮助你以干净,容于阅读
760757
的方式来构建动态 SQL。
761758
</td>
@@ -848,6 +845,53 @@ User getUserById(Integer id);
848845
})
849846
@Select("select * from company where id = #{id}")
850847
Company getCompanyById(Integer id);</source>
848+
849+
<p>This example shows solo parameter using the Sql Provider annotation:</p>
850+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
851+
List<User> getUsersByName(String name);
852+
853+
class UserSqlBuilder {
854+
public String buildGetUsersByName(final String name) {
855+
return new SQL(){{
856+
SELECT("*");
857+
FROM("users");
858+
if (name != null) {
859+
WHERE("name like #{value} || '%'");
860+
}
861+
ORDER_BY("id");
862+
}}.toString();
863+
}
864+
}]]></source>
865+
866+
<p>This example shows multiple parameters using the Sql Provider annotation:</p>
867+
<source><![CDATA[@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
868+
List<User> getUsersByName(
869+
@Param("name") String name, @Param("orderByColumn") String orderByColumn);
870+
871+
class UserSqlBuilder {
872+
873+
// If not use @Param, you should be define same arguments with mapper method
874+
public String buildGetUsersByName(
875+
final String name, final String orderByColumn) {
876+
return new SQL(){{
877+
SELECT("*");
878+
FROM("users");
879+
WHERE("name like #{name} || '%'");
880+
ORDER_BY(orderByColumn);
881+
}}.toString();
882+
}
883+
884+
// If use @Param, you can define only arguments to be used
885+
public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
886+
return new SQL(){{
887+
SELECT("*");
888+
FROM("users");
889+
WHERE("name like #{name} || '%'");
890+
ORDER_BY(orderByColumn);
891+
}}.toString();
892+
}
893+
}]]></source>
894+
851895
</subsection>
852896

853897
</section>

0 commit comments

Comments
 (0)