QueryOrder.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.hcks.cmfds.commons.lang;
  2. import java.io.Serializable;
  3. /**
  4. *
  5. * 查询时的顺序
  6. *
  7. * @author:liusha
  8. * @date:Feb 3, 2011 11:12:44 AM
  9. */
  10. public class QueryOrder implements Serializable {
  11. private static final long serialVersionUID = 5291198501266064857L;
  12. /**
  13. * 是否升序
  14. */
  15. private boolean ascending;
  16. /**
  17. * 忽略排序
  18. */
  19. private boolean ignoreCase;
  20. public boolean isIgnoreCase() {
  21. return ignoreCase;
  22. }
  23. public void setIgnoreCase(boolean ignoreCase) {
  24. this.ignoreCase = ignoreCase;
  25. }
  26. /**
  27. * 属性名称
  28. */
  29. private String propertyName;
  30. /**
  31. * 返回字符串
  32. */
  33. public String toString(){
  34. return propertyName + " " +(ascending ? "asc" : "desc");
  35. }
  36. /**
  37. * 带参数的构造方法
  38. * @param propertyName
  39. * @param ascending
  40. */
  41. public QueryOrder(String propertyName,boolean ascending){
  42. this.propertyName = propertyName;
  43. this.ascending = ascending;
  44. }
  45. /**
  46. * 带参数的构造方法
  47. * @param propertyName
  48. * @param ascending
  49. */
  50. public QueryOrder(String propertyName,String ascending){
  51. this.propertyName = propertyName;
  52. if(null !=ascending)
  53. {
  54. if(ascending.equals("asc"))
  55. {
  56. this.ascending = true;
  57. }else if(ascending.equals("desc"))
  58. {
  59. this.ascending = false;
  60. }
  61. }
  62. }
  63. /**
  64. * 静态方法:返回升序的QueryOrder实例
  65. * @param propertyName
  66. * @return
  67. */
  68. public static QueryOrder asc(String propertyName){
  69. return new QueryOrder(propertyName,true);
  70. }
  71. /**
  72. * 静态方法:返回降序的QueryOrder实例
  73. * @param propertyName
  74. * @return
  75. */
  76. public static QueryOrder desc(String propertyName){
  77. return new QueryOrder(propertyName,false);
  78. }
  79. /**
  80. * 忽略排序方法
  81. * @return
  82. */
  83. public QueryOrder ignoreCase(){
  84. ignoreCase = true;
  85. return this;
  86. }
  87. public void appendSqlOrder(StringBuilder sb){
  88. if(sb == null)
  89. sb = new StringBuilder();
  90. if(sb.toString().indexOf("order by") == -1){
  91. sb.append(" order by ");
  92. }
  93. sb.append(" ").append(toString());
  94. }
  95. public void appendSqlOrder(StringBuilder sb,boolean isSplit){
  96. if(sb == null)
  97. sb = new StringBuilder();
  98. if(sb.toString().indexOf("order by") == -1){
  99. sb.append(" order by ");
  100. }
  101. if(isSplit)
  102. {
  103. sb.append(" ").append(toString()).append(",");
  104. }else
  105. {
  106. sb.append(" ").append(toString());
  107. }
  108. }
  109. }