当我们需要查询到位于不同表中的不同数据时,我们很容易想到建立视图,这样更加直观易懂,那么我们又怎么在my eclipse中实现对视图的操作呢?下面我们来举个例子:
在对视图逆向生成的时候会有两个实体类:
一个是带id的,一个是不带id的,我们可以看下里面所对应的代码:
- DetailInfoV.java
- package com.yaxing.entity;
- /**
* DetailInfoV entity. @author MyEclipse Persistence Tools
*/ - public class DetailInfoV implements java.io.Serializable {
- // Fields
- private DetailInfoVId id;
- // Constructors
- /** default constructor */
public DetailInfoV() {
} - /** full constructor */
public DetailInfoV(DetailInfoVId id) {
this.id = id;
} - // Property accessors
- public DetailInfoVId getId() {
return this.id;
} - public void setId(DetailInfoVId id) {
this.id = id;
} - }
- DetailInfoVId:
- package com.yaxing.entity;
- import java.util.Date;
- /**
* DetailInfoVId entity. @author MyEclipse Persistence Tools
*/ - public class DetailInfoVId implements java.io.Serializable {
- // Fields
- private String eveName;
private String athName;
private String sex;
private Date birthday;
private String number;
private String unitName;
private String fullName;
private long userId; - // Constructors
- /** default constructor */
public DetailInfoVId() {
} - /** full constructor */
public DetailInfoVId(String eveName, String athName, String sex,
Date birthday, String number, String unitName, String fullName,
long userId) {
this.eveName = eveName;
this.athName = athName;
this.sex = sex;
this.birthday = birthday;
this.number = number;
this.unitName = unitName;
this.fullName = fullName;
this.userId = userId;
} - // Property accessors
- public String getEveName() {
return this.eveName;
} - public void setEveName(String eveName) {
this.eveName = eveName;
} - public String getAthName() {
return this.athName;
} - public void setAthName(String athName) {
this.athName = athName;
} - public String getSex() {
return this.sex;
} - public void setSex(String sex) {
this.sex = sex;
} - public Date getBirthday() {
return this.birthday;
} - public void setBirthday(Date birthday) {
this.birthday = birthday;
} - public String getNumber() {
return this.number;
} - public void setNumber(String number) {
this.number = number;
} - public String getUnitName() {
return this.unitName;
} - public void setUnitName(String unitName) {
this.unitName = unitName;
} - public String getFullName() {
return this.fullName;
} - public void setFullName(String fullName) {
this.fullName = fullName;
} - public long getUserId() {
return this.userId;
} - public void setUserId(long userId) {
this.userId = userId;
} - public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof DetailInfoVId))
return false;
DetailInfoVId castOther = (DetailInfoVId) other; - return ((this.getEveName() == castOther.getEveName()) || (this
.getEveName() != null
&& castOther.getEveName() != null && this.getEveName().equals(
castOther.getEveName())))
&& ((this.getAthName() == castOther.getAthName()) || (this
.getAthName() != null
&& castOther.getAthName() != null && this.getAthName()
.equals(castOther.getAthName())))
&& ((this.getSex() == castOther.getSex()) || (this.getSex() != null
&& castOther.getSex() != null && this.getSex().equals(
castOther.getSex())))
&& ((this.getBirthday() == castOther.getBirthday()) || (this
.getBirthday() != null
&& castOther.getBirthday() != null && this
.getBirthday().equals(castOther.getBirthday())))
&& ((this.getNumber() == castOther.getNumber()) || (this
.getNumber() != null
&& castOther.getNumber() != null && this.getNumber()
.equals(castOther.getNumber())))
&& ((this.getUnitName() == castOther.getUnitName()) || (this
.getUnitName() != null
&& castOther.getUnitName() != null && this
.getUnitName().equals(castOther.getUnitName())))
&& ((this.getFullName() == castOther.getFullName()) || (this
.getFullName() != null
&& castOther.getFullName() != null && this
.getFullName().equals(castOther.getFullName())))
&& (this.getUserId() == castOther.getUserId());
} - public int hashCode() {
int result = 17; - result = 37 * result
+ (getEveName() == null ? 0 : this.getEveName().hashCode());
result = 37 * result
+ (getAthName() == null ? 0 : this.getAthName().hashCode());
result = 37 * result
+ (getSex() == null ? 0 : this.getSex().hashCode());
result = 37 * result
+ (getBirthday() == null ? 0 : this.getBirthday().hashCode());
result = 37 * result
+ (getNumber() == null ? 0 : this.getNumber().hashCode());
result = 37 * result
+ (getUnitName() == null ? 0 : this.getUnitName().hashCode());
result = 37 * result
+ (getFullName() == null ? 0 : this.getFullName().hashCode());
result = 37 * result + (int) this.getUserId();
return result;
} - }
如上所示,在对实体类操作的时候,实际上我们要操作的是id的类。
所对应的action如下所示:
package com.yaxing.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaxing.entity.DetailInfoV;
import com.yaxing.entity.DetailInfoVId;
import com.yaxing.service.DetailInfoVService;
import com.yaxing.util.PageModel;
public class DetailInfoVAction extends ActionSupport {
private DetailInfoV detailInfoV;
private DetailInfoVService detailInfoVService;
private List<DetailInfoVId> findByEveNameAndUserID;
private String eveName;
private Long id;
private int offset;
private PageModel pager;
public void setDetailInfoV(DetailInfoV detailInfoV) {
this.detailInfoV = detailInfoV;
}
public DetailInfoV getDetailInfoV() {
return detailInfoV;
}
public void setDetailInfoVService(DetailInfoVService detailInfoVService) {
this.detailInfoVService = detailInfoVService;
}
public DetailInfoVService getDetailInfoVService() {
return detailInfoVService;
}
public void setFindByEveNameAndUserID(
List<DetailInfoVId> findByEveNameAndUserID) {
this.findByEveNameAndUserID = findByEveNameAndUserID;
}
public List<DetailInfoVId> getFindByEveNameAndUserID() {
return findByEveNameAndUserID;
}
public void setEveName(String eveName) {
this.eveName = eveName;
}
public String getEveName() {
return eveName;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setPager(PageModel pager) {
this.pager = pager;
}
public PageModel getPager() {
return pager;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getOffset() {
return offset;
}
public String findByEveNameAndUserId() throws Exception {
try {
/** System.out.println("eveName:"
+ new String(eveName.getBytes("ISO8859-1"), "UTF-8"));
System.out.println("id:" + id); */
List allValue = this.detailInfoVService.findByEveNameAndUserId(
new String(eveName.getBytes("ISO8859-1"), "UTF-8"), id);
findByEveNameAndUserID = new ArrayList();
Iterator iterator = allValue.iterator();
while (iterator.hasNext()) {
Object[] all = (Object[]) iterator.next(); 这里我们把要查询到的数据放到数组中,然后将数组中的值给数据集
DetailInfoVId detailInfoVId = new DetailInfoVId();
detailInfoVId.setEveName((String) all[0]);
detailInfoVId.setAthName((String) all[1]);
detailInfoVId.setSex((String) all[2]);
detailInfoVId.setBirthday((Date) all[3]);
detailInfoVId.setNumber((String) all[4]);
detailInfoVId.setUnitName((String) all[5]);
detailInfoVId.setFullName((String) all[6]);
detailInfoVId.setUserId((Long) all[7]);
findByEveNameAndUserID.add(detailInfoVId);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return INPUT;
}
return SUCCESS;
}
public String findByUserId() throws Exception {
try {
Map session = ActionContext.getContext().getSession();
String ids = session.get("userId").toString();
setId(Long.parseLong(ids));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return INPUT;
}
return SUCCESS;
}
public String pagerTaglib() {
int pagesize = 5;// 每页10条记录
int offset = 0;
HttpServletRequest request = ServletActionContext.getRequest();
if (request.getParameter("pager.offset") != null)
offset = Integer.parseInt(request.getParameter("pager.offset"));
PageModel pm = detailInfoVService.findAllDetailInfoV(offset, pagesize);
Map session = ActionContext.getContext().getSession();
session.put("pm", pm);
request.setAttribute("pm", pm);
return "success";
}
}
实际的数据集是存在于findByEveNameAndUserID中,当我们对里面的数据进行查询时,只需要迭代的进行输出即可。