SQL.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2009-2010 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License i distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package net.paoding.rose.jade.annotation;
  17. import java.lang.annotation.Documented;
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Retention;
  20. import java.lang.annotation.RetentionPolicy;
  21. import java.lang.annotation.Target;
  22. /**
  23. * 用{@link SQL}注解标注在Jade DAO方法上,表示这个DAO方法所要执行的SQL语句。
  24. * <p>
  25. *
  26. * Jade把SQL语句分为查询类型和更改类型,Jade进行SQL类型分类的基于两个目的:<br>
  27. * <ul>
  28. * <li>1)因为这两种类型的SQL返回结果不一样,查询类型返回结果是一个结果集, 更新类型的SQL返回结果只是一个数字表示更新的条目;</li>
  29. * <li>2)是为了能够使SQL能够在master- slave的数据库架构中发往正确的目的数据库执行。</li>
  30. * </ul>
  31. * <p>
  32. *
  33. * 简单地,Jade认为所有以SELECT开始在SQL是查询类型的,其他的都是更新类型的。不过当然这种分法非常不合理,
  34. * 比如SHOW语句所代表的就应该是查询类型的,在这种情况下,我们还是希望由开发者您在{@link SQL}
  35. * 指定吧,如果有需要执行一些非SELECT的查询类型的语句的话。
  36. * <p>
  37. *
  38. * 在写SQL时可把SQL参数值直接放到SQL中,如下:<br>
  39. * <span style='margin-left:50px;'>
  40. * <code>@SQL("SELECT id, account, name FROM user WHERE id='12345'")</code>
  41. * </span>
  42. * <p>
  43. * 也可由DAO方法的命名参数指定,因此支持了动态参数,即以冒号开始并紧跟一个名字字符串表示,如下:<br>
  44. * <span style='margin-left:50px;'>
  45. * <code>@SQL("SELECT id, account, name FROM user WHERE id=:userId")<span>
  46. * <br>
  47. * <span style='margin-left:50px;'> public User getUser(@SQLParam("userId") String id);</code><span>
  48. * <p>
  49. * OR<br>
  50. * <span style='margin-left:50px;'>
  51. * <code>@SQL("SELECT id, account, name FROM user where id=:user.id")<span>
  52. * <br>
  53. * <span style='margin-left:50px;'> public User getUser(@SQLParam("user") User user);</code><span> <br>
  54. * <p>
  55. * 在此,我们也示例了{@link SQL}注解所使用语句和标准的SQL的有所区别。为了更加有效地支持编程,此处的SQL具有较为丰富的法,具体请见:
  56. * http://paoding-rose.googlecode.com/....
  57. *
  58. * @author 王志亮 [qieqie.wang@gmail.com]
  59. * @author 廖涵 [in355hz@gmail.com]
  60. */
  61. @Target( { ElementType.METHOD })
  62. @Retention(RetentionPolicy.RUNTIME)
  63. @Documented
  64. public @interface SQL {
  65. /**
  66. *
  67. * @return Jade支持的SQL语句
  68. */
  69. String value();
  70. /**
  71. * 返回该语句的类型,查询类型或变更类型。
  72. * 默认Jade认为只有以SELECT开始的才是查询类型,其他的为变更类型。开发者通过这个属性用来变更Jade默认的处理!
  73. *
  74. * @return 查询类型
  75. */
  76. SQLType type() default SQLType.AUTO_DETECT;
  77. }