javaee论坛

普通会员

225648

帖子

335

回复

349

积分

楼主
发表于 2017-07-18 00:45:26 | 查看: 84 | 回复: 3
当我们需要查询到位于不同表中的不同数据时,我们很容易想到建立视图,这样更加直观易懂,那么我们又怎么在my eclipse中实现对视图的操作呢?

下面我们来举个例子:

在对视图逆向生成的时候会有两个实体类:

一个是带id的,一个是不带id的,我们可以看下里面所对应的代码:

  1. DetailInfoV.java
  2. package com.yaxing.entity;
  3. /**
     * DetailInfoV entity. @author MyEclipse Persistence Tools
     */
  4. public class DetailInfoV implements java.io.Serializable {
  5.  // Fields
  6.  private DetailInfoVId id;
  7.  // Constructors
  8.  /** default constructor */
     public DetailInfoV() {
     }
  9.  /** full constructor */
     public DetailInfoV(DetailInfoVId id) {
      this.id = id;
     }
  10.  // Property accessors
  11.  public DetailInfoVId getId() {
      return this.id;
     }
  12.  public void setId(DetailInfoVId id) {
      this.id = id;
     }
  13. }
  14. DetailInfoVId:
  15. package com.yaxing.entity;
  16. import java.util.Date;
  17. /**
     * DetailInfoVId entity. @author MyEclipse Persistence Tools
     */
  18. public class DetailInfoVId implements java.io.Serializable {
  19.  // Fields
  20.  private String eveName;
     private String athName;
     private String sex;
     private Date birthday;
     private String number;
     private String unitName;
     private String fullName;
     private long userId;
  21.  // Constructors
  22.  /** default constructor */
     public DetailInfoVId() {
     }
  23.  /** 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;
     }
  24.  // Property accessors
  25.  public String getEveName() {
      return this.eveName;
     }
  26.  public void setEveName(String eveName) {
      this.eveName = eveName;
     }
  27.  public String getAthName() {
      return this.athName;
     }
  28.  public void setAthName(String athName) {
      this.athName = athName;
     }
  29.  public String getSex() {
      return this.sex;
     }
  30.  public void setSex(String sex) {
      this.sex = sex;
     }
  31.  public Date getBirthday() {
      return this.birthday;
     }
  32.  public void setBirthday(Date birthday) {
      this.birthday = birthday;
     }
  33.  public String getNumber() {
      return this.number;
     }
  34.  public void setNumber(String number) {
      this.number = number;
     }
  35.  public String getUnitName() {
      return this.unitName;
     }
  36.  public void setUnitName(String unitName) {
      this.unitName = unitName;
     }
  37.  public String getFullName() {
      return this.fullName;
     }
  38.  public void setFullName(String fullName) {
      this.fullName = fullName;
     }
  39.  public long getUserId() {
      return this.userId;
     }
  40.  public void setUserId(long userId) {
      this.userId = userId;
     }
  41.  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;
  42.   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());
     }
  43.  public int hashCode() {
      int result = 17;
  44.   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;
     }
  45. }

 

如上所示,在对实体类操作的时候,实际上我们要操作的是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中,当我们对里面的数据进行查询时,只需要迭代的进行输出即可。


普通会员

0

帖子

296

回复

312

积分
沙发
发表于 2023-09-02 14:20:46

围观

普通会员

1

帖子

319

回复

328

积分
板凳
发表于 2023-12-07 20:50:58

很好

普通会员

0

帖子

306

回复

311

积分
地板
发表于 2024-03-28 08:21:48

专业抢二楼!顺便笑摸狗头(3L)

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017