Java reflection method using dynamic invocation, data generated grid

Java project need and assembled foreground grid background check data, the number of rows of data variable, define the variable data rows, beginning with the most primitive way, write a few hundred lines, that is, what fields need stitching background foreground what field, java code is redundant margin is very large, and not flexible enough, as soon as the order of fields need to adjust the front page or add a field, remove a field, the background need to modify the java code. Reconstruction efforts after a morning, arranged to database fields and fields of the corresponding method, java object method calls using reflection to acquire a dynamic field value, transmitted to the reception list of the splice. After such modification, the code clean, very flexible, as long as the method is present in an object, any configuration can be displayed or not in the database, the display order and the like, the implementation process is now introduced:

First, the database definition

 
TABLE `IS_WEB_TABLECONF` the CREATE (
  ` guid` BIGINT (20 is) the NOT NULL,
  `columnname` VARCHAR (255) the CHARACTER UTF8 the COLLATE utf8_general_ci the DEFAULT the SET NULL, - display of fields in the page name
  ` language` varchar (255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, - reflection method field
  `columnmethodname` VARCHAR (255) the CHARACTER UTF8 the COLLATE utf8_general_ci the DEFAULT the SET NULL,
  ` orderstr` int (10) the DEFAULT NULL, - display of fields in the page order

  `tablename` varchar (255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, - form name
  `modulename` varchar (255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL, - the module name
  a PRIMARY KEY (` guid`) the USING BTREE
) the InnoDB the CHARACTER the SET ENGINE = = = UTF8 the COLLATE utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of IS_WEB_TABLECONF
-- ----------------------------
INSERT INTO `IS_WEB_TABLECONF` VALUES (1, '公司名称:', 'zh-ch', 'getBranchName', 1, b'0', 23, '2019-09-18 10:44:32', '2019-09-18 10:44:30', 12, 'WebInstallation', 'BaseInfo');
INSERT INTO `IS_WEB_TABLECONF` VALUES (2, '初始分公司:', 'zh-ch', 'getProtocolStartDate', 2, b'0', 345, '2019-09-18 10:45:48', '2019-09-18 10:45:50', 4, 'WebInstallation', 'BaseInfo');
 
Two, java defined, depending on the data assembling module queries the corresponding table
 
void getListInfo Private (String licenseClassName, List qualifyList, Object DTO, String qualifyInfo) { 
// licenseClassName need reflecting object class path name, such as:
"com.neuxa.is.isinterface.fm.dto.IsSlInstallationunitReviewDto"
//dto是查询好带有数据信息的对象实体

List<IsWebTableconfDto> tableConfQua = dictionaryUtil.getTableConf("WebInstallation", qualifyInfo);
for (IsWebTableconfDto confDto : tableConfQua) {
JSONObject object = new JSONObject();
object.put("key", confDto.getColumnname());
String value = "";
value = getInvokeMethodValue(licenseClassName, confDto.getColumnmethodname(), dto);
object.put("value", value);
qualifyList.add(object);
}

private String getInvokeMethodValue(String classBeanName,String methodName,Object curDto){
String value="";
try {
Class<?> clazz = Class.forName(classBeanName);
Method method = clazz.getDeclaredMethod(methodName);
method.setAccessible(true);
Object invoke = method.invoke(curDto);
value=invoke.toString();
}catch(Exception e){
e.printStackTrace();
}
return value;
}

Guess you like

Origin www.cnblogs.com/DylanZ/p/11541557.html