Hibernate--使用離線查詢DetachedCriteria完成對列表的篩選

2019-9-29    seo達(dá)人

User表:





User類:

package pers.zhang.domain;



public class User {



private Long user_id;

private String user_code;

private String user_name;

private String user_password;

private String user_state;

public Long getUser_id() {

return user_id;

}

public void setUser_id(Long user_id) {

this.user_id = user_id;

}

public String getUser_code() {

return user_code;

}

public void setUser_code(String user_code) {

this.user_code = user_code;

}

public String getUser_name() {

return user_name;

}

public void setUser_name(String user_name) {

this.user_name = user_name;

}

public String getUser_password() {

return user_password;

}

public void setUser_password(String user_password) {

this.user_password = user_password;

}

public String getUser_state() {

return user_state;

}

public void setUser_state(String user_state) {

this.user_state = user_state;

}

}



ORM元數(shù)據(jù):

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    ";

<hibernate-mapping package="pers.zhang.domain" >

<class name="User" table="sys_user" >

<id name="user_id"  >

<generator class="identity"></generator>

</id>

<property name="user_code" column="user_code" ></property>

<property name="user_name" column="user_name" ></property>

<property name="user_password" column="user_password" ></property>

<property name="user_state" column="user_state" ></property>

</class>

</hibernate-mapping>



控制層:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

//獲得查詢參數(shù)

String userIdStr = request.getParameter("userId");

String userName = request.getParameter("userName");

//獲得離線查詢對象

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);

//拼裝查詢條件

if(userIdStr != null && !"".equals(userIdStr)) {

Long userId = (long) Integer.parseInt(userIdStr);

detachedCriteria.add(Restrictions.eq("user_id", userId));

}

if(userName != null && !"".equals(userName)) {

detachedCriteria.add(Restrictions.like("user_name", userName, MatchMode.ANYWHERE));

}



//作為參數(shù)傳遞給service層

UserService userService = new UserService();

List<User> list = userService.findUserByCondition(detachedCriteria);



request.setAttribute("userList", list);

request.getRequestDispatcher("list2.jsp").forward(request, response);

}



Service層:

public List<User> findUserByCondition(DetachedCriteria detachedCriteria) {

//傳遞給Dao層

UserDao userDao = new UserDao();

return userDao.findUserByCondition(detachedCriteria);

}

1

2

3

4

5

Dao層:

public List<User> findUserByCondition(DetachedCriteria detachedCriteria) {

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();



//關(guān)聯(lián)session

Criteria criteria = detachedCriteria.getExecutableCriteria(session);

//查詢

List<User> list = criteria.list();

return list;



}



前端頁面:

忘記寫查詢數(shù)據(jù)回顯了…



<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ taglib uri="
http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>

<script type="text/javascript" src="js/bootstrap.min.js" ></script>

<link rel="stylesheet" href="css/bootstrap.css" />

</head>

<body>

<form class="form-inline" role="form" method="post" action="${pageContext.request.contextPath }/findUserByCondition">

  <div class="form-group">

    <input type="text" class="form-control" id="exampleInputEmail2" placeholder="用戶ID" name="userId">

  </div>

  <div class="form-group">

    <input type="text" class="form-control" id="exampleInputPassword2" placeholder="用戶名" name="userName">

  </div>

  <button type="submit" class="btn btn-default">篩選</button>

</form>



<div style="text-align: center; width: 600px;">

<table class="table table-hover">

  <tr><td>編號</td><td>用戶ID</td><td>用戶名</td><td>昵稱</td><td>密碼</td></tr>

  <!-- 顯示數(shù)據(jù) -->

  <c:forEach items="${userList }" var="list">

  <tr><td>${list.user_id }</td><td>${list.user_code }</td><td>${list.user_name }</td><td>${list.user_password }</td><td>${list.user_state }</td></tr>

  </c:forEach>

</table>

</div>





</body>

</html>



測試:



藍(lán)藍(lán)設(shè)計m.820esy.cn )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計 、 cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標(biāo)定制 、 用戶體驗 、交互設(shè)計、 網(wǎng)站建設(shè) 平面設(shè)計服務(wù)。

分享本文至:

日歷

鏈接

個人資料

存檔