-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme_en.md
746 lines (576 loc) · 16.8 KB
/
readme_en.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# EasyCommonDAO manual
EasyCommonDAO is a Java ORM Common `DAO(Data Access Object)` implementation, can eliminate the interface and implements of DAO.
## Latest Version
- Least version: `1.5.1-RELEASE`
- Test enviroment:
```
Hibernate 5.1.10(JPA 2.1)
Hibernate 4.3.11(JPA 2.1)
Hibernate 3.6.10(JPA 2.0)
EclipseLink 2.6.4(JPA 2.0+)
Spring 4.3.13
```
## Featuter
1. Provides common DAO implementation based on **Hibernate Native APIs** and **Java Persistence API (JPA)** (`Hiberante`, `EclipseLink`)
2. Eliminate the definition and implementation of DAO layer interface
3. Two implementation options: **pure ORM DAO implementation**, **Spring ORM DAO seamless integration implementation**
4. Easy to use, low learning costs, Hibernate and JPA use a unified interface specification
5. Provides paging objects (`PageBean`), conditional objects (`EasyCriteria`)
6. Provide cache query; cache management; batch data processing; cascade delete support ...
## EasyCommonDAO Architecture
![EasyCommonDAO](images/Architecture.png)
## Technical comparison
Compared to `Basic DAO` and `Spring Data JPA`, `EasyCommonDAO` does not need to define a DAO interface, nor does it need to learn method naming conventions. Reduce the cost of learning, and truly eliminate the DAO layer to achieve.
- **EasyCommonDAO**
![EasyCommonDAO](images/EasyCommonDAO.png)
- **Spring Data JPA**
![Spring Data JPA](images/SpringDataJPA.png)
- **Basic DAO**
![Basic DAO](images/BasicDAO.png)
## Maven
**Choice one EasyCommonDAO `artifactId`**
- Hibernate Native API
```
`easycommondao-hibernate5`
`easycommondao-hibernate4`
`easycommondao-hibernate3`
```
- JPA
```
`easycommondao-jpa-hibernate5`
`easycommondao-jpa-hibernate4`
`easycommondao-jpa-hibernate3`
`easycommondao-jpa-eclipselink`
```
```XML
<dependency>
<groupId>cn.easyproject</groupId>
<artifactId>${artifactId}</artifactId>
<version>1.5.1-RELEASE</version>
</dependency>
```
## Pure ORM use
1. Configure your ORM framework
- Configure Hibernate3/4/5: `hibernate.cfg.xml`
- Configure JPA(`Hibernate3/4/5`, `EclipseLink`): `persistence.xml`, set `name="EasyCommonDAO"`
```XML
<persistence-unit name="EasyCommonDAO" transaction-type="RESOURCE_LOCAL">
```
2. Write Service layer code, instance CommonDAO object, invoke CommonDAO API
```JAVA
public class UserServiceImpl implements UserService {
// EasyCommonDAO instance
public CommonDAO dao=new CommonDAOImpl();
public void get(int userId){
// Invoke ComonDAO API
return dao.find(User.class, userId);
}
// ...
}
```
## Spring integration with ORM
1. Configure Spring DAO, Generally including:
- `DataSource`
- `EntityManagerFactory`/`SessionFactory`
- `TransactionManager`
3. Configre Spring scann package `cn.easyproject`
```XML
<context:component-scan base-package="cn.easyproject"></context:component-scan>
```
2. Inject CommonDAO component to Service layer `@Resource(name="commonDAO")`, invoke CommonDAO API
```JAVA
public class UserServiceImpl implements UserService {
// Inject EasyCommonDAO instance
@Resource(name="commonDAO")
public CommonDAO dao;
public void get(int userId){
// Invoke ComonDAO API
return dao.find(User.class, userId);
}
// ...
}
```
## PageBean pagination
If you use **EclipseLink**, the select statement must be specified.
- Example
```JAVA
PageBean pb = new PageBean();
// FROM Clause; optional
// **If use EclipseLink is required!**
pb.setSelect(" select new com.company.ssh.entity.Account(ac.accountid, ac.qxname) ");
// FROM Clause Entity Name; rquired
pb.setEntityName("Account ac");
// Page Number; optional; default is 1
pb.setPageNo(1);
// Rows per page; optional; default is 10
pb.setRowsPerPage(4);
// WHERE Clause; optional; default is ''
pb.setCondition(" and ac.accountid>2");
// Append where clause condition; optional; default is ''
// pb.addCondition(" and name='A'");
// SortName; optional; default is ''
pb.setSort("ac.accountid");
// SortOrder; optional; default is 'asc'
pb.setSortOrder("desc");
// Other sorting methods; optional; default is ''
pb.setLastSort(",time desc");
// Execute pagination quries
commonDAO.findByPage(pb);
// Pagination data
System.out.println(pb.getData());
System.out.println(pb.getPageNo());
System.out.println(pb.getRowsPerPage());
System.out.println(pb.getRowsCount());
System.out.println(pb.getPageTotal());
```
- Example2
```JAVA
// Immediate use this query
// data sql
pb.setQuery("select ac from Account ac where ac.accountid>=10 and ac.accountid<1000");
// total sql
pb.setCountSQL("select count(1) from Account ac where ac.accountid>=10 and ac.accountid<1000");
// Page Number; optional; default is 1
pb.setPageNo(1);
// Rows per page; optional; default is 10
pb.setRowsPerPage(4);
// Execute pagination quries
commonDAO.findByPage(pb);
// Pagination data
System.out.println(pb.getData());
System.out.println(pb.getPageNo());
System.out.println(pb.getRowsPerPage());
System.out.println(pb.getRowsCount());
System.out.println(pb.getPageTotal());
```
## EasyCriteria query
1. New your EasyCriteria class, must **extends EasyCriteria implements Serializable**
2. Write your condition by **getCondition()**
- Non-JPA Project, use `?` index query parameters
```JAVA
public class SysUserCriteria extends EasyCriteria implements java.io.Serializable {
// 1. Criteria field
private String name;
private String realName;
private Integer status; // 0 is ON; 1 is OFF; 2 is REMOVED
// 2. Constructor
public SysUserCriteria() {
}
public SysUserCriteria(String name, String realName, Integer status) {
super();
this.name = name;
this.realName = realName;
this.status = status;
}
// 3. Condition genertator abstract method implements
public String getCondition() {
values.clear(); // **Must clear old values**
StringBuffer condition = new StringBuffer();
if (StringUtils.isNotNullAndEmpty(this.getName())) {
// Use ? index query parameters
condition.append(" and name like ?");
values.add("%" + this.getName() + "%");
}
if (StringUtils.isNotNullAndEmpty(this.getRealName())) {
condition.append(" and realName like ?");
values.add("%" + this.getRealName() + "%");
}
if (StringUtils.isNotNullAndEmpty(this.getStatus())) {
condition.append(" and status=?");
values.add(this.getStatus());
}
return condition.toString();
}
// 4. Setters&Getters...
}
```
- JPA Project, Use `:Named` named query parameters
> JAP 2.1 already don't support ? index query paramters
```JAVA
public class SysUserCriteria extends EasyCriteria implements java.io.Serializable {
// 1. 添加条件属性
private String name;
private String realName;
private Integer status; // 用户状态:0启用;1禁用;2删除
// 2. 生成构造方法
public SysUserCriteria() {
}
public SysUserCriteria(String name, String realName, Integer status) {
super();
this.name = name;
this.realName = realName;
this.status = status;
}
// 3. 条件生成抽象方法实现
public String getCondition() {
values.clear(); //清除条件数据
StringBuffer condition = new StringBuffer();
if (StringUtils.isNotNullAndEmpty(this.getName())) {
// Use :NAME named query parameters
condition.append(" and name like :name");
values.put("name", "%" + this.getName() + "%");
}
if (StringUtils.isNotNullAndEmpty(this.getRealName())) {
condition.append(" and realName like :realName");
values.put("realName", "%" + this.getRealName() + "%");
}
if (StringUtils.isNotNullAndEmpty(this.getStatus())) {
condition.append(" and status=:status");
values.add("status", this.getStatus());
}
return condition.toString();
}
// 4. Setters&Getters...
}
```
3. Find by EasyCriteria
```JAVA
PageBean pageBean = new PageBean();
pageBean.setEntityName("SysUser users");
pageBean.setSelect("select users");
// EasyCriteria
SysUserCriteria usersCriteria =new SysUserCriteria();
usersCriteria.setName("A");
usersCriteria.setStatus(0);
// Find by EasyCriteria
commonDAO.findByPage(pageBean, usersCriteria);
```
## CommonDAO API
```JAVA
/**
* SQL Batch
* @param sql SQL
* @param values values
*/
public void batchUpdateSQL(String sql, Object[] values);
/**
* SQL Batch
* @param sql SQL
* @param values values
*/
public void batchUpdateSQL(String sql, Object[][] values);
/**
* Remove persists object
*
* @param cls Class
* @param id OID
*/
public void remove(Class cls, Serializable id);
/**
*Remove persists object
*
* @param o Persists object
*/
public void remove(Object o);
/**
* Batch delete by query statement(not support cascade)
* @param cls Class
* @param fieldName fieldName
* @param values values
* @return affected rows count
*/
public Integer deleteByValues(Class cls, String fieldName, Object[] values);
/**
* Batch delete by query statement(support cascade)
* @param cls Class
* @param Field Name
* @param Values
*/
public void deleteCascadeByValues(Class cls, String fieldName, Object[] values);
/**
* Clear second cache
* @param cls Class
*/
public void evict(Class cls);
/**
* Clear second cache
* @param cls Class
* @param id OID
*/
public void evict(Class cls,Serializable id);
/**
* Clear second cache
*/
public void evictAll();
/**
* Clear second cache
* @param collectionRegion Region
*/
public void evictCollectionRegion(String collectionRegion);
/**
* Clear second cache
* @param collectionRegion Region
* @param id OID
*/
public void evictCollectionRegion(String collectionRegion,Serializable id);
/**
* Clear query cache
*/
public void evictQueries();
/**
* Clear query cache
* @param queryCacheRegion Region
*/
public void evictQueries(String queryCacheRegion);
/**
* Find
* @param cls Class
* @param id OID
* @return Object
*/
public <T> T find(Class cls, Serializable id);
/**
* Use jpql/hql query
* @param jpql jpql/hql
* @param easyCriteria EasyCriteria
* @return List
*/
public List find(String jpql,EasyCriteria easyCriteria);
/**
* Use jpql/hql query
* @param jpql Use jpql/hql query
* @param values parma values
* @return List
*/
public List find(String jpql, Object... values);
/**
* Find all
* @param cls Classls
* @return List
*/
public List findAll(Class cls);
/**
* Find by query cache
* @param jpql jpql/hql
* @param easyCriteria EasyCriteria
* @return List
*/
public List findByCache(String jpql, EasyCriteria easyCriteria);
/**
* Find by query cache
* @param jpql jpql/hql
* @param values parma values
* @return List
*/
public List findByCache(String jpql, Object... values);
/**
* Find by query cache
* @param jpql jpql/hql
* @param queryCacheRegion Region
* @param easyCriteria EasyCriteria
* @return List
*/
public List findByCache(String jpql, String queryCacheRegion, EasyCriteria easyCriteria);
/**
* Find by query cache
* @param jpql jpql/hql
* @param queryCacheRegion Region
* @param values parma values
* @return List
*/
public List findByCache(String jpql, String queryCacheRegion, Object... values);
/**
* Pagination query
* @param pageBean pageBean
*/
public void findByPage(PageBean pageBean);
/**
* Pagination query
* @param pageBean pageBean
* @param easyCriteria EasyCriteria
*/
public void findByPage(PageBean pageBean,EasyCriteria easyCriteria);
/**
* Pagination query
* @param pageBean pageBean
* @param values parma values
*/
public void findByPage(PageBean pageBean,List values);
/**
* Find by propertyName
* @param cls Classls
* @param propertyName propertyName
* @param value value
* @return List
*/
public List findByProperty(Class cls, String propertyName, Object value);
/**
* Find by property ignore case
* @param cls Classls
* @param propertyName Property name
* @param value value
* @return List
*/
public List findByPropertyIgnoreCase(Class cls, String propertyName, String value);
/**
* Query by Native SQL, return define result class
* @param sql SQL
* @param resultClassResult Result class
* @param easyCriteria EasyCriteria
* @return List<resultClass>
*/
public List findBySQL(String sql, Class resultClass, EasyCriteria easyCriteria);
/**
* Query by Native SQL, return define result class
* @param sql SQL
* @param resultClassResult class
* @param values parma values
* @return List<resultClass>
*/
public List findBySQL(String sql, Class resultClass, Object... values);
/**
* Query by Native SQL, return Object[]
* @param sql SQL
* @param easyCriteria EasyCriteria
* @return List<Object[]>
*/
public List findBySQL(String sql,EasyCriteria easyCriteria);
/**
* Query by Native SQL, return Object[]
* @param sql SQL
* @param values parma values
* @return List<Object[]>
*/
public List findBySQL(String sql,Object... values);
/**
* Find count by jqpl/hql
* @param jpql jpql/hql
* @param easyCriteria EasyCriteria
* @return count
*/
public int findCount(String jpql, EasyCriteria easyCriteria);
/**
* Find count by jqpl/hql
* @param jpql jpql/hql
* @param values parma values
* @return count
*/
public int findCount(String jpql, Object... values);
/**
* Query by Native SQL, return Map result
* @param sql SQL
* @param easyCriteria EasyCriteria
* @return List<Map>
*/
public List findMapResultBySQL(String sql,EasyCriteria easyCriteria);
/**
* Query by Native SQL, return Map result
* @param sql SQL
* @param values parma values
* @return List<Map>
*/
public List findMapResultBySQL(String sql, Object... values);
/**
* Find max page by jpql/hql
* @param jpql jpql/hql
* @param rowsPerPage Rows per page
* @param easyCriteria EasyCriteria
* @return Max page
*/
public int findMaxPage(String jpql, int rowsPerPage, EasyCriteria easyCriteria);
/**
* Find max page by jpql/hql
* @param jpql jpql/hql
* @param rowsPerPage Rows per page
* @param values parma values
* @return Max page
*/
public int findMaxPage(String jpql, int rowsPerPage, Object... values);
/**
* Query by Named Query
* @param name queryName
* @param values parma values
* @return List
*/
public List findNamedQuery(String name, Object... values) ;
/**
* Query by Named Query
* @param name queryName
* @param queryCacheRegion Region
* @param values parma values
* @return List
*/
public List findNamedQueryByCache(String name, String queryCacheRegion, Object... values) ;
/**
* Find top N
* @param jpql jpql/hql
* @param topCount count
* @param easyCriteria EasyCriteria
* @return List
*/
public List findTop(String jpql,int topCount, EasyCriteria easyCriteria);
/**
* Find top N
* @param jpql jpql/hql
* @param topCount count
* @param values parma values
* @return List
*/
public List findTop(String jpql,int topCount, Object... values);
/**
* Find scalar value
* @param jpql jpql/hql
* @param easyCriteria EasyCriteria
* @return Object
*/
public <T> T findVal(String jpql, EasyCriteria easyCriteria);
/**
* Find scalar value
* @param jpql jpql/hql
* @param values values
* @return Object
*/
public <T> T findVal(String jpql, Object... values);
/**
* Get current EntityManager
* @return EntityManager
*/
public EntityManager getCurrentEntityManager();
/**
* Get current EntityManagerFactory
* @return EntityManagerFactory
*/
public EntityManagerFactory getEntityManagerFactory();
/**
* Initialize Object
* @param proxy Object
*/
public void initialize(Object proxy) ;
/**
* Initialize Object form collection
* @param collection Collection
*/
public void initializeDeep(Collection collection);
/**
* Update
* @param o Entity
*/
public void merge(Object o);
/**
* Save
* @param o Entity
*/
public void persist(Object o);
/**
* Batch update by jqpl/hql
* @param jpql jpql/hql
* @param values parma values
*/
public void updateByJpql(String jpql,Object... values);
/**
* Execute update/delete/insert by JDBC SQL
* @param sql SQL
* @param values parma values
*/
public void updateBySQL(String sql,Object...values);
```
## End
[English Readme](doc/readme_en.md)
[The official home page](http://www.easyproject.cn/easycommondao/en/index.jsp 'The official home page')
[Comments](http://www.easyproject.cn/easycommondao/en/index.jsp#donation 'Comments')
If you have more comments, suggestions or ideas, please contact me.
Contact, Feedback, Custom, Train Email:<inthinkcolor@gmail.com>
[http://www.easyproject.cn](http://www.easyproject.cn "EasyProject Home")
We believe that the contribution of each bit by bit, will be driven to produce more and better free and open source products a big step.
**Thank you donation to support the server running and encourage more community members.**
[![PayPal](http://www.easyproject.cn/images/paypaldonation5.jpg)](https://www.paypal.me/easyproject/10 "Make payments with PayPal - it's fast, free and secure!")