您的位置:首页 > 其它

中国城市数据 上下级关系 区号 邮编 entity by jpa

2013-02-05 22:47 639 查看
主要是脚本里包含中国城市的数据,如果对数据没兴趣,不要继续往下看了。可导入使用,一直到县或区,包括电话区号和邮编。(数据没有经过严格验证,若存在问题,请邮件联系partner4java@163.com)

脚本下载地址:http://download.csdn.net/detail/partner4java/5055425

代码示例下载地址:http://download.csdn.net/detail/partner4java/5055436

entity为jpa格式,不建议每次都查取,因为城市基本就不会变,建议设置为一个本机的Hash启动加载或使用商业缓存产品。

等级枚举:

package com.nongfu888.info.enums;

/**
* 城市等级<br/>
*
*
* @author partner4java
*
*/
public enum CityGrade {
/** 省/自治区 */
PROVINCIAL {
@Override
public int grade() {
return 1;
}
},
/** 直辖市 */
MUNICIPALITIES {
@Override
public int grade() {
return 1;
}
},
/** 市 */
CITY {
@Override
public int grade() {
return 2;
}
},
/** 县\区 */
DISTRICT {
@Override
public int grade() {
return 3;
}
},
/** 乡镇 */
VILLAGES {
@Override
public int grade() {
return 4;
}
};

/**
* 返回显示级别<br/>
*
* @return 1为省会或直辖市(直辖市的2级也填写市名称);2为市级(包括直辖市);3为县级或直辖市的区;4为乡镇。
*/
public abstract int grade();
}
城市entity:

package com.nongfu888.info.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.nongfu888.info.enums.CityGrade;

/**
* 城市对象
*
* @author partner4java
*
*/
@Entity
public class City implements Serializable {
private static final long serialVersionUID = 6258700470046195512L;

/** 城市id */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int cityId;

/** 城市名称 */
@Column(length = 20)
private String cityName;

/** 邮编 */
@Column(length = 6)
private int zipCode;

/** 电话区号,但是保存为数字,去掉前面的0,取的时候需要加上0 */
@Column(length = 3)
private int areaCode;

/** 城市级别 */
@Enumerated(EnumType.STRING)
private CityGrade grade;

/** 所属上级城市:为null,即为最顶级 -- 省、直辖市 */
@ManyToOne(fetch = FetchType.LAZY)
private City belongCity;

/** 包含的城市 */
@OneToMany(mappedBy = "belongCity", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<City> containCitys = new HashSet<City>();
service借助了p4jorm(p4jorm 参照相关文章 http://blog.csdn.net/partner4java)
package com.nongfu888.info.service;

import com.nongfu888.info.entity.City;
import com.partner4java.orm.dao.P4jDao;

/**
* 城市实体业务处理类
*
* @author partner4java
*
*/
public interface CityService extends P4jDao<City> {

}
package com.nongfu888.info.service.impl;

import org.springframework.stereotype.Service;

import com.nongfu888.info.entity.City;
import com.nongfu888.info.service.CityService;
import com.partner4java.orm.dao.P4jJpaDaoSupport;

@Service
public class CityServiceBean extends P4jJpaDaoSupport<City> implements CityService {

}
配置具体查看下载附件吧,本测试结合了P6Spy,可以打印出当下的SQL最终执行脚本(就是赋值后的)。

测试用例又借助了spring-test分支:

@ContextConfiguration(locations = { "/META-INF/spring/beans.xml" })
@TransactionConfiguration(transactionManager = "transactionManagerJpa")
public class CityServiceBeanTest extends AbstractTransactionalJUnit4SpringContextTests {
private CityService cityService;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

@Autowired
public void setCityService(CityService cityService) {
this.cityService = cityService;
}

@Autowired
public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}

@Test
public void testClear() {
}

@Test
@Rollback(false)
public void testDelete() {
cityService.delete(1);
}

@Test
public void testGet() {
City city = cityService.get(1);
System.out.println(city);
System.out.println("=============================");
System.out.println(city.getContainCitys());
System.out.println("-------------------------");
// System.out.println(city.getBelongCity());
}

@Test
@Rollback(false)
public void testSave() {
City city = new City();
city.setCityName("山东省");
city.setGrade(CityGrade.PROVINCIAL);
Set<City> containCitys = new HashSet<City>();
containCitys.add(new City("济宁市", 272100, 0537, CityGrade.CITY, city));
containCitys.add(new City("济南市", 272000, 0531, CityGrade.CITY, city));

city.setContainCitys(containCitys);
cityService.save(city);
}

@Test
public void testQuery() {
CityFormBean cityFormBean = new CityFormBean();
cityFormBean.setCityName("山东省");

PageData<City> citys = cityService.query(cityFormBean, new PageIndex(1), null);
City city = citys.getResultlist().get(0);
System.out.println(city);
System.out.println("=============================");
Set<City> chCitys = city.getContainCitys();
for(City chCity:chCitys){
System.out.println(chCity);
System.out.println(chCity.getContainCitys());
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: