需求:现有两张表,需要从中取出给前台做级联的JSON格式数据,省份中有城市,城市中有经销商。

一张字典表CMS_DICTIONARY 存放省份信息和城市信息,表结构为:

CID NUMBER(38) 主键   
UPID NUMBER(38) 上级关联外键
CNNAME VARCHAR2(255) 中文名称  

ps:其中UPID=1的为省份

一张经销商表TB_AUDI_DEALER存放经销商相关信息,表结构为:

AID NUMBER(38) 主键   
PROVINCE NUMBER(38) 省份id
CITY NUMBER(38) 城市id
DEALERNAME VARCHAR2(255) 经销商名称
 

对应的bean和映射配置文件:

1、Province

 
  
  1. private Long provinceid;  
  2. private Long upid;  
  3. private String name;  
  4. private Set<City> citys = new HashSet<City>(); 
 
  
  1. <hibernate-mapping> 
  2.     <class name="com.oemp.audi.user.Province" table="CMS_DICTIONARY"  > 
  3.         <id name="provinceid" type="long"> 
  4.             <column name="CID" precision="38" scale="0" /> 
  5.           <generator class="sequence"> 
  6.     <param name="sequence">SEQ_DICTIONARY</param> 
  7.    </generator> 
  8.         </id> 
  9.         <property name="upid" type="long"> 
  10.             <column name="UPID"/> 
  11.         </property> 
  12.         <property name="name" type="java.lang.String"> 
  13.             <column name="CNNAME" length="50" not-null="false" /> 
  14.         </property> 
  15.         <set name="citys" inverse="true" lazy="false"> 
  16.    <key> 
  17.     <column name="UPID" precision="38" scale="0" /> 
  18.    </key> 
  19.    <one-to-many class="com.oemp.audi.user.City" /> 
  20.   </set> 
  21.     </class> 
  22. </hibernate-mapping> 

2、City

 
  
  1. private Long cityid;  
  2. private String name;  
  3. private Set<Dealer> dealers = new HashSet<Dealer>();  
 
  
  1. <hibernate-mapping> 
  2.     <class name="com.oemp.audi.user.City" table="CMS_DICTIONARY"  > 
  3.         <id name="cityid" type="long"> 
  4.             <column name="CID" precision="38" scale="0" /> 
  5.           <generator class="sequence"> 
  6.                 <param name="sequence">SEQ_DICTIONARY</param> 
  7.             </generator> 
  8.         </id> 
  9.         <property name="name" type="java.lang.String"> 
  10.             <column name="CNNAME" length="50" not-null="false" /> 
  11.         </property> 
  12.           
  13.         <set name="dealers" inverse="true" lazy="false"> 
  14.             <key> 
  15.                 <column name="CITY" precision="38" scale="0" /> 
  16.             </key> 
  17.             <one-to-many class="com.oemp.audi.user.Dealer" /> 
  18.         </set> 
  19.     </class> 
  20. </hibernate-mapping> 

3、Dealer

 
  
  1. private Long dealerid;  
  2. private String dealername; 
 
  
  1. <hibernate-mapping> 
  2.     <class name="com.oemp.audi.user.Dealer" table="TB_AUDI_DEALER"  > 
  3.         <id name="dealerid" type="long"> 
  4.             <column name="AID" precision="38" scale="0" /> 
  5.             <generator class="sequence"> 
  6.                 <param name="sequence">SEQ_DICTIONARY</param> 
  7.             </generator> 
  8.         </id> 
  9.         <property name="dealername" type="java.lang.String"> 
  10.             <column name="DEALERNAME" length="255" not-null="false" /> 
  11.         </property> 
  12.     </class> 
  13. </hibernate-mapping> 

 struts2 的 action 中 调动方法

 
  
  1. public void getJson(){  
  2.             List<Province> list = provinceService.getProvinces();//直接查询得到省份  
  3.             List<Province> delP = new ArrayList<Province>();//待删除的省份:省份中城市们为空需删除  
  4.             for (Province p : list) {//遍历省份  
  5.                 Set<City> citys = p.getCitys();//得到城市们  
  6.                 List<City> delC = new ArrayList<City>();//待删除的经销商:城市中经销商们为空需删除  
  7.                 if(citys != null && !citys.isEmpty()){//判断城市们是否为空  
  8.                     for (City c : citys) {//遍历城市  
  9.                         Set<Dealer> dealers = c.getDealers();//得到经销商们  
  10.                         if(dealers == null || dealers.isEmpty()){//查看经销商们是否为空  
  11.                             delC.add(c);//加入待删除的经销商们  
  12.                         }  
  13.                     }  
  14.                     citys.removeAll(delC);//删除经销商们    
  15.                 }else{  
  16.                     delP.add(p);//加入待删除的城市们  
  17.                 }  
  18.             }  
  19.             list.removeAll(delP);//删除城市们  
  20.               
  21.             JsonConfig config = new JsonConfig();//json配置  
  22.             PropertyFilter proFilter = new PropertyFilter() {//过滤属性  
  23.                 public boolean apply(Object arg0, String arg1, Object arg2) {  
  24.                     if ("upid".equals(name) || "upid" == name) {  
  25.                         return true;  
  26.                     }  
  27.                     return false;  
  28.                 }  
  29.             };  
  30.             config.setJsonPropertyFilter(proFilter);//设置过滤属性  
  31.             JSONArray json = JSONArray.fromObject(list, config);//生成json对象  
  32.             renderText(json.toString());//输出json格式的字符串  
  33.     } 

 dao 中调用方法:

 

 
  
  1. public List<Province> getProvinces() {  
  2.         String hql = "from Province p where p.upid = 1 ";  
  3.         List<Province> list = new ArrayList<Province>();  
  4.         try{  
  5.             list = find(hql);  
  6.         }catch(Exception e){  
  7.             e.printStackTrace();  
  8.         }  
  9.         return list;  
  10.     }