java 分页方法
来源:互联网 发布:linux more 下一页 编辑:程序博客网 时间:2024/06/05 14:08
分页查询的两种方式:
第一种:使用SessionFactory .openSession();方法,此方法不会创建的session会和绑定到当前线程,创建的线程不会在事务回滚或事物提交后自动关闭,必须手动关闭。
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
/**
* @Description: 审批已通过报销单请求,分页查询
* @param fid 查询条件
* @Description: 审批已通过报销单请求,分页查询
* @param fid 查询条件
* @param m 第几条数据开始
* @param n 展示几条数据
* @return List<UserReimburse>
* @throws
*/
@ SuppressWarnings("unchecked")
public List<UserReimburse> findApprovaPass(String fid, int m, int n) {
List<UserReimburse> result = null;
try {
SessionFactory sf = hTemplate.getSessionFactory();
Session session = sf.openSession();
String hql = "from UserReimburse u where u.userAccount=:fid and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
System.out.println("=======================>>>>>>>" + hql);
Query query = session.createQuery(hql);
query.setParameter("fid", fid);
query.setFirstResult(m);
query.setMaxResults(n);
result = query.list();
System.out.println("result = " + result);
session.flush();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
* @throws
*/
@ SuppressWarnings("unchecked")
public List<UserReimburse> findApprovaPass(String fid, int m, int n) {
List<UserReimburse> result = null;
try {
SessionFactory sf = hTemplate.getSessionFactory();
Session session = sf.openSession();
String hql = "from UserReimburse u where u.userAccount=:fid and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
System.out.println("=======================>>>>>>>" + hql);
Query query = session.createQuery(hql);
query.setParameter("fid", fid);
query.setFirstResult(m);
query.setMaxResults(n);
result = query.list();
System.out.println("result = " + result);
session.flush();
session.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
第二种:使用SessionFactory .getCurrentSession();方法,此方法会创建的session会和绑定到当前线程,创建的线程会在事务回滚或事物提交后自动关闭。
使用此方法Spring中applicationContext.xml配置文件要进行如下设置:
<!-- JDBC事务管理器,根据你的情况使用不同的事务管理器,如果工程中有Hibernate,就用Hibernate的事务管理器 -->
<beans
xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- 用注解来实现事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
/**
* 分页查询
*
* @param message 查询条件
* @param pageNo 第几条数据开始
* @param pageSize 展示几条数据
*
* @return List<UserReimburse>
*/
@SuppressWarnings("unchecked")
@Transactional(rollbackFor=Exception.class)
public List<UserReimburse> findPageByExcemple(String message, int pageNo, int pageSize) throws Exception{
List<UserReimburse> listReimburse = null;
//String hql = "from UserReimburse u where u.userAccount='YBWADC' and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
String hql = "from UserReimburse u where u.userAccount=:fid and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
try {
System.out.println("hql >>>> " + hql);
Session session = hbTemplate.getSessionFactory().getCurrentSession();
Query query = session.createQuery(hql);
query.setParameter("fid", message);
query.setFirstResult(pageNo); // 第几条数据
query.setMaxResults(pageSize); // 展示几条数据
System.out.println("分页查询结果 = " + query.list());
listReimburse = query.list();
if (null != listReimburse) {
return listReimburse;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return null;
}
0 0
- java 分页方法
- java 分页方法
- Java分页原理及常用分页方法
- Java的分页查询方法
- 模拟百度分页java方法
- java实现的分页方法(按照页码范围分页)
- 在Java中分页的方法
- java分页查询 方法一 Query
- java分页查询 方法二 Criteria
- Java和SQL中的分页方法大全
- Java 分页时获取总页数方法
- Java分页的三种方法
- 分页方法
- 分页方法
- 分页方法
- 分页方法
- 分页方法
- java实现的分页方法(上一页下一页)
- 漫步空间
- C语言中随机函数的使用
- Java 8新特性探究(二)深入解析默认方法
- thymleaf 使用
- Hacking Vim: calendar.vim(04)
- java 分页方法
- PHP基础
- cvsnt 配置 cvsnt2.5.04配置 cvsnt服务器端配置
- 文章标题
- 数据库事务隔离级别
- ElasticSearch 入门
- UVA - 12657 Boxes in a Line(双向链表)
- 几条使用率最高的SQL语句
- 在命令里编译和执行 带包名 和 jar包 java的文件