在hql中使用select *但获取值的地址

时间:2015-01-16 06:28:20

标签: java sql spring hibernate

我正在尝试在满足条件时从数据库中获取所有值。但我只获取列表的地址,而不是列为ALL时的值。我尝试使用<c:forEach>,但我一直收到错误java.lang.String。对于另一列,它工作正常。所以这是我在jsp中的代码

c:url var="actionUrl" value="process" />
<form id="student-form" method="post" action="${actionUrl }">
<select name="column">
    <option>All</option>
    <option>lastName</option>
    <option>firstName</option>
    <option>cpNum</option>
    <option>birthday</option>
    <option>grade</option>
</select>&nbsp

<select name="condition">
    <option>=</option>
    <option><</option>
    <option>></option>
    <option><=</option>
    <option>>=</option>
    <option>!=</option>
</select>&nbsp

<select id="gradeID" name="grades">
</select>&nbsp

<button id=submit>Submit</button><br/><br/>
</form>

<p>${studentList}</p>

控制器

@RequestMapping(value = "/process", method = RequestMethod.POST)
public String process(@RequestParam(value="column", defaultValue="") String column,
        @RequestParam(value="condition", defaultValue="") String condition,
        @RequestParam(value="grades", defaultValue="") int grades, ModelMap model)
{
    model.addAttribute("studentList", studentDao.findData(column, condition, grades));
    return "StudentForm";
}

StudentDaoImpl

@SuppressWarnings("unchecked")
public List<Student> findData(String column, String condition, int grades)
{
    if(column.equals("All"))
    {
        String hql = "FROM Student x WHERE x.grade"+ condition +":grades";
        Query query = getSessionFactory().openSession().createQuery(hql);
        query.setParameter("grades", grades);
        List<Student> result = query.list();

        return result;
    }

    else
    {
        String hql = "SELECT x."+ column +" FROM Student x WHERE x.grade"+ condition +":grades";
        Query query = getSessionFactory().openSession().createQuery(hql);
        query.setParameter("grades", grades);
        List<Student> result = query.list();

        return (List<Student>)result;
    }
}

StudentDao

public interface StudentDao 
{
  public List<Student> findData(String column, String condition, int grades);
}

2 个答案:

答案 0 :(得分:0)

用户迭代器并保持填充pojos并将它们添加到列表中。

答案 1 :(得分:0)

您应该以这种方式使用<c:foreach>

<c:forEach items="${studentList}" var="student">
    Name: <c:out value="${student.firstName} ${student.lastName}" />
    <!-- More data -->
</c:foreach>