tree的json的生成方法

前段时间工作中需要使用树,这一点,在前台用ext很好实现,只是这个tree的store的这个json在后台拼写的时候有些费了我的脑筋,下面总结一下,我发现很好用的一个方法:

(哦,亲们,这些代码里,数据是从不同数据源获得的,所以在代码里体现了切换数据源的代码,如果你不需要切换数据源,没有必要这个做哦,只需要看sql的组成和返回list就行了。)

sql:
Java代码 复制代码 收藏代码
  1. public List FindComboTreeDataBySubjectId(String subjectId, List<String> paramList,List constantValue) {
  2. //权限表的数据及主题
  3. String pmsSql = reportPermissionService.getPmsSql(constantValue);
  4. Subject pmsSubject = reportPermissionService.getPmsSubject();
  5. Subject subject = this.subjectDomainService.getSubject(subjectId);
  6. if(subject==null||"".equals(subject)){
  7. return new ArrayList();
  8. }
  9. String subTable = subject.getSubTable();
  10. String ds = subject.getDataSource().toString();
  11. // 切换到给定的数据源
  12. HotSwappableTargetSource swapper = (HotSwappableTargetSource)app.getBean("swappableDataSource");
  13. DataSource dataSource = (DataSource)app.getBean(ds);
  14. swapper.swap(dataSource);
  15. // 查询元数据
  16. List result = new ArrayList();
  17. String sql = "SELECT ID AS COMPANYID, ID,PID,NAME,NODETYPE,YEARMONTH FROM "+subTable+" WHERE 1=1 ";
  18. if(pmsSubject !=null && pmsSubject.getDataSource().toString().equals(ds) && pmsSql !=""){
  19. sql = "SELECT T.ID AS COMPANYID,T.ID,T.PID,T.NAME,T.NODETYPE,T.YEARMONTH, DECODE(PMS.ID,NULL,0,1) AS PERMIS FROM "+subTable + " T LEFT JOIN" + pmsSql + " ON T.ID = PMS.ID WHERE 1=1 ";
  20. }
  21. if(paramList != null){
  22. Iterator paramItem = paramList.iterator();
  23. while(paramItem.hasNext()){
  24. String param = paramItem.next().toString();
  25. if(!"".equals(param)||param!="")
  26. sql +=" and "+param+" ";
  27. }
  28. }
  29. sql = "SELECT T.*,LEVEL FROM ("+ sql + ")T start with t.pid is null connect by prior t.id = t.PID order by level,T.NAME desc";
  30. SQLQuery query = getSession().createSQLQuery(sql);
  31. List list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
  32. return list;
  33. }
public List FindComboTreeDataBySubjectId(String subjectId, List<String> paramList,List constantValue) {
		//权限表的数据及主题
		String pmsSql = reportPermissionService.getPmsSql(constantValue);
		Subject pmsSubject = reportPermissionService.getPmsSubject();
		
		Subject subject = this.subjectDomainService.getSubject(subjectId);
		if(subject==null||"".equals(subject)){
			return new ArrayList();
		}
		
		String subTable = subject.getSubTable();
		String ds = subject.getDataSource().toString();
		// 切换到给定的数据源
		HotSwappableTargetSource swapper = (HotSwappableTargetSource)app.getBean("swappableDataSource");
		DataSource dataSource = (DataSource)app.getBean(ds);
		swapper.swap(dataSource);
		// 查询元数据
		List result = new ArrayList();	
		String sql = "SELECT ID AS COMPANYID, ID,PID,NAME,NODETYPE,YEARMONTH FROM "+subTable+" WHERE 1=1 ";
		if(pmsSubject !=null && pmsSubject.getDataSource().toString().equals(ds) && pmsSql !=""){
			sql = "SELECT T.ID AS COMPANYID,T.ID,T.PID,T.NAME,T.NODETYPE,T.YEARMONTH, DECODE(PMS.ID,NULL,0,1) AS PERMIS FROM "+subTable + " T LEFT JOIN" + pmsSql + " ON T.ID = PMS.ID WHERE 1=1 ";
		}
		
		if(paramList != null){
			Iterator paramItem = paramList.iterator();
			while(paramItem.hasNext()){
				String param = paramItem.next().toString();
				if(!"".equals(param)||param!="")
					sql +=" and "+param+" ";
			}
		}
		sql = "SELECT T.*,LEVEL FROM ("+ sql + ")T start with t.pid is null connect by prior t.id = t.PID order by level,T.NAME desc";
		SQLQuery query = getSession().createSQLQuery(sql);
		List list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		return list;

	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值