• Hibernate使用Criteria实现分页



    Controller


    @RequestMapping(value = "/page")
    public String page(Model model,DataGridModel page,UserCharge userCharges) throws Exception{
    // List<SpInfo> spInfos = spInfoService.findSpInfoPageList(page)
    List<UserCharge> userCharge = userChargeService.geAllChargeByID(page,userCharges);
    model.addAttribute("userCharge", userCharge);
    model.addAttribute("uid", userCharges.getUid());
    model.addAttribute("page",page);
    return "user/page";
    }


    ServiceImpl
    public List<UserCharge> geAllChargeByID(DataGridModel page,UserCharge userCharge) throws Exception {
    List<UserCharge> list=new ArrayList<UserCharge>();
    try {
    page.setOrder(Constants.DESC);
    page.setSort("createdate");
    long totalRows = page.getTotalRows();
    if(totalRows==0){
    totalRows = userChargeDao.getTotalCount(userCharge);
    }
    page.setPageSize(page.getPageSize());
    page.initPage(totalRows);
    list= userChargeDao.findByConditionWithPage(userCharge,page);
    }catch (Exception e){
    e.printStackTrace();
    }
    return list;
    }

    BaseDao
    Long getTotalCount(Object object);
    List<E> findByConditionWithPage(Object obj, DataGridModel page);
    public List<E> findByConditionWithPage(Object object, DataGridModel page){
    Criteria criteria=getCurrentSession().createCriteria(object.getClass());
    if (object != null) {
    try {
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {// --for() begin
    if (field.getGenericType().toString().equals(
    "class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));

    String val = (String) m.invoke(object);// 调用getter方法获取属性值
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Integer
    if (field.getGenericType().toString().equals(
    "class java.lang.Integer")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Integer val = (Integer) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是Double
    if (field.getGenericType().toString().equals(
    "class java.lang.Double")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Double val = (Double) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是Boolean 是封装类
    if (field.getGenericType().toString().equals(
    "class java.lang.Boolean")) {
    Method m = (Method) object.getClass().getMethod(
    field.getName());
    Boolean val = (Boolean) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是boolean 基本数据类型不一样 这里有点说名如果定义名是 isXXX的 那就全都是isXXX的
    // 反射找不到getter的具体名
    if (field.getGenericType().toString().equals("boolean")) {
    Method m = (Method) object.getClass().getMethod(
    field.getName());
    Boolean val = (Boolean) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Date
    if (field.getGenericType().toString().equals(
    "class java.util.Date")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Date val = (Date) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Short
    if (field.getGenericType().toString().equals(
    "class java.lang.Short")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Short val = (Short) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    setPageRequestToCriteria(criteria, page);
    List<E> result = criteria.list();
    return result;
    }


    private Criteria setPageRequestToCriteria(Criteria c, DataGridModel page) {
    Validate.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");
    c.setFirstResult(page.getCurrentRow());
    c.setMaxResults(page.getPageSize());
    if ("asc".equals(page.getOrder())) {
    c.addOrder(Order.asc(page.getSort()));
    } else if("desc".equals(page.getOrder())){
    c.addOrder(Order.desc(page.getSort()));
    }else{

    }
    return c;
    }

    public Long getTotalCount(Object object){
    Criteria criteria=getCurrentSession().createCriteria(object.getClass());
    if (object != null) {
    try {
    Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {// --for() begin
    if (field.getGenericType().toString().equals(
    "class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));

    String val = (String) m.invoke(object);// 调用getter方法获取属性值
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Integer
    if (field.getGenericType().toString().equals(
    "class java.lang.Integer")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Integer val = (Integer) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是Double
    if (field.getGenericType().toString().equals(
    "class java.lang.Double")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Double val = (Double) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是Boolean 是封装类
    if (field.getGenericType().toString().equals(
    "class java.lang.Boolean")) {
    Method m = (Method) object.getClass().getMethod(
    field.getName());
    Boolean val = (Boolean) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }

    // 如果类型是boolean 基本数据类型不一样 这里有点说名如果定义名是 isXXX的 那就全都是isXXX的
    // 反射找不到getter的具体名
    if (field.getGenericType().toString().equals("boolean")) {
    Method m = (Method) object.getClass().getMethod(
    field.getName());
    Boolean val = (Boolean) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Date
    if (field.getGenericType().toString().equals(
    "class java.util.Date")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Date val = (Date) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    // 如果类型是Short
    if (field.getGenericType().toString().equals(
    "class java.lang.Short")) {
    Method m = (Method) object.getClass().getMethod(
    "get" + getMethodName(field.getName()));
    Short val = (Short) m.invoke(object);
    if (val != null) {
    criteria.add(Restrictions.eq(field.getName(), val));
    }

    }
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    }
    //criteria.add(Restrictions.eq("uid",uid));
    criteria.setProjection(Projections.rowCount());
    return (Long)criteria.uniqueResult();
    }

    // 把一个字符串的第一个字母大写、效率是最高的、
    public String getMethodName(String fildeName) throws Exception{
    byte[] items = fildeName.getBytes();
    items[0] = (byte) ((char) items[0] - 'a' + 'A');
    return new String(items);
    }


    注:hibernate DetachedCriteria离线条件查询
    hibernate criteria在线数据查询

    方法大同小异,可自行到网上查找
    DetachedCriteria的用法


  • 相关阅读:
    Adding timestamps to terminal prompts
    opensuse nvidia
    小物体检测
    openSUSE 多个GPU设置 深度学习 Caffe PyTorch 等
    openSUSE 高清屏设置
    手把手教你NLTK WordNet使用方法
    [ICCV 2019] Weakly Supervised Object Detection With Segmentation Collaboration
    Instance Segmentation入门总结
    [PAMI 2018] Differential Geometry in Edge Detection: accurate estimation of position, orientation and curvature
    [CVPR2017] Deep Self-Taught Learning for Weakly Supervised Object Localization 论文笔记
  • 原文地址:https://www.cnblogs.com/foreverstudy/p/10273503.html
Copyright © 2020-2023  润新知