前段时间工作中需要使用树,这一点,在前台用ext很好实现,只是这个tree的store的这个json在后台拼写的时候有些费了我的脑筋,下面总结一下,我发现很好用的一个方法:
(哦,亲们,这些代码里,数据是从不同数据源获得的,所以在代码里体现了切换数据源的代码,如果你不需要切换数据源,没有必要这个做哦,只需要看sql的组成和返回list就行了。)
sql:
(哦,亲们,这些代码里,数据是从不同数据源获得的,所以在代码里体现了切换数据源的代码,如果你不需要切换数据源,没有必要这个做哦,只需要看sql的组成和返回list就行了。)
sql:
- 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;
- }
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;
}