需求:现有两张表,需要从中取出给前台做级联的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
- private Long provinceid;
- private Long upid;
- private String name;
- private Set<City> citys = new HashSet<City>();
- <hibernate-mapping>
- <class name="com.oemp.audi.user.Province" table="CMS_DICTIONARY" >
- <id name="provinceid" type="long">
- <column name="CID" precision="38" scale="0" />
- <generator class="sequence">
- <param name="sequence">SEQ_DICTIONARY</param>
- </generator>
- </id>
- <property name="upid" type="long">
- <column name="UPID"/>
- </property>
- <property name="name" type="java.lang.String">
- <column name="CNNAME" length="50" not-null="false" />
- </property>
- <set name="citys" inverse="true" lazy="false">
- <key>
- <column name="UPID" precision="38" scale="0" />
- </key>
- <one-to-many class="com.oemp.audi.user.City" />
- </set>
- </class>
- </hibernate-mapping>
2、City
- private Long cityid;
- private String name;
- private Set<Dealer> dealers = new HashSet<Dealer>();
- <hibernate-mapping>
- <class name="com.oemp.audi.user.City" table="CMS_DICTIONARY" >
- <id name="cityid" type="long">
- <column name="CID" precision="38" scale="0" />
- <generator class="sequence">
- <param name="sequence">SEQ_DICTIONARY</param>
- </generator>
- </id>
- <property name="name" type="java.lang.String">
- <column name="CNNAME" length="50" not-null="false" />
- </property>
- <set name="dealers" inverse="true" lazy="false">
- <key>
- <column name="CITY" precision="38" scale="0" />
- </key>
- <one-to-many class="com.oemp.audi.user.Dealer" />
- </set>
- </class>
- </hibernate-mapping>
3、Dealer
- private Long dealerid;
- private String dealername;
- <hibernate-mapping>
- <class name="com.oemp.audi.user.Dealer" table="TB_AUDI_DEALER" >
- <id name="dealerid" type="long">
- <column name="AID" precision="38" scale="0" />
- <generator class="sequence">
- <param name="sequence">SEQ_DICTIONARY</param>
- </generator>
- </id>
- <property name="dealername" type="java.lang.String">
- <column name="DEALERNAME" length="255" not-null="false" />
- </property>
- </class>
- </hibernate-mapping>
struts2 的 action 中 调动方法
- public void getJson(){
- List<Province> list = provinceService.getProvinces();//直接查询得到省份
- List<Province> delP = new ArrayList<Province>();//待删除的省份:省份中城市们为空需删除
- for (Province p : list) {//遍历省份
- Set<City> citys = p.getCitys();//得到城市们
- List<City> delC = new ArrayList<City>();//待删除的经销商:城市中经销商们为空需删除
- if(citys != null && !citys.isEmpty()){//判断城市们是否为空
- for (City c : citys) {//遍历城市
- Set<Dealer> dealers = c.getDealers();//得到经销商们
- if(dealers == null || dealers.isEmpty()){//查看经销商们是否为空
- delC.add(c);//加入待删除的经销商们
- }
- }
- citys.removeAll(delC);//删除经销商们
- }else{
- delP.add(p);//加入待删除的城市们
- }
- }
- list.removeAll(delP);//删除城市们
- JsonConfig config = new JsonConfig();//json配置
- PropertyFilter proFilter = new PropertyFilter() {//过滤属性
- public boolean apply(Object arg0, String arg1, Object arg2) {
- if ("upid".equals(name) || "upid" == name) {
- return true;
- }
- return false;
- }
- };
- config.setJsonPropertyFilter(proFilter);//设置过滤属性
- JSONArray json = JSONArray.fromObject(list, config);//生成json对象
- renderText(json.toString());//输出json格式的字符串
- }
dao 中调用方法:
- public List<Province> getProvinces() {
- String hql = "from Province p where p.upid = 1 ";
- List<Province> list = new ArrayList<Province>();
- try{
- list = find(hql);
- }catch(Exception e){
- e.printStackTrace();
- }
- return list;
- }