Spring+Hibernate系列教材 (三)- 查询总数

使用HibernateTemplate查询总数

步骤1:查询总数

步骤 1 : 查询总数

通过find方法执行select(*),接着会返回一个List里面第一个元素即总数

package com.how2java.test;

  

import java.util.List;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.dao.CategoryDAO;

  

public class TestSpring {

  

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext(

                new String[] { "applicationContext.xml" });

        CategoryDAO dao = (CategoryDAO) context.getBean("dao");

         

        List<Long> l =dao.find("select count(*) from Category c");

        long total = l.get(0);

        System.out.println(total);

    }

}


更多内容,点击了解: https://how2j.cn/k/spring-hibernate/spring-hibernate-count/103.html

原文地址:https://www.cnblogs.com/Lanht/p/12789356.html