分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

基于zTree的Jsp通用动态级联树结构

发布时间:2023-09-06 01:39责任编辑:林大明关键词:暂无标签
1、categoryTree.html 通用页面

<linkrel="stylesheet"href="/static/plugins/zTree_v3/css/zTreeStyle.css"type="text/css"><style>/*展示树状结构的样式*/ul.ztree{position:relative;top:32px;left:0px;z-index:999;border:1pxsolid#617775;background:#f0f7fb;width:220px;height:360px;overflow-y:scroll;overflow-x:auto;display:none}</style><scripttype="text/javascript"src="/static/plugins/zTree_v3/js/jquery-1.4.4.min.js"></script><scripttype="text/javascript"src="/static/plugins/zTree_v3/js/jquery.ztree.core.js"></script><SCRIPTtype="text/javascript">varnode_id,node_name;varzTreeObj,setting={view:{selectedMulti:false//多选选项},data:{//定义数据规范simpleData:{enable:false,//是否使用简单的数据结构,本例使用标准json树状结构idKey:'cat_id',//nodeId字段名称pIdKey:'parent_id',//parentId字段名称rootPId:0//跟nodeId的值},key:{//字段名称定义name:"cat_name",//展示名称children:"nodes"//子节点对象}}},zTreeNodes=[];//加载动态数据functionloadTree(data){if(data&&data.length>0){zTreeNodes=data;zTreeObj=$.fn.zTree.init($("#tree"),setting,zTreeNodes);}}$(document).ready(function(){$.post("/category/getCategoryTree",null,loadTree);});</SCRIPT><ulid="tree"class="ztree"></ul>

2、调用方法

<divclass="t_textml10"id="category"><c:importurl="../common/categoryTree.jsp"/><!--如果是静态页面可以使用jquery.load()方法--><inputtype="hidden"name="cat_id"id="cat_id"value="${pageParam.cat_id}"/><inputplaceholder="类别"type="text"name="cat_name"id="cat_name"/></div>

3.controller

importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importjava.util.List;/***@authorliujun*@since2018/1/18.*产品分类*/@Controller@RequestMapping("/category")publicclassCategoryController{Loggerlogger=LoggerFactory.getLogger(CategoryController.class);@AutowiredCategoryTreeServicecategoryService;@RequestMapping(value="/getCategoryTree")@ResponseBodypublicList<CategoryTree>getCategoryTree(){logger.info("处理请求:/category/getCategoryTree");returnthis.categoryService.getCategoryTree();}}

4、service接口

importjava.util.List;/***@authorliujun*@since2018/1/18.*产品类别服务接口*/publicinterfaceCategoryTreeServiceextendsCommonService<CategoryTree,Long>{/***获取类别树*@returnList<CategoryTree>商品类别{@linkCategoryTree}*/publicList<CategoryTree>getCategoryTree();}importorg.springframework.data.domain.Page;importorg.springframework.data.domain.Pageable;importorg.springframework.data.domain.Sort;importorg.springframework.data.jpa.domain.Specification;importjava.io.Serializable;importjava.util.Collection;importjava.util.List;/***Createdbyliujunon2018/1/22.*/publicinterfaceCommonService<E,IDextendsSerializable>{publicEget(IDid);publicEfind(IDid);publicList<E>getAll();publicLonggetTotalCount();publicEsave(Eentity);publicEupdate(Eentity);publicvoiddelete(Eentity);publicvoiddelete(IDid);publicvoiddelete(Collection<E>entities);publicvoidflush();publicList<E>findAll(Specification<E>spec);publicPage<E>findAll(Pageablepageable);publicPage<E>findAll(Specification<E>spec,Pageablepageable);publicList<E>findAll(Specification<E>spec,Sortsort);publiclongcount(Specification<E>spec);}

5、service实现

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;importjava.util.List;/***@authorliujun*@since2018/1/18.*@seeCategoryTreeServiceImpl*产品类别业务类{@linkService}*/@ServicepublicclassCategoryTreeServiceImplextendsCommonServiceImpl<CategoryTree,Long>implementsCategoryTreeService{@Resource(name="baseSqlDaoImpl")CustomBaseSqlDaobaseSqlDao;@AutowiredCategoryTreeDaocategoryTreeDao;@OverridepublicList<CategoryTree>getCategoryTree(){Stringhql="selectofromCategoryTreeowhereo.parent_id=0orderbyo.sort_order";returnthis.baseSqlDao.queryForList(hql);}@AutowiredpublicvoidsetCategoryDao(CategoryTreeDaocategoryTreeDao){super.setCommonDao(categoryTreeDao);this.categoryTreeDao=categoryTreeDao;}}/***基础Service的定义*@authorJeffXu*@since2015-12-09*@param<E>*@param<ID>*/publicabstractclassCommonServiceImpl<E,IDextendsSerializable>implementsCommonService<E,ID>{protectedCommonDao<E,ID>commonDao;publicvoidsetCommonDao(CommonDao<E,ID>commonDao){this.commonDao=commonDao;}publicCommonDao<E,ID>getCommonDao(){returncommonDao;}/***根据ID获取某个Entity*@paramid*@return*/publicEget(IDid){returncommonDao.getOne(id);}/***根据ID查找某个Entity(建议使用)*@paramid*@return*/publicEfind(IDid){returncommonDao.findOne(id);}/***获取所有的Entity列表*@return*/publicList<E>getAll(){returncommonDao.findAll();}/***获取Entity的总数*@return*/publicLonggetTotalCount(){returncommonDao.count();}/***保存Entity*@paramentity*@return*/publicEsave(Eentity){returncommonDao.save(entity);}/***修改Entity*@paramentity*@return*/publicEupdate(Eentity){returncommonDao.save(entity);}/***删除Entity*@paramentity*/publicvoiddelete(Eentity){commonDao.delete(entity);}/***根据Id删除某个Entity*@paramid*/publicvoiddelete(IDid){commonDao.delete(id);}/***删除Entity的集合类*@paramentities*/publicvoiddelete(Collection<E>entities){commonDao.delete(entities);}/***清空缓存,提交持久化*/publicvoidflush(){commonDao.flush();}/***根据查询信息获取某个Entity的列表*@paramspec*@return*/publicList<E>findAll(Specification<E>spec){returncommonDao.findAll(spec);}/***获取Entity的分页信息*@parampageable*@return*/publicPage<E>findAll(Pageablepageable){returncommonDao.findAll(pageable);}/***根据查询条件和分页信息获取某个结果的分页信息*@paramspec*@parampageable*@return*/publicPage<E>findAll(Specification<E>spec,Pageablepageable){returncommonDao.findAll(spec,pageable);}/***根据查询条件和排序条件获取某个结果集列表*@paramspec*@paramsort*@return*/publicList<E>findAll(Specification<E>spec,Sortsort){returncommonDao.findAll(spec);}/***查询某个条件的结果数集*@paramspec*@return*/publiclongcount(Specification<E>spec){returncommonDao.count(spec);}}

6、dao接口

/***Createdbyliujunon2018/1/22.*/publicinterfaceCustomBaseSqlDao{publicList<Map<String,Object>>querySqlObjects(Stringsql,IntegercurrentPage,IntegerrowsInPage);publicList<Map<String,Object>>querySqlObjects(Stringsql);publicList<Map<String,Object>>querySqlObjects(Stringsql,List<Object>params);publicList<Map<String,Object>>querySqlObjects(Stringsql,Objectparams,IntegercurrentPage,IntegerrowsInPage);publicPageModel<Map<String,Object>>querySqlObjects(Stringsql,StringsbCount,Map<String,Object>params,IntegercurrentPage,IntegerrowsInPage);publicintgetCount(Stringsql);publicListqueryForList(Stringhql,List<Object>params);publicListqueryByMapParams(Stringhql,Map<String,Object>params,IntegercurrentPage,IntegerpageSize);publicListqueryByMapParams(Stringhql,Map<String,Object>params);publicListqueryForList(Stringhql);publicPageModelqueryForPage(Stringhql,intcurrentPage,intpageSize);publicPageModelqueryForPageWithParams(Stringhql,StringhqlCount,Map<String,Object>params,intcurrentPage,intpageSize);publicPageModelqueryForPageWithParams(Stringhql,Map<String,Object>params,intcurrentPage,intpageSize);publicPageModelqueryForPageBySql(Stringsql,IntegercurrentPage,IntegerpageSize);publicPageModelqueryForPageBySql(Stringsql,Map<String,Object>params,IntegercurrentPage,IntegerpageSize);publicLongqueryCount(Stringhql,Map<String,Object>params);publicIntegerqueryCountBySql(Stringsql,Map<String,Object>params);publicintexecuteSql(Stringsql,List<Object>params);}/***Createdbyliujunon2018/1/19.*/publicinterfaceCategoryTreeDaoextendsCommonDao<CategoryTree,Long>{}/***基础Dao接口定义*@since2015-12-09*/@NoRepositoryBeanpublicinterfaceCommonDao<E,IDextendsSerializable>extendsJpaRepository<E,ID>,JpaSpecificationExecutor<E>{}

7、dao实现

@Component(value="baseSqlDaoImpl")publicclassCustomBaseSqlDaoImplimplementsCustomBaseSqlDao{@AutowiredprivateEntityManagerem;publicList<Map<String,Object>>querySqlObjects(Stringsql,IntegercurrentPage,IntegerrowsInPage){returnthis.querySqlObjects(sql,null,currentPage,rowsInPage);}publicList<Map<String,Object>>querySqlObjects(Stringsql){returnthis.querySqlObjects(sql,null,null,null);}publicList<Map<String,Object>>querySqlObjects(Stringsql,List<Object>params){returnthis.querySqlObjects(sql,params,null,null);}@SuppressWarnings("unchecked")publicList<Map<String,Object>>querySqlObjects(Stringsql,Objectparams,IntegercurrentPage,IntegerrowsInPage){Queryqry=em.createNativeQuery(sql);SQLQuerys=qry.unwrap(SQLQuery.class);//设置参数if(params!=null){if(paramsinstanceofList){List<Object>paramList=(List<Object>)params;for(inti=0,size=paramList.size();i<size;i++){qry.setParameter(i+1,paramList.get(i));}}elseif(paramsinstanceofMap){Map<String,Object>paramMap=(Map<String,Object>)params;for(Stringkey:paramMap.keySet()){qry.setParameter(key,paramMap.get(key));}}}if(currentPage!=null&&rowsInPage!=null){//判断是否有分页//起始对象位置qry.setFirstResult(rowsInPage*(currentPage-1));//查询对象个数qry.setMaxResults(rowsInPage);}s.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);List<Map<String,Object>>resultList=newArrayList<Map<String,Object>>();try{resultList=s.list();}catch(Exceptione){}finally{em.close();}returnresultList;}publicPageModel<Map<String,Object>>querySqlObjects(Stringsql,StringsbCount,Map<String,Object>params,IntegercurrentPage,IntegerrowsInPage){PageModel<Map<String,Object>>pageModel=newPageModel<Map<String,Object>>();List<Map<String,Object>>list=this.querySqlObjects(sql,params,currentPage,rowsInPage);pageModel.setList(list);if(currentPage==null||rowsInPage==null){pageModel.setTotalCount(list==null?0:list.size());}else{Integercount=this.queryCountBySql(sbCount,params);pageModel.setCurrentPage(currentPage);pageModel.setTotalCount(count);pageModel.setPageSize(rowsInPage);inttotalPage=0;if(count%rowsInPage==0){totalPage=count/rowsInPage;}else{totalPage=count/rowsInPage+1;}pageModel.setTotalPage(totalPage);}returnpageModel;}publicintgetCount(Stringsql){StringsqlCount="selectcount(0)count_numfrom("+sql+")astotal";List<Map<String,Object>>list=this.querySqlObjects(sqlCount);if(list.size()>0){intcountNum=((BigInteger)list.get(0).get("count_num")).intValue();returncountNum;}else{return0;}}/***处理sql语句**@param_strSql*@return*/publicStringtoSql(String_strSql){StringstrNewSql=_strSql;if(strNewSql!=null){strNewSql=regReplace("'","''",strNewSql);}else{strNewSql="";}returnstrNewSql;}privateStringregReplace(StringstrFind,StringstrReplacement,StringstrOld){StringstrNew=strOld;Patternp=null;Matcherm=null;try{p=Pattern.compile(strFind);m=p.matcher(strOld);strNew=m.replaceAll(strReplacement);}catch(Exceptione){}returnstrNew;}/***根据hql语句查询数据*@paramhql*@return*/@SuppressWarnings("rawtypes")publicListqueryForList(Stringhql,List<Object>params){Queryquery=em.createQuery(hql);Listlist=null;try{if(params!=null&&!params.isEmpty()){for(inti=0,size=params.size();i<size;i++){query.setParameter(i+1,params.get(i));}}list=query.getResultList();}catch(Exceptione){e.printStackTrace();}finally{em.close();}returnlist;}@SuppressWarnings("rawtypes")publicListqueryByMapParams(Stringhql,Map<String,Object>params,IntegercurrentPage,IntegerpageSize){//EntityManagerem=this.emf.createEntityManager();Queryquery=em.createQuery(hql);Listlist=null;try{if(params!=null&&!params.isEmpty()){for(Map.Entry<String,Object>entry:params.entrySet()){query.setParameter(entry.getKey(),entry.getValue());}}if(currentPage!=null&&pageSize!=null){query.setFirstResult((currentPage-1)*pageSize);query.setMaxResults(pageSize);}list=query.getResultList();}catch(Exceptione){e.printStackTrace();}finally{em.close();}returnlist;}@SuppressWarnings("rawtypes")publicListqueryByMapParams(Stringhql,Map<String,Object>params){returnqueryByMapParams(hql,params,null,null);}@SuppressWarnings("rawtypes")publicListqueryForList(Stringhql){returnqueryForList(hql,null);}/***根据hql语句和分页条件查找分页数据*@paramhql*@paramcurrentPage*@parampageSize*@return*/@SuppressWarnings({"rawtypes","unchecked"})publicPageModelqueryForPage(Stringhql,intcurrentPage,intpageSize){PageModel&n

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved