http://blog.csdn.net/wangshfa/article/details/27323297
1 |
public interface BatchDao<T> { |
2 |
public void batchInsert(List<T> list); |
4 |
public void batchUpdate(List<T> list); |
1 |
public interface ReceiptDao extends PagingAndSortingRepository<Receipt, Long>, BatchDao<Receipt> { |
4 |
Page<Receipt> findByUserId(Long userId, Pageable pageable); |
01 |
import java.util.List; |
03 |
import javax.persistence.EntityManager; |
04 |
import javax.persistence.PersistenceContext; |
06 |
import org.hibernate.Query; |
07 |
import org.hibernate.ScrollableResults; |
08 |
import org.hibernate.Session; |
09 |
import org.springframework.transaction.annotation.Transactional; |
11 |
import com.ygsoft.cxpt.dao.BatchDao; |
12 |
import com.ygsoft.util.dwz.Page; |
16 |
* @author <a href="mailto:ketayao@gmail.com">ketayao</a> Version 1.1.0 |
17 |
* @since 2012-8-27 上午10:55:41 |
20 |
public abstract class AbstractDao implements BatchDao{ |
23 |
protected EntityManager em; |
26 |
public void batchInsert(List list) { |
27 |
for ( int i = 0 ; i < list.size(); i++) { |
28 |
em.persist(list.get(i)); |
37 |
public void batchUpdate(List list) { |
38 |
for ( int i = 0 ; i < list.size(); i++) { |
39 |
em.merge(list.get(i)); |
48 |
* Hibernate使用游标分页的一个通用查询分页方法 |
55 |
@SuppressWarnings ( "unchecked" ) |
56 |
public List findPageByQuery( final String queryString, |
57 |
final Object[] parameters, final Page page) { |
59 |
Session session = (Session) em.getDelegate(); |
61 |
Query query = session.createQuery(queryString); |
64 |
if (parameters != null ) { |
65 |
for ( int i = 0 ; i < parameters.length; i++) { |
66 |
query.setParameter(i, parameters[i]); |
71 |
ScrollableResults sr = query.scroll(); |
73 |
int totalCount = sr.getRowNumber(); |
76 |
int totalRec = totalCount + 1 ; |
77 |
page.setTotalCount(totalRec); |
79 |
int startIndex = (page.getPageNum() - 1 ) * page.getNumPerPage(); |
81 |
query.setFirstResult(startIndex); |
82 |
query.setMaxResults(page.getNumPerPage()); |
84 |
List reList = query.list(); |
01 |
import com.ygsoft.cxpt.entity.FmisItem; |
05 |
* @author <a href="mailto:ketayao@gmail.com">ketayao</a> |
07 |
* @since 2012-9-7 下午5:28:58 |
10 |
public class FmisItemDaoImpl extends AbstractDao<FmisItem> { |