jeecg单页面多表字段显示

jeecg单页面多表字段显示


在使用jeecg过程中,有时候需要页面不仅仅显示单表数据,还需要显示多个表中不同字段的数据,而jeecg仅支持单表显示,下面本文自行编写了一个工具类,来实现页面显示多张表不同字段的内容;
代码:
jeecg单页面多表字段显示_第1张图片

package com.jeecg.demo.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Popup列内容数据绑定助手
 */
@Component
public class PopupDataBindUtil {
	@Autowired
	private SystemService systemService;

	/**
	 * 根据参数绑定数据表数据 例: bindUtil.BindDDName(dataGrid, "trader", "fullname",
	 * "traderid", "orgid", "orgidname");
	 * 搜索名字叫Trader表下的fullname字段,条件是表的traderid等于数据对象的orgid值, 最后绑定到数据的列名为orgidname
	 * 
	 * @param dataGrid
	 *            数据对象
	 * @param tableName
	 *            数据表名称
	 * @param fieldName
	 *            数据表字段
	 * @param queryName
	 *            数据表查询条件字段
	 * @param keyName
	 *            要绑定的数据对象字段名称
	 * @param bindName
	 *            绑定到数据对象的字段名称
	 * 
	 * @throws Exception
	 */
	public void BindDDName(DataGrid dataGrid, String tableName,
			String fieldName, String queryName, String keyName, String bindName)
			throws Exception {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();

		boolean isMap = false;

		if (dataGrid.getResults().size() > 0
				&& "java.util.HashMap".equals(dataGrid.getResults().get(0)
						.getClass().getName())) {
			isMap = true;
		}

		for (Object item : dataGrid.getResults()) {
			Map<String, Object> itemMap = null;

			if (isMap) {
				itemMap = (Map<String, Object>) item;
			} else {
				itemMap = objectToMap(item);
			}

			if (itemMap != null) {

				if (itemMap.get(keyName) != null) {
					List<String> results = systemService
							.findListbySql("select " + fieldName + " from "
									+ tableName + " where " + queryName
									+ " in (" + itemMap.get(keyName).toString()
									+ ")");

					if (results != null && !results.toString().equals("")) {
						itemMap.put(
								bindName,
								results.toString().replace("[", "")
										.replace("]", ""));
					} else {
						itemMap.put(bindName, "");
					}
				}

				resultList.add(itemMap);
			}
		}

		dataGrid.setResults(resultList);
	}

	public Map<String, Object> BindDDName(Object obj, String tableName,
			String fieldName, String queryName, String keyName, String bindName)
			throws Exception {
		Map<String, Object> itemc = objectToMap(obj);

		if (keyName != null) {
			List<String> aaa = systemService.findListbySql("select "
					+ fieldName + " from " + tableName + " where " + queryName
					+ " in (" + itemc.get(keyName).toString() + ")");

			if (aaa != null) {
				itemc.put(bindName,
						aaa.toString().replace("[", "").replace("]", ""));
			}
		}

		return itemc;

	}

	/**
	 * 将数据对象转换为Map
	 * 
	 * @param obj
	 *            数据对象
	 * @return
	 * @throws Exception
	 */
	private Map<String, Object> objectToMap(Object obj) throws Exception {
		if (obj == null)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();

		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo
				.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
			String key = property.getName();
			if (key.compareToIgnoreCase("class") == 0) {
				continue;
			}
			Method getter = property.getReadMethod();
			Object value = getter != null ? getter.invoke(obj) : null;
			map.put(key, value);
		}

		return map;
	}

	/**
	 * 将ids字符串转换为Integer数组用于查询
	 * 
	 * @param ids
	 * @return
	 */
	public Integer[] idsToIntegerArray(String ids) {
		List<Integer> listIds = new ArrayList<Integer>();
		String[] idsArray = ids.split(",");
		for (String item : idsArray) {
			if (null != item && !"".equals(item)) {
				listIds.add(Integer.valueOf(item));
			}
		}

		return listIds.toArray(new Integer[listIds.size()]);
	}
}

使用实例:

@Controller
@RequestMapping("/shopsController")
public class ShopsController extends BaseController {
	private static final Logger logger = LoggerFactory
			.getLogger(ShopsController.class);

	@Autowired
	private ShopsServiceI shopsService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private PopupDataBindUtil bindUtil;
	@Autowired
	private SysOrderVerifyServiceI sysOrderVerifyService;

	/**
	 * SHOPS列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request, String type) {
		request.setAttribute("type", type);
		return new ModelAndView("com/jeecg/demo/shopsList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 * @throws Exception
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ShopsEntity shops, HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid, String type)
			throws Exception {
		CriteriaQuery cq = new CriteriaQuery(ShopsEntity.class, dataGrid);
		if ("sh".equals(type)) {
			cq.eq("shopstate", "1");
			cq.add();
		}
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				shops, request.getParameterMap());
		try {
			// 自定义追加查询条件
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.shopsService.getDataGridReturn(cq, true);

		// 绑定非字典表数据
		bindUtil.BindDDName(dataGrid, "trader", "fullname", "traderid",
				"traderid", "fullname");

		TagUtil.datagrid(response, dataGrid);
	}

原理:DateGrid是一个HashMap,将其截获,作为对象处理,向其中插入所需字段后,在前台获取,缺点是每一个字段都需要执行一次方法,在数据量大时,非常影响性能。

你可能感兴趣的:(日常问题)