1. /**
  2. * Copyright: Copyright (c) 2005-2005
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.javaresearch.jerch;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. /**
  9. * 部分根据字段的名称进行自动匹配的Mappable实现。
  10. * 需要指定的是例外部分字段的相应方法和类型。
  11. * 最后更新日期:2005年3月28日
  12. * @author cherami
  13. */
  14. public class PartNameMatchMappable implements Mappable {
  15. private Map expectField;
  16. /**
  17. * 构造一个例外字段为空的PartNameMatchMappable实例。
  18. *
  19. */
  20. public PartNameMatchMappable() {
  21. this.expectField = new HashMap();
  22. }
  23. /**
  24. * 以指定参数构造一个PartNameMatchMappable实例。
  25. * @param expectField 例外的字段映射,主键为字段名,值为对象数组,第一个元素为方法名,第二个元素为类型。
  26. */
  27. public PartNameMatchMappable(Map expectField) {
  28. this.expectField = expectField;
  29. }
  30. /**
  31. * 设置字段对应的方法名和类型。
  32. * @param fieldName 数据库字段名
  33. * @param methodName 对应的方法
  34. * @param type 类型
  35. */
  36. public void setMapField(String fieldName,String methodName,Class type) {
  37. Object[] map={methodName,type};
  38. expectField.put(fieldName,map);
  39. }
  40. /**
  41. * 得到字段对应的填充方法的方法名。
  42. * @param fieldName 数据库表的字段名
  43. * @return 如果例外映射中有对应的方法则返回该方法,否则前面加set,将字段名首字母大写。
  44. */
  45. public String getMapMethod(String fieldName) {
  46. Object[] result = (Object[]) expectField.get(fieldName);
  47. if (result != null) {
  48. return (String) result[0];
  49. } else {
  50. return "set" + Character.toUpperCase(fieldName.charAt(0))
  51. + fieldName.substring(1);
  52. }
  53. }
  54. /**
  55. * 得到字段对应的填充方法的参数类型。
  56. * @see SQLJavaTypeConverter#getJavaType(int sqlType) SQLJavaTypeConverter.getJavaType
  57. * @param fieldName 数据库表的字段名
  58. * @param dbType 数据库返回的类型常量定义
  59. * @return 如果例外映射中有对应的类型则返回该类型,否则返回数据库的返回类型对应的Java类型
  60. */
  61. public Class getMethodParameterType(String fieldName, int dbType) {
  62. Object[] result = (Object[]) expectField.get(fieldName);
  63. if (result != null) {
  64. return (Class) result[1];
  65. } else {
  66. return SQLJavaTypeConverter.getJavaType(dbType);
  67. }
  68. }
  69. }