JPA 分页处理

1、要实现jpa分页管理首先得要正确配置jpa环境,在spring环境中的配置如下:

开启注解功能

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bus" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="3" />
<property name="maxPoolSize" value="30" />
<property name="initialPoolSize" value="3" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="1" />
<property name="acquireRetryAttempts" value="30" />
<property name="acquireRetryDelay" value="1000" />
<property name="testConnectionOnCheckin" value="true" />
<property name="idleConnectionTestPeriod" value="18000" />
<property name="checkoutTimeout" value="3000" />
</bean>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
<property name="packagesToScan" value="com.jjb.domain"></property>
<property name="persistenceUnitName" value="cml"></property>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="template" class="org.springframework.orm.jpa.JpaTemplate"
scope="prototype">
<property name="entityManagerFactory" ref="myEmf"></property>
</bean>
<!-- 配置事务 -->
<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>


<tx:annotation-driven transaction-manager="myTxManager" />

还需在src目录下建立META-INF/persistence.xml

具体配置如下:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="cml" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/bus" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>


</persistence>

使用时,需要使用JPATemplate进分页功能代码如下:

 this.getTemplate().execute(new JpaCallback<List<BusStation>>()
{

public List<BusStation> doInJpa(EntityManager manager)
throws PersistenceException
{
Query query = manager
.createQuery("from BusStation order by id desc");
query.setFirstResult(start);
query.setMaxResults(size);
return query.getResultList();
}
});




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值