无法正确显示数据库的提取项目

时间:2018-12-31 12:59:45

标签: java hibernate spring-mvc

我在NetBeans上使用Java启动了一个新项目,其中包括Spring MVC和hibernate框架。成功连接数据库后,我继续为控制器和模型创建两个程序包。在模型中,我使用了休眠模式来生成所有必需的类。我创建了一个客户控制器来从数据库中获取数据,并且确实进行了获取,但是获取的数据是对应的类和附加的字母数字字符的随机组合。即[com.model.Cutomer @ 317ee475,com.model.Cutomer @ 17b74633,com.model.Cutomer @ 31057ac8,com.model.Cutomer @ 41381eb6,com.model.Cutomer @ 7a307a8,com.model.Cutomer @ 2a884fb1 ,com.model.Cutomer@3041a312,com.model.Cutomer@46913e7b]

为获得更好的见解,代码看起来像 CustomerController:

public class CustomerController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        ModelAndView mv =new ModelAndView("customer");
        try{
            Session session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            List result=session.createQuery("from Cutomer").list();         
            mv.addObject("custob",result);
            session.getTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
        }
        mv.addObject("title","Customer List");
        return mv;
    }
}

在模型包cutomer.java中:

package com.model;
public class Cutomer  implements java.io.Serializable {
     private String custId;
     private Address address;
     private String custName;

    public Cutomer() {
    }


    public Cutomer(String custId) {
        this.custId = custId;
    }
    public Cutomer(String custId, Address address, String custName) {
       this.custId = custId;
       this.address = address;
       this.custName = custName;
    }

    public String getCustId() {
        return this.custId;
    }

    public void setCustId(String custId) {
        this.custId = custId;
    }
    public Address getAddress() {
        return this.address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
    public String getCustName() {
        return this.custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }
}

和 cutomer.hbm.xml看起来像:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 27, 2018 1:50:14 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="com.model.Cutomer" table="cutomer" catalog="farm" optimistic-lock="version">
        <id name="custId" type="string">
            <column name="custID" length="5" />
            <generator class="assigned" />
        </id>
        <many-to-one name="address" class="com.model.Address" fetch="select">
            <column name="address" />
        </many-to-one>
        <property name="custName" type="string">
            <column name="custName" length="30" />
        </property>
    </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

您的JSTL代码应该是

<c:forEach items="${custob}" var="cutomer">
    <tr>
        <td>Cust Id : <c:out value="${cutomer.custId}"/></td>
        <td>Cust Name: <c:out value="${cutomer.custName }"/></td>  
    </tr>
</c:forEach>