c4387a5ebd16d725157a6fccb9371f1f72894085.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.iamberry.app.sqlprovider;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Method;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Map;
  7. import org.apache.ibatis.jdbc.SQL;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. /**
  10. *
  11. * @author Sikandar
  12. * @date 2016年1月18日 下午9:34:48
  13. */
  14. public class DMLProvider {
  15. /**
  16. * update
  17. *
  18. * @param map
  19. * @return String
  20. * @throws Exception
  21. */
  22. public String update(Map<String, Object> map) {
  23. try {
  24. final String entityName = getEntityName(map);
  25. final Object object_ = map.get(entityName);
  26. final Class<?> cls = Class.forName(object_.getClass().getName());
  27. SQL sql = new SQL() {
  28. {
  29. Object id = null;
  30. UPDATE(entityName);
  31. for (Method method : cls.getDeclaredMethods()) {
  32. String mName = method.getName();
  33. if (mName.startsWith("get")) {
  34. Object rValue = method.invoke(object_);
  35. if (mName.equalsIgnoreCase("getId")) {
  36. id = rValue;
  37. } else {
  38. if (method.getReturnType() == String.class) {
  39. if (rValue != null)
  40. SET(mName.substring(mName.indexOf("get") + 3).toLowerCase() + " = '" + rValue + "'");
  41. } else if (method.getReturnType() == Date.class) {
  42. SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
  43. if (rValue != null)
  44. SET(mName.substring(mName.indexOf("get") + 3).toLowerCase() + " = '" + format.format(rValue) + "'");
  45. } else {
  46. if (rValue != null)
  47. SET(mName.substring(mName.indexOf("get") + 3).toLowerCase() + "=" + rValue.toString());
  48. }
  49. }
  50. }
  51. }
  52. WHERE("id = " + id);
  53. }
  54. };
  55. return sql.toString();
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. return null;
  59. }
  60. }
  61. /**
  62. * insert
  63. *
  64. * @param map
  65. * @return String
  66. */
  67. public String insert(Map<String, Object> map) {
  68. try {
  69. final String entityName = getEntityName(map);
  70. final Object object_ = map.get(entityName);
  71. final Class<?> cls = Class.forName(object_.getClass().getName());
  72. SQL sql = new SQL() {
  73. {
  74. INSERT_INTO(entityName);
  75. for (Method method : cls.getDeclaredMethods()) {
  76. String mName = method.getName();
  77. if (mName.startsWith("get")) {
  78. Object rValue = method.invoke(object_);
  79. if (method.getReturnType() == String.class) {
  80. if (rValue == null)
  81. VALUES(mName.substring(mName.indexOf("get") + 3).toLowerCase(), "" + rValue + "");
  82. else
  83. VALUES(mName.substring(mName.indexOf("get") + 3).toLowerCase(), "'" + rValue + "'");
  84. } else if (method.getReturnType() == Date.class) {
  85. SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
  86. if (rValue != null)
  87. VALUES(mName.substring(mName.indexOf("get") + 3).toLowerCase(), "'" + format.format(rValue) + "'");
  88. else
  89. VALUES(mName.substring(mName.indexOf("get") + 3).toLowerCase(), "" + null + "");
  90. } else
  91. VALUES(mName.substring(mName.indexOf("get") + 3).toLowerCase(), rValue == null ? "0" : rValue.toString());
  92. }
  93. }
  94. }
  95. };
  96. return sql.toString();
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. return null;
  100. }
  101. }
  102. private String getEntityName(Map<String, Object> map) throws Exception {
  103. String entityName = null;
  104. for (Object object_ : map.values()) {
  105. Class<?> cls = Class.forName(object_.getClass().getName());
  106. for (Annotation annotation : cls.getAnnotations()) {
  107. if (annotation instanceof Qualifier) {
  108. Qualifier myAnnotation = (Qualifier) annotation;
  109. entityName = myAnnotation.value();
  110. }
  111. }
  112. }
  113. if (entityName == null)
  114. throw new Exception(
  115. "@Qualifier not found, Entity should be defined with @Qualifier name, @Qualifier name must match DataBase table name");
  116. return entityName;
  117. }
  118. }