cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.core.dao;
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
 
import cn.ksource.core.page.PageBean;
import cn.ksource.core.page.PageInfo;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.StringUtil;
import cn.ksource.core.web.SplitPage;
 
 
 
public class BaseDao {
    
    private JdbcTemplate jdbcTemplate;
 
    private NamedParameterJdbcTemplate paraJdbcTemplate;
    
 
    /**
     * GG_USER user = baseDao.queryForBean("select * from GG_USER where id=:id", new GG_USER().setId('aasaaa'));
     * @param <T>
     * @param sql
     * @param bean
     * @return
     */
    public <T> T queryForBean(String sql,BaseBean bean) {
        List<T> list = this.paraJdbcTemplate.query(sql, bean.getBeanValues(), bean);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
    
    
    /**
     * 
     * GG_USER user = baseDao.queryForBean("select * from GG_USER where id=:id",
                new SqlParameter("id","1dcd7d8483c2434dabbb142a76003df9"),
                new GG_USER());
     * @param <T>
     * @param sql
     * @param param
     * @param bean
     * @return
     */
    public <T> T queryForBean(String sql,Map param,BaseBean bean) {
        List<T> list = this.paraJdbcTemplate.query(sql, param, bean);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
    
    /**
     * 只提供了对bean中set方法的注入
     * @param bean
     * @param map
     * @return
     */
    private BaseBean map2Bean(BaseBean bean,Map map){
        Iterator iterator = map.keySet().iterator();
        Class<?> c = bean.getClass();
        Method[] methods = c.getDeclaredMethods();
        try {
            while (iterator.hasNext()) {
                String key = iterator.next().toString();
                for (Method method : methods) {
                    if (StringUtils.equals(method.getName().toLowerCase(), "set"+key.toLowerCase())) {
                        String parameterType = method.getParameterTypes()[0].getSimpleName();
                        if (StringUtils.equals("String", parameterType)) {
                            method.invoke(bean, ConvertUtil.obj2Str(map.get(key)));
                        } else if (StringUtils.equals("Integer", parameterType)) {
                            method.invoke(bean, ConvertUtil.obj2Integer(map.get(key)));
                        } else if (StringUtils.equals("Long", parameterType)) {
                            method.invoke(bean, ConvertUtil.obj2Long(map.get(key)));
                        } else if (StringUtils.equals("Double", parameterType)) {
                            method.invoke(bean, ConvertUtil.obj2Double(map.get(key)));
                        } else {
                            System.out.println(method.getName() + "方法的参数类型为:" + parameterType + "该类型暂时没有处理,请联系杨凯");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("com.zzsb.core.utils.ConvertUtil.map2Bean(BaseBean, Map)方法执行失败");
        }
        return bean;
    }
    
    public <T> T queryForList(String sql,BaseBean baseBean){
        List<Map> list = this.queryForList(sql, baseBean.getBeanValues());
        if (list != null && list.size() > 0) {
            List<BaseBean> beanList = new ArrayList<BaseBean>();
            for (Map map : list) {
                beanList.add(map2Bean(baseBean.newInstance(), map));
            }
            return (T)beanList;
        }
        return (T)new ArrayList<BaseBean>();
    }
    
    
    /**
     * @param sql
     * @param param
     * @return
     */
    public List<Map> queryForList(String sql,Map param) {
        List<Map<String,Object>> list = this.paraJdbcTemplate.queryForList(sql, param);
        if (list == null) {
            list = new ArrayList<Map<String,Object>>();
        }
        return (List)list;
    }
    
    public List<Map> queryForList(String sql) {
        List<Map<String,Object>> list = this.jdbcTemplate.queryForList(sql);
        if (list == null) {
            list = new ArrayList<Map<String,Object>>();
        }
        return (List)list;
    }
    
    
    /**
     * ��ҳ��ѯ
     * @param sql
     * @param param
     * @param splitPage
     * @return
     * @version V1.0.0
     * @author �
     * @date Sep 19, 2013 8:21:07 AM
     */
    public List<Map> queryForSplit(String sql,SqlParameter param,SplitPage splitPage){
        /*String infoCountSql = "select count(*) from ("+ sql +") as infoCount"; 
        int infoCount = ConvertUtil.obj2Integer(this.queryForString(infoCountSql,param));
        splitPage.setInfoCount(infoCount);
        String infoListSql = sql +  " limit " + (splitPage.getCurrentPageIndex()-1)*splitPage.getPageSize() +"," + splitPage.getPageSize();
        return queryForList(infoListSql,param);*/
        
        List<Map> list = queryForList(sql,param);
        int infoCount = list.size();
        splitPage.setInfoCount(infoCount);
        int bin = (splitPage.getCurrentPageIndex()-1)*splitPage.getPageSize();
        int end = (splitPage.getCurrentPageIndex()-1)*splitPage.getPageSize()+splitPage.getPageSize();
        if(end>infoCount){
            end=infoCount;
        }
        return list.subList(bin,end );
 
    }
    
    
    public Map queryForDataGrid(HttpServletRequest request,String sql,SqlParameter param){
        int pageindex = ConvertUtil.obj2Integer(request.getParameter("page")); //当前页码
        int pagesize = ConvertUtil.obj2Integer(request.getParameter("rows")); //每页展示数量
        
        //TODO 此处分页采用的是将所有数据加载至内存后,使用JAVA分页,需优化
        List<Map> list = queryForList(sql,param);
        int infoCount = list.size();
        int bin = (pageindex-1)* pagesize ;
        int end = (pageindex-1)* pagesize + pagesize;
        if(end>infoCount){
            end=infoCount;
        }
        List<Map> subList = list.subList(bin,end);
        
        Map rootMap = new HashMap();
        rootMap.put("total", infoCount); //信息总数
        rootMap.put("rows", subList);
        return rootMap;
    }
 
    
    public Map queryForMap(String sql,Map paramMap){
        List<Map> list  = queryForList(sql, paramMap);
        if (list == null || list.size() == 0) {
            return  new HashMap();
        }
        return list.get(0);
    }
    
    
    public Map queryForMap(String sql){
        Map map = this.queryForMap(sql,new SqlParameter());
        if (map == null) {
            map = new HashMap();
        }
        return map;
    }
    
    
    public String queryForString(String sql,Map paramMap){
        Map map = queryForMap(sql, paramMap);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Str(map.get(map.keySet().iterator().next()));
        }
    }
    
    public String queryForString(String sql){
        Map map = queryForMap(sql);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Str(map.get(map.keySet().iterator().next()));
        }
    }
    
    
    public Integer queryForInteger(String sql){
        Map map = queryForMap(sql);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Integer(map.get(map.keySet().iterator().next()));
        }
    }
    
    public Long queryForLong(String sql){
        Map map = queryForMap(sql);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Long(map.get(map.keySet().iterator().next()));
        }
    }
    
    public Double queryForDouble(String sql){
        Map map = queryForMap(sql);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Double(map.get(map.keySet().iterator().next()));
        }
    }
    
    public Double queryForDouble(String sql,Map paramMap){
        Map map = queryForMap(sql,paramMap);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Double(map.get(map.keySet().iterator().next()));
        }
    }
    
    public Long queryForLong(String sql,Map paramMap){
        Map map = queryForMap(sql,paramMap);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Long(map.get(map.keySet().iterator().next()));
        }
    }
    
    
    public Integer queryForInteger(String sql,Map paramMap){
        Map map = queryForMap(sql,paramMap);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            return ConvertUtil.obj2Integer(map.get(map.keySet().iterator().next()));
        }
    }
    
    
    
    
    
    /**
     * @param sql
     * @param param
     */
    public Integer execute(String sql,Map paramMap) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        SqlParameterSource source = new MapSqlParameterSource(paramMap);
        this.paraJdbcTemplate.update(sql, source, keyHolder);
        if (keyHolder.getKey() == null) {
            return null;
        }
        return keyHolder.getKey().intValue();
    }
    
    /**
     * 
     * 在执行insert操作之后获取最新的ID
     * @return
     * @version V1.0.0
     * @author 杨凯
     * @date Dec 3, 2013 1:35:52 PM
     */
    public int getCurrentPK(){
        return queryForInteger("SELECT @@IDENTITY");
    }
    
    
    public void executeBatch(String sql,List<SqlParameter> paramList){
        if (paramList == null || paramList.size() == 0) {
            return;
        }
        Map[] maps = new Map[paramList.size()];
        for (int i = 0; i < paramList.size(); i++) {
            maps[i] = paramList.get(i);
        }
        this.paraJdbcTemplate.batchUpdate(sql, maps);
    }
    
    public void executeBatch(List<String> sqlList){
        String[] sqls = new String[sqlList.size()];
        for (int i = 0; i < sqlList.size(); i++) {
            sqls[i] = sqlList.get(i);
        }
        this.jdbcTemplate.batchUpdate(sqls);
    }
    
    
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
 
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public NamedParameterJdbcTemplate getParaJdbcTemplate() {
        return paraJdbcTemplate;
    }
 
    public void setParaJdbcTemplate(NamedParameterJdbcTemplate paraJdbcTemplate) {
        this.paraJdbcTemplate = paraJdbcTemplate;
    }
 
    public Map queryForDataGrid(Integer pageindex, Integer pagesize, String sql, SqlParameter sqlParameter) {
        if(pageindex==null){
            pageindex=1;
        }
        if(pagesize==null){
            pagesize=10;//默认
        }
        //TODO 此处分页采用的是将所有数据加载至内存后,使用JAVA分页,需优化
        List<Map> list = queryForList(sql,sqlParameter);
        int infoCount = list.size();
        int bin = (pageindex-1)* pagesize ;
        int end = (pageindex-1)* pagesize + pagesize;
        if(end>infoCount){
            end=infoCount;
        }
        List<Map> subList = list.subList(bin,end);
 
        Map rootMap = new HashMap();
        rootMap.put("total", infoCount); //信息总数
        rootMap.put("rows", subList);
        rootMap.put("page",pageindex);
        return rootMap;
    }
    
    public Map queryForDataGrid(Integer pageindex, Integer pagesize, String sql) {
        if(pageindex==null){
            pageindex=1;
        }
        if(pagesize==null){
            pagesize=10;//默认
        }
        //TODO 此处分页采用的是将所有数据加载至内存后,使用JAVA分页,需优化
        List<Map> list = queryForList(sql);
        int infoCount = list.size();
        int bin = (pageindex-1)* pagesize ;
        int end = (pageindex-1)* pagesize + pagesize;
        if(end>infoCount){
            end=infoCount;
        }
        List<Map> subList = list.subList(bin,end);
 
        Map rootMap = new HashMap();
        rootMap.put("total", infoCount); //信息总数
        rootMap.put("rows", subList);
        rootMap.put("page",pageindex);
        return rootMap;
    }
    
    
    
    /**
     * 获取分页信息
     * @param sql
     * @param paramMap
     * @return
     */
    public List<Map> queryforSplitPage(HttpServletRequest request,String sql,Map paramMap) {
        StringBuilder builder = new StringBuilder(sql);
        String pageSize = request.getParameter("pageSize");
        String currPage = request.getParameter("currPage");
        if(!StringUtil.notEmptyNum(pageSize)){
            pageSize = "10";
        }
        if(!StringUtil.notEmptyNum(currPage)){
            currPage = "1";
        }
        int begin = Integer.valueOf(currPage);
        int size = Integer.valueOf(pageSize);
        
        builder.append( " LIMIT :begin,:size ");
        paramMap.put("begin", (begin-1)*size);
        paramMap.put("size", size);
        List<Map> result = queryForList(builder.toString(),paramMap);
        return result;
    }
    
    /**
     * 获取分页信息
     * @param sql
     * @param paramMap
     * @return
     */
    public PageInfo queryforSplitPageInfo(PageInfo pageInfo,String sql,Map paramMap) {
        StringBuilder builder = new StringBuilder(sql);
        String pageSize = pageInfo.getPageSize();
        String currPage = pageInfo.getCurrPage();
        if(!StringUtil.notEmptyNum(pageSize)){
            pageSize = "10";
        }
        if(!StringUtil.notEmptyNum(currPage)){
            currPage = "1";
        }
        int begin = Integer.valueOf(currPage);
        int size = Integer.valueOf(pageSize);
        
        builder.append( " LIMIT :begin,:size ");
        int beginSize =(begin-1)*size;
        paramMap.put("begin", (begin-1)*size);
        paramMap.put("size", size);
        List<Map> result = queryForList(builder.toString(),paramMap);
        
        PageInfo info = new PageInfo();
        info.setRowNum(beginSize);
        info.setDatas(result);
        return info;
    }
    
    /**
     * 获取分页信息
     * @param sql
     * @param paramMap
     * @return
     */
    public PageInfo queryforSplitPageInfo(HttpServletRequest request,String sql,Map paramMap) {
        StringBuilder builder = new StringBuilder(sql);
        String pageSize = request.getParameter("pageSize");
        String currPage = request.getParameter("currPage");
        if(!StringUtil.notEmptyNum(pageSize)){
            pageSize = "10";
        }
        if(!StringUtil.notEmptyNum(currPage)){
            currPage = "1";
        }
        int begin = Integer.valueOf(currPage);
        int size = Integer.valueOf(pageSize);
        
        builder.append( " LIMIT :begin,:size ");
        int beginSize =(begin-1)*size;
        paramMap.put("begin", (begin-1)*size);
        paramMap.put("size", size);
        List<Map> result = queryForList(builder.toString(),paramMap);
        
        PageInfo info = new PageInfo();
        info.setRowNum(beginSize);
        info.setDatas(result);
        return info;
    }
    public PageBean getPageList(String sql, Map param) {
        Integer pageSize = ConvertUtil.obj2Integer(param.get("pageSize"));
        Integer page = ConvertUtil.obj2Integer(param.get("page"));
        Integer start = (page - 1) * pageSize;
        if (pageSize != null && page != null) {
            Integer count = queryForInteger("select count(*) from ( " + sql + " ) query_sql ", param);
            sql = sql + " limit :start,:size ";
            param.put("start", start);
            param.put("size", pageSize);
            List<Map> list = queryForList(sql, param);
            return new PageBean(page, pageSize, count, list);
        }
        return new PageBean();
    }
}