欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取多表中的数据 博客分类: Hibernate Hibernate 

程序员文章站 2024-03-02 10:42:28
...
往往在页面上展示数据的时候往往不是来自同一张数据表中,这就需要进行多表查询,我一般操作有两种方式:但这两种方式都需要建立一个VO,就是需要将查询出来的数据用一个临时VO接受。
第一种做法:通过构造
1.先建立VO,vo的属性就是页面上显示的数据(如果页面上的数据来自一个表,就不需要这个操作了,因为一般都会给每个表建立一个对应的PO,数据直接从PO中获取接)
public class ErBaseStationVo {
	private String id;//基站信息objectid
	private String stationId;//基站编号
	private String stationName;//基站名称
	private String planLong;//滚动规划精度
	private String planLat;//滚动规划纬度
	private String isImport;//是否已导入
	public ErBaseStationVo(String id, String stationId, String stationName,
			String planLong, String planLat,String isImport) {
		super();
		this.id = id;
		this.stationId = stationId;
		this.stationName = stationName;
		this.planLong = planLong;
		this.planLat = planLat;
		this.isImport=isImport;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getStationId() {
		return stationId;
	}
	public void setStationId(String stationId) {
		this.stationId = stationId;
	}
	public String getStationName() {
		return stationName;
	}
	public void setStationName(String stationName) {
		this.stationName = stationName;
	}
	public String getPlanLong() {
		return planLong;
	}
	public void setPlanLong(String planLong) {
		this.planLong = planLong;
	}
	public String getPlanLat() {
		return planLat;
	}
	public void setPlanLat(String planLat) {
		this.planLat = planLat;
	}
	public String getIsImport() {
		return isImport;
	}
	public void setIsImport(String isImport) {
		this.isImport = isImport;
	}
	
}


2.通过sql语句查出每条记录,通过Object类型的List集合接受数据,这个List中的元素都是Object类型的,将其进行转换,通过构造函数,将获得的数据存到我们之前定义好的List中,代码如下:
public List<ErBaseStationVo> getAllStationVoInfoByProjectId(String projectId,String empId) {
		String sql="select e.objectId,e.gstaId,e.gstaPlanName,e.gstaDesLongit,e.gstaDesLatit,e.gisImport  from ErBaseinfo e where e.projectId='"+projectId+"' "
				+ " and e.ldesignInsId='"+empId+"'";
		List list=basedao.find(sql);
		List<ErBaseStationVo> returnlist=new ArrayList<ErBaseStationVo>();
		for (Object object : list) {
			Object[] o=(Object[]) object;
			returnlist.add(new ErBaseStationVo((String)o[0], (String)o[1], (String)o[2], (String)o[3], (String)o[4],(String)o[5]));
		}
		return returnlist;
	}

第二种做法:通过Transformers函数

1.依然是建立我们的VO,这个VO有一个空构造函数
public class StationForDesignerVO {
	private String objectId;
	private String stationName;//基站规划名称
	private String gbelongBanner;//旗县
	private String gnetStandard;//网络制式
	private String gsight;//场景
	private String gstaBuildState;//物理站建设状态
	
	public StationForDesignerVO(){
		
	}
	
	public StationForDesignerVO(String objectId, String stationName,
			String gbelongBanner, String gnetStandard, String gsight,
			String gstaBuildState) {
		super();
		this.objectId = objectId;
		this.stationName = stationName;
		this.gbelongBanner = gbelongBanner;
		this.gnetStandard = gnetStandard;
		this.gsight = gsight;
		this.gstaBuildState = gstaBuildState;
	}
	public String getObjectId() {
		return objectId;
	}
	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}
	public String getStationName() {
		return stationName;
	}
	public void setStationName(String stationName) {
		this.stationName = stationName;
	}
	public String getGbelongBanner() {
		return gbelongBanner;
	}
	public void setGbelongBanner(String gbelongBanner) {
		this.gbelongBanner = gbelongBanner;
	}
	public String getGnetStandard() {
		return gnetStandard;
	}
	public void setGnetStandard(String gnetStandard) {
		this.gnetStandard = gnetStandard;
	}
	public String getGsight() {
		return gsight;
	}
	public void setGsight(String gsight) {
		this.gsight = gsight;
	}
	public String getGstaBuildState() {
		return gstaBuildState;
	}
	public void setGstaBuildState(String gstaBuildState) {
		this.gstaBuildState = gstaBuildState;
	}
	
}


2.通过sql语句查询数据,与上一种不同的是,需要将每个字段重命名即as,之后的每个字段与建立的VO中的属性名一致即可,用Hibernate的转换函数即可实现内容的存储,比第一种方便的多
public List<StationForDesignerVO> getStationInfo(String projectId) {
		// TODO Auto-generated method stub
		String hql="select e.objectId as objectId,e.gstaPlanName as stationName,e.gbelongBanner as gbelongBanner,"
				+ " e.gnetStandard as gnetStandard,e.gsight as gsight,e.gstaBuildState as gstaBuildState  from ErBaseinfo e "
				+ " where e.projectId='"+projectId+"' and e.ldesignInsId='未分配'";
		Session session=basedao.sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setResultTransformer(Transformers.aliasToBean(StationForDesignerVO.class));
		List<StationForDesignerVO> list=query.list();
		return list;
	}
相关标签: Hibernate