用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - jsp代码库

hibernate多条件筛选器 模糊查询

2013-06-23 作者:海大软件1102班举报

[jsp]代码库

public List<User> searchUser(final String username, final int userId,
                             final String lastVisitIP, final Date fromCreateDate,
                             final Date toCreateDate, final Date fromVisitDate,
                             final Date toVisitDate, final String isdesc) {
    List<User> users = (List<User>) this.hibernateTemplate
    .execute(new HibernateCallback() {
        public Object doInHibernate(Session session)
        throws HibernateException, SQLException {
            Criteria cr = session.createCriteria(User.class);
            System.out.println("用户ID" + userId);
            if (userId > 0) {
                cr.add(Expression.eq("id", userId));
            } else {
                if (username != null && !username.equals("")) {
                    cr.add(Expression.like("username", "%"
                                           + username + "%"));
                }
                if (lastVisitIP != null && !lastVisitIP.equals("")) {
                    cr.add(Expression.like("lastVisitIP",
                                           lastVisitIP + "%"));
                }
                System.out.println("时间" + fromCreateDate + " || "
                                   + toCreateDate + " || " + fromVisitDate
                                   + " || " + toVisitDate);
                Date fromdate = new Date();
                fromdate.setYear(02);
                Date todate = new Date();
                todate.setYear(998);
                if (!fromCreateDate.before(fromdate)
                && !toCreateDate.after(todate)) {
                    cr.add(Restrictions.between("registTime",
                                                fromCreateDate, toCreateDate));
                }
                if (!fromVisitDate.before(fromdate)
                && !toVisitDate.after(todate)) {

                    cr.add(Restrictions.between("lastVisitTime",
                                                fromVisitDate, toVisitDate));
                }

                if (isdesc != null && !isdesc.equals("")) {
                    if (isdesc.equals("true")) {
                        cr.addOrder(Order.desc("id"));
                    } else {
                        cr.addOrder(Order.asc("id"));

                    }
                }

            }

            return cr.list();

        }
    });
    for (User user : users) {
        System.out.println(user.getUsername() + " - " + user.getId());
    }
    return users;
}


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。