<address id="ttjl9"></address>

      <noframes id="ttjl9"><address id="ttjl9"><nobr id="ttjl9"></nobr></address>
      <form id="ttjl9"></form>
        <em id="ttjl9"><span id="ttjl9"></span></em>
        <address id="ttjl9"></address>

          <noframes id="ttjl9"><form id="ttjl9"></form>

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

          2019-9-29    seo達人

          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元數據:

          <?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");

          //獲得查詢參數

          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));

          }



          //作為參數傳遞給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();



          //關聯session

          Criteria criteria = detachedCriteria.getExecutableCriteria(session);

          //查詢

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

          return list;



          }



          前端頁面:

          忘記寫查詢數據回顯了…



          <%@ 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>

            <!-- 顯示數據 -->

            <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>



          測試:



          藍藍設計www.syprn.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計  cs界面設計  ipad界面設計 、 包裝設計 、 圖標定制  用戶體驗 、交互設計、 網站建設 、平面設計服務。

          日歷

          鏈接

          個人資料

          藍藍設計的小編 http://www.syprn.cn

          存檔

          亚洲va欧美va天堂v国产综合