hql查询实例

时间:2022-02-06 08:20:11

1、设计思路

(1)在页面中设计一个下拉框,数据取自数据库;

(2)查询是用hql查询。

2、设计实例

(1)Java模型层

public class Tree
{
      private String id;

      private String name;

      //id和name的get和set方法
}

(2)Java Dao层

public List<Tree> findTree();

(3)Java Dao实现层

@Override
public List<Tree> findTree()
{
      String hql = "from Tree tree order by tree.id";
      Query query = sessionFactory.getCurrentSession().createQuery(hql);
      List<Tree> list = query.list();
      return list;
}

(4)Java Service层

public List<Tree> findTree();

(5)Java Service实现层

@Override
public List<Tree> findTree()
{
       return treeDao.findTree();
}

(6)Java Action

private List<Tree> list;

//list get和set方法

@Resource(name="treeService")
private TreeService treeService;

@Override
@Action(value="tree",results={@Result(name="success",location="../../tree.jsp")})
public String execute() throws Exception
{
       list = treeService.findTree();
       return SUCCESS;
}

(7)页面

<label id="tree">树木:</label>
<select id="treeData">
     <option value="0">全部</option>
     <c:forEach items="${list}" var="tree">
          <option value="${tree.id}">${tree.name}</option>
     </c:forEach>
</select>