1 |
public interface BatchDao<T> {
|
2 |
public void batchInsert(List<T> list);
|
3 |
|
4 |
public void batchUpdate(List<T> list);
|
5 |
} |
1 |
public interface ReceiptDao extends PagingAndSortingRepository<Receipt, Long>, BatchDao<Receipt> {
|
2 |
|
3 |
// start回单录入
|
4 |
Page<Receipt> findByUserId(Long userId, Pageable pageable);
|
5 |
6 |
} |
01 |
import java.util.List;
|
02 |
03 |
import javax.persistence.EntityManager;
|
04 |
import javax.persistence.PersistenceContext;
|
05 |
06 |
import org.hibernate.Query;
|
07 |
import org.hibernate.ScrollableResults;
|
08 |
import org.hibernate.Session;
|
09 |
import org.springframework.transaction.annotation.Transactional;
|
10 |
11 |
import com.ygsoft.cxpt.dao.BatchDao;
|
12 |
import com.ygsoft.util.dwz.Page;
|
13 |
14 |
/** |
15 |
*
|
16 |
* @author <a href="mailto:ketayao@gmail.com">ketayao</a> Version 1.1.0
|
17 |
* @since 2012-8-27 上午10:55:41
|
18 |
*/
|
19 |
20 |
public abstract class AbstractDao implements BatchDao{
|
21 |
22 |
@PersistenceContext
|
23 |
protected EntityManager em;
|
24 |
25 |
@Transactional
|
26 |
public void batchInsert(List list) {
|
27 |
for (int i = 0; i < list.size(); i++) {
|
28 |
em.persist(list.get(i));
|
29 |
if (i % 30 == 0) {
|
30 |
em.flush();
|
31 |
em.clear();
|
32 |
}
|
33 |
}
|
34 |
}
|
35 |
|
36 |
@Transactional
|
37 |
public void batchUpdate(List list) {
|
38 |
for (int i = 0; i < list.size(); i++) {
|
39 |
em.merge(list.get(i));
|
40 |
if (i % 30 == 0) {
|
41 |
em.flush();
|
42 |
em.clear();
|
43 |
}
|
44 |
}
|
45 |
}
|
46 |
47 |
/**
|
48 |
* Hibernate使用游标分页的一个通用查询分页方法
|
49 |
* 描述
|
50 |
* @param queryString
|
51 |
* @param parameters
|
52 |
* @param page
|
53 |
* @return
|
54 |
*/
|
55 |
@SuppressWarnings("unchecked")
|
56 |
public List findPageByQuery(final String queryString,
|
57 |
final Object[] parameters, final Page page) {
|
58 |
59 |
Session session = (Session) em.getDelegate();
|
60 |
61 |
Query query = session.createQuery(queryString);
|
62 |
63 |
// 判断有无条件参数的情况
|
64 |
if (parameters != null) {
|
65 |
for (int i = 0; i < parameters.length; i++) {
|
66 |
query.setParameter(i, parameters[i]);
|
67 |
}
|
68 |
}
|
69 |
70 |
// 使用游标来得到总条数
|
71 |
ScrollableResults sr = query.scroll();
|
72 |
sr.last();
|
73 |
int totalCount = sr.getRowNumber();
|
74 |
75 |
// 索引加1
|
76 |
int totalRec = totalCount + 1;
|
77 |
page.setTotalCount(totalRec);
|
78 |
79 |
int startIndex = (page.getPageNum() - 1) * page.getNumPerPage();
|
80 |
81 |
query.setFirstResult(startIndex);
|
82 |
query.setMaxResults(page.getNumPerPage());
|
83 |
84 |
List reList = query.list();
|
85 |
86 |
return reList;
|
87 |
}
|
88 |
} |
01 |
import com.ygsoft.cxpt.entity.FmisItem;
|
02 |
03 |
/** |
04 |
*
|
05 |
* @author <a href="mailto:ketayao@gmail.com">ketayao</a>
|
06 |
* Version 1.1.0
|
07 |
* @since 2012-9-7 下午5:28:58
|
08 |
*/
|
09 |
@Repository |
10 |
public class FmisItemDaoImpl extends AbstractDao<FmisItem> {
|
11 |
12 |
} |