DAOTemplate.ftl
31.3 KB
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//${tableName}
<#--包名 -->
package ${packages}
<#-- 所有的引入-->
<#list imports as ilist>
<#if ilist?exists>${ilist}<#else><#rt></#if>
</#list>
import java.math.BigDecimal;
/**
<#if version?exists>
* @version ${version}
</#if>
*/
@Repository
<#--类, 还是接口 名称, 继承的类, 实现的接口 -->
public class ${beanName}Dao {
<#--变量 -->
@Resource
private JdbcTemplate jdbcegroceryRead;
@Resource
private JdbcTemplate jdbcegroceryWrite;
Log logger = LogFactory.getLog(this.getClass());
/**
* 根据Request Map 生成PO对象
*/
public ${beanName}PO get${beanName}POFromRequest(Map<String, Object> map) throws Exception {
${beanName}PO po = new ${beanName}PO();
<#if reqParameterToPOMap?exists>
<#list reqParameterToPOMap?keys as poKey>
po.set${reqParameterToPOMap[poKey]};
</#list>
</#if>
return po;
}
/**
* 按主键查询
*/
public ${beanName}PO findPOByID(${pkType} id) {
return findPOByID(id, true, false);
}
/**
* 按主键查询
* isLock 是否锁定, 默认不锁
* fromWriteDB 是否从写库读写,默认从读库查询
*/
public ${beanName}PO findPOByID(${pkType} id, boolean isLock, boolean fromWriteDB) {
Date starttime = new Date();
StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}");
StringBuffer pql = new StringBuffer(sql.toString());
sql.append(" WHERE ${pkColumName} = ?");
pql.append(" WHERE ${pkColumName} = " + id);
if (isLock) {
sql.append(" FOR UPDATE");
pql.append(" FOR UPDATE");
}
List<${beanName}PO> resultList = null;
try {
resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), id);
if (resultList == null || resultList.size() == 0) {
return null;
}
return resultList.get(0);
} catch (Exception e) {
LogUtils.error("error:findPOByID", logger, e, pql.toString(), id);
} finally {
Date endtime = new Date();
if(false)LogUtils.info("info:", logger,
"${beanName}Dao.findPOByID(" + id + ", " + isLock + ", " + ", " + fromWriteDB + ")\n"
+ "返回数据条数:" + (resultList==null?0:resultList.size())
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return null;
}
/**
* 根据条件List<Object[]>查询
* Object[]数组长度是3
* Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public ${beanName}PO findPOByCondition(List<Object[]> condition) {
List<${beanName}PO> tempList = findListByCondition(condition,null, false);
if(tempList == null || tempList.size() == 0){
return null;
}else{
return tempList.get(0);
}
}
/**
* 根据条件sql查询
* sqlCondition 为where 后面的条件。
*/
public ${beanName}PO findPOBySql(String sqlCondition) {
List<${beanName}PO> tempList = findListBySql(sqlCondition, false);
if(tempList == null || tempList.size() == 0){
return null;
}else{
return tempList.get(0);
}
}
/**
* 根据条件List<Object[]>查询
* Object[]数组长度是3
* Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public List<${beanName}PO> findListByCondition(List<Object[]> condition) {
return findListByCondition(condition,null, false);
}
/**
* 根据条件List<Object[]>查询
* Object[]数组长度是3
* Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public List<${beanName}PO> findListByCondition(List<Object[]> condition,String sortCondition, boolean fromWriteDB) {
if (StringUtil.isAutowired(condition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return null;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
JdbcUtil.appendWhereCondition(sql, pql, list, condition);
if(!StringUtils.isEmpty(sortCondition)){
sql.append(" " + sortCondition + " ");
pql.append(" " + sortCondition + " ");
}
List<${beanName}PO> resultList = null;
try {
resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), list.toArray());
return resultList;
} catch (Exception e) {
LogUtils.error("error:", logger, e, pql.toString(), null);
} finally {
Date endtime = new Date();
if(false)LogUtils.info("info", logger,
"${beanName}Dao.findListByCondition(" + condition.toString() + ", " + fromWriteDB + ")\n"
+ "fromWriteDB:" + fromWriteDB
+ "执行SQL:" + pql.toString() + "\n"
+ "返回条数:" + (resultList==null?0:resultList.size()) + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return null;
}
/**
* 根据条件sql查询
* sqlCondition 为where 后面的条件。
*/
public List<${beanName}PO> findListBySql(String sqlCondition) {
return findListBySql(sqlCondition, false);
}
/**
* 根据条件sql查询
* sqlCondition 为where 后面的条件。
*/
public List<${beanName}PO> findListBySql(String sqlCondition,boolean fromWriteDB) {
if (StringUtil.isAutowired(sqlCondition)) {
LogUtils.error("error", logger, new Exception("请求条件异常,无效请求!"), null, null);
return null;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition);
List<${beanName}PO> resultList = null;
try {
resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class));
return resultList;
} catch (Exception e) {
LogUtils.error("error:", logger, e, sql.toString(), null);
} finally {
Date endtime = new Date();
if(false)LogUtils.info("info", logger,
"${beanName}Dao.findListBySql(" + sqlCondition + ", " + fromWriteDB + ")\n"
+ "fromWriteDB:" + fromWriteDB + "\n"
+ "执行SQL:" + sql.toString() + "\n"
+ "返回条数:" + (resultList==null?0:resultList.size()) + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return null;
}
/**
* 按条件分页查询
* Object[]数组长度是3
* Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public Map<String, Object> findPageByCondition(List<Object[]> condition,int page, int pageSize) {
return findPageByCondition(condition, null , page, pageSize, false);
}
/**
* 按条件分页查询
* Object[]数组长度是3
* Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
* boolean isUseCache, 是否用缓存,默认用。
* boolean isAddCache, 是否添加缓存,默认添加。
*/
public Map<String, Object> findPageByCondition(List<Object[]> condition,String sortCondition, int page, int pageSize, boolean fromWriteDB) {
if (StringUtil.isAutowired(condition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return null;
}
Date starttime = new Date();
<#if dbType = "mysql">
StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}");
StringBuffer pql = new StringBuffer(sql.toString());
<#else>
StringBuffer sql = new StringBuffer("SELECT ${column_list_str}, ROWNUM RN FROM ${tableName}");
StringBuffer pql = new StringBuffer(sql.toString());
</#if>
StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) FROM ${tableName}");
List<Object> count_list = new ArrayList<Object>();
List<Object> page_list = new ArrayList<Object>();
JdbcUtil.appendWhereConditionForCount(sqlCount, condition);
JdbcUtil.appendWhereCondition(sql, pql, count_list, condition);
for (int i = 0; i < count_list.size(); i++)
page_list.add(count_list.get(i));
<#if dbType == "mysql">
page_list.add((page - 1) * pageSize);
page_list.add(pageSize);
if(!StringUtils.isEmpty(sortCondition)){
sql.append(" " + sortCondition + " ");
pql.append(" " + sortCondition + " ");
}
String pageSql = sql.toString() + " limit ?, ?";
String pagePql = pql.toString() + " limit " + (page -1) * pageSize + ", " + pageSize;
<#else>
String pageSql = null;
String pagePql = null;
if (page_list.size() == 0) {
pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?";
pagePql = "SELECT * FROM (" + pql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1);
} else {
pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?";
pagePql = "SELECT * FROM (" + pql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1);
}
page_list.add(page * pageSize);
page_list.add((page - 1) * pageSize + 1);
</#if>
List<${beanName}PO> resultList = null;
try {
int totalPages = 0;
int totalRows = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).queryForInt(sqlCount.toString(), count_list.toArray()) ;
if (totalRows % pageSize == 0) {
totalPages = totalRows / pageSize;
} else {
totalPages = (totalRows / pageSize) + 1;
}
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("page", page);
resultMap.put("total", totalRows);
resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query( pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray());
resultMap.put("rows", resultList);
return resultMap;
} catch (Exception e) {
LogUtils.error("error:", logger, e, pagePql.toString(), page_list.toArray());
} finally {
Date endtime = new Date();
if(false)LogUtils.info("info:", logger,
"${beanName}Dao.findPageByCondition(" + condition.toString() + ", " + page + ", " + pageSize + ", " + fromWriteDB + ")\n"
+ "执行SQL:" + pagePql.toString() + "\n"
+ "返回条数:" + (resultList==null?0:resultList.size()) + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return null;
}
/**
* 按sql分页查询, sqlCondition为where 后条件sql
*/
public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize) {
return findPageBySql(sqlCondition, page, pageSize, false);
}
/**
* 按sql分页查询, sqlCondition为where 后条件sql
*/
public Map<String, Object> findPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB) {
if (StringUtil.isAutowired(sqlCondition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return null;
}
Date starttime = new Date();
<#if dbType = "mysql">
StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition );
StringBuffer sqlCount = new StringBuffer("SELECT count(1) from ${tableName} WHERE " + sqlCondition);
String pageSql = sql.toString() + " limit ?, ?";
String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize);
List<Object> page_list = new ArrayList<Object>();
page_list.add((page - 1) * pageSize);
page_list.add(page * pageSize);
<#else>
StringBuffer sql = new StringBuffer("SELECT ${column_list_str}, ROWNUM RN FROM ${tableName} WHERE " + sqlCondition);
StringBuffer sqlCount = new StringBuffer("SELECT count(1) from ${tableName} WHERE " + sqlCondition);
String pageSql = null;
String pagePql = null;
pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?";
pagePql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1);
List<Object> page_list = new ArrayList<Object>();
page_list.add(page * pageSize);
page_list.add((page - 1) * pageSize + 1);
</#if>
List<${beanName}PO> resultList = null;
try {
int totalPages = 0;
int totalRows = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).queryForInt(sqlCount.toString());
if (totalRows % pageSize == 0) {
totalPages = totalRows / pageSize;
} else {
totalPages = (totalRows / pageSize) + 1;
}
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("page", page);
resultMap.put("total", totalRows);
resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray());
resultMap.put("rows", resultList);
return resultMap;
} catch (Exception e) {
LogUtils.error("error:", logger, e, pagePql.toString(), null);
} finally {
Date endtime = new Date();
if(false)LogUtils.info("info:", logger,
"${beanName}Dao.findPageBySql(" + sqlCondition + ", " + page + ", " + pageSize + ", " + fromWriteDB + ")\n"
+ "fromWriteDB:" +fromWriteDB + "\n"
+ "执行SQL:" + pagePql.toString() + "\n"
+ "返回条数:" + (resultList==null?0:resultList.size()) + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return null;
}
/**
* 添加
*/
public int add${beanName}PO(${beanName}PO ${beanName}) {
if (null == ${beanName}) {
LogUtils.error("error:", logger, new Exception("请求条件异常,${beanName}不能为空!"), null, null);
return -1;
}
Date starttime = new Date();
<#--
if (${beanName}.get${pkBeanName}() == null) {
${beanName}.set${pkBeanName}(this.find${beanName}POSeqNextVal());
}
-->
<#if columnList?exists>
<#list columnList as clist>
<#if clist.seqName?exists>
if (${beanName}.get${pkBeanName}() == null) {
${beanName}.set${clist.columnUName}(this.find${beanName}POSeqNextVal());
}
</#if>
</#list>
</#if>
StringBuffer sql = new StringBuffer("INSERT INTO ${tableName}(" + "${column_list_str}" + ") VALUES (");
StringBuffer sqlI = new StringBuffer("INSERT INTO ${tableName}(");
StringBuffer sqlC = new StringBuffer(") VALUES (");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
<#if columnList?exists>
<#list columnList as clist>
if(${beanName}.get${clist.columnUName}() != null){
sqlI.append("${clist.columnName},");
sqlC.append(" ?, ");
pql.append(${beanName}.get${clist.columnUName}() + ", ");
list.add(${beanName}.get${clist.columnUName}());
}
</#list>
</#if>
int count = 0;
try {
jdbcegroceryWrite.update(StringUtil.removeLast(StringUtil.removeLast(sqlI.toString())+sqlC.toString()) + ")", list.toArray());
count = jdbcegroceryWrite.queryForInt("SELECT LAST_INSERT_ID()");
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + ")", null);
} finally {
Date endtime = new Date();
LogUtils.info("info:", logger,
"${beanName}Dao.add${beanName}PO (${beanName}PO ${beanName})\n"
+ "执行SQL:" + StringUtil.removeLast(pql.toString()) + ")" + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 批量添加
*/
public int add${beanName}POList(List<${beanName}PO> ${beanName}list) {
Date starttime = new Date();
int count = 0;
for (${beanName}PO ${beanName} : ${beanName}list) {
count += add${beanName}PO(${beanName});
}
Date endtime = new Date();
LogUtils.info("info:", logger,
"${beanName}Dao.add${beanName}POList(List<${beanName}PO> ${beanName}list)\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
return count;
}
/**
* 按ID删除
*/
public int delete${beanName}POByID(${pkType} id) {
Date starttime = new Date();
StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE");
StringBuffer pql = new StringBuffer(sql.toString());
pql.append(" ${pkColumName} = " + id);
sql.append(" ${pkColumName} = ?");
int count = 0;
try {
count = jdbcegroceryWrite.update(sql.toString(), id);
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, pql.toString(), null);
} finally {
Date endtime = new Date();
LogUtils.info("info:", logger,
"${beanName}Dao.delete${beanName}POByID(${pkType} id)\n"
+ "执行SQL:" + pql.toString() + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 删除按List<Object[]>条件
* Object[]数组长度是3
* Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public int delete${beanName}POByCondition(List<Object[]> condition) {
if (null == condition || condition.size() == 0) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List<Object[]> condition不能为空!"), null, null);
return -1;
}
if (StringUtil.isAutowired(condition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return -1;
}
Date starttime = new Date();
List<Object> list = new ArrayList<Object>();
StringBuffer sql = new StringBuffer("DELETE FROM ${tableName}");
StringBuffer pql = new StringBuffer(sql.toString());
JdbcUtil.appendWhereCondition(sql, pql, list, condition);
int count = 0;
try {
count = jdbcegroceryWrite.update( sql.toString(), list.toArray());
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, pql.toString(), null);
} finally {
Date endtime = new Date();
LogUtils.info("cacheinfo:", logger,
"${beanName}Dao.delete${beanName}POByCondition(List<Object[]> condition)\n"
+ "执行SQL:" + pql.toString() + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 删除按condition条件
* 建议使用delete${beanName}POByCondition(List<Object[]> condition), 如果remove${beanName}POByCondition(List<Object[]> condition)满足不了where条件可以使用此方法。
* condition为where后面的条件,condition不能为空。
*/
public int delete${beanName}POBySql(String sqlCondition) {
if (StringUtil.isEmpty(sqlCondition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件String sqlCondition不能为空!"), null, null);
return -1;
}
if (StringUtil.isAutowired(sqlCondition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return -1;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE ");
int count = 0;
try {
count = jdbcegroceryWrite.update( sql.toString() + sqlCondition);
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, sql.toString() + sqlCondition, null);
} finally {
Date endtime = new Date();
LogUtils.info("info", logger,
"${beanName}Dao.delete${beanName}POBySql(String sqlCondition)\n"
+ "执行SQL:" + sql.toString() + sqlCondition + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 根据list对象逐个删除。
*/
public int delete${beanName}POList(List<${beanName}PO> ${beanName}list) {
int count = 0;
for (${beanName}PO ${beanName} : ${beanName}list) {
count += delete${beanName}POByID(${beanName}.get${pkBeanName}());
}
return count;
}
/**
* 根据对象ID修改。
*/
public int update${beanName}PO(${beanName}PO ${beanName}) {
if (null == ${beanName} || null == ${beanName}.get${pkBeanName}()) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求对象和对象ID不能为空!"), null, null);
return -1;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
<#if columnList?exists>
<#list columnList as clist>
<#if clist.seqName?exists>
<#else>
<#if clist.columnJAVAType = "Long" || clist.columnJAVAType = "Integer" || clist.columnJAVAType = "Short" || clist.columnJAVAType = "Float" || clist.columnJAVAType = "Double">
boolean ${clist.columnUName}IsZero = false;
Object variation${clist.columnUName} = ${beanName}.getVariation${clist.columnUName}();
if (variation${clist.columnUName} instanceof BigDecimal) {
${clist.columnUName}IsZero = (((BigDecimal)variation${clist.columnUName}).compareTo(BigDecimal.ZERO) == 0);
} else {
Long l${clist.columnUName} = Long.parseLong(String.valueOf(variation${clist.columnUName}));
${clist.columnUName}IsZero = (l${clist.columnUName} == 0);
}
if (!${clist.columnUName}IsZero) {
sql.append(" ${clist.columnName} = ${clist.columnName} + ?, ");
pql.append(" ${clist.columnName} = ${clist.columnName} + " + ${beanName}.getVariation${clist.columnUName}() + ",");
list.add(${beanName}.getVariation${clist.columnUName}());
} else {
sql.append(" ${clist.columnName} = ?, ");
pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ",");
list.add(${beanName}.get${clist.columnUName}());
}
<#elseif clist.columnJAVAType = "java.math.BigDecimal">
if (${beanName}.getVariation${clist.columnUName}().compareTo(BigDecimal.ZERO) != 0) {
sql.append(" ${clist.columnName} = ${clist.columnName} + ?, ");
pql.append(" ${clist.columnName} = ${clist.columnName} + " + ${beanName}.getVariation${clist.columnUName}() + ",");
list.add(${beanName}.getVariation${clist.columnUName}());
} else {
sql.append(" ${clist.columnName} = ?, ");
pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ",");
list.add(${beanName}.get${clist.columnUName}());
}
<#else>
sql.append(" ${clist.columnName} = ?, ");
pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ",");
list.add(${beanName}.get${clist.columnUName}());
</#if>
</#if>
</#list>
</#if>
String where = " WHERE ${pkColumName} = ?";
String pwhere = " WHERE ${pkColumName} = " + ${beanName}.get${pkBeanName}();
list.add(${beanName}.get${pkBeanName}());
int count = 0;
try {
count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + where, list.toArray());
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + pwhere, null);
} finally {
Date endtime = new Date();
LogUtils.info("info", logger,
"${beanName}Dao.update${beanName}PO(${beanName}PO ${beanName})\n"
+ "执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 根据ID修改指定的值
*/
public int update${beanName}FieldById(List<Object[]> changeList, ${pkType} id) {
if (null == id) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求对象和对象ID不能为空!"), null, null);
return -1;
}
Date starttime =new Date();
StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
Map<Object,Object> map = new HashMap<Object,Object>();
for (int i = 0, iLen = changeList.size(); i < iLen; i++) {
String name = (String) changeList.get(i)[0];
Object value = changeList.get(i)[1];
String variationOpt = "=";
if (changeList.get(i).length > 2)
variationOpt = (String)changeList.get(i)[2];
if (variationOpt.equals("=")) {
sql.append(" " + name + " = ?,");
list.add(value);
if (value == null) {
pql.append(" " + name + "=null,");
} else {
pql.append(" " + name + "=\"" + value.toString() + "\",");
}
map.put(name, value);
} else {
sql.append(" " + name + " = " + name + " + ?,");
list.add(value);
pql.append(" " + name + " = " + name + " + " + value.toString() + ",");
<#--会有问题 暂留
map.put(name, value);-->
}
}
String where = " WHERE ${pkColumName}=?";
String pwhere = " WHERE ${pkColumName}=\"" + id + "\"";
list.add(id);
int count = 0;
try {
count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + where, list.toArray());
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + pwhere, null);
} finally {
Date endtime = new Date();
LogUtils.info("info", logger,
"${beanName}Dao.update${beanName}PO(List<Object[]> changeList, ${pkType} id)\n"
+ "执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* 批量修改。
*/
public int update${beanName}POList(List<${beanName}PO> ${beanName}list) {
int count = 0;
for (${beanName}PO ${beanName} : ${beanName}list) {
count += update${beanName}PO(${beanName});
}
return count;
}
/**
* List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。
* List<Object[]> condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。
*/
public int update${beanName}POByCondition(List<Object[]> updateObj, List<Object[]> condition) {
if (null == updateObj || updateObj.size() == 0) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"), null, null);
return -1;
}
if (null == condition || condition.size() == 0) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List<Object[]> condition不能为空!"), null, null);
return -1;
}
if (StringUtil.isAutowired(condition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return -1;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
JdbcUtil.appendSql(sql, pql, list, updateObj);
StringBuffer where = new StringBuffer("");
StringBuffer pwhere = new StringBuffer("");
JdbcUtil.appendWhereCondition(where, pwhere, list, condition);
int count = 0;
try {
count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + where.toString(), list.toArray());
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString())+pwhere.toString(), null, null);
} finally {
Date endtime = new Date();
LogUtils.info("info", logger,
"${beanName}Dao.update${beanName}POByCondition(List<Object[]> updateObj, List<Object[]> condition)\n"
+ "执行SQL:" + StringUtil.removeLast(pql.toString())+pwhere.toString() + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
/**
* List<Object[]> updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。
* String sqlCondition 修改的条件。
*/
public int update${beanName}POBySql(List<Object[]> updateObj, String sqlCondition) {
if (null == updateObj || updateObj.size() == 0) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"), null, null);
return -1;
}
if (StringUtil.isEmpty(sqlCondition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件sqlCondition不能为空!"), null, null);
return -1;
}
if (StringUtil.isAutowired(sqlCondition)) {
LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null);
return -1;
}
Date starttime = new Date();
StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET");
StringBuffer pql = new StringBuffer(sql.toString());
List<Object> list = new ArrayList<Object>();
JdbcUtil.appendSql(sql, pql, list, updateObj);
int count = 0;
try {
count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + " WHERE "+sqlCondition, list.toArray());
return count;
} catch (Exception e) {
LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition, null, null);
} finally {
Date endtime = new Date();
LogUtils.info("info", logger,
"${beanName}Dao.update${beanName}POBySql(List<Object[]> updateObj, String sqlCondition)\n"
+ "执行SQL:" + StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition + "\n"
+ "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "("
+ StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS")
+ "*-*"
+ StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS")
+ ")");
}
return -1;
}
}