1.spring 配置:
<bean id="superMapperProxy" class="com.qws.v1.daoImpl.MapperProxy"> <property name="mapperPackagePrifix" value="com.qws.v1.mapper."></property> </bean>
  1 package com.qsw.v1.daoImpl;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.concurrent.TimeUnit;
8
9 import org.apache.ibatis.annotations.Param;
10 import org.springframework.beans.BeansException;
11 import org.springframework.context.ApplicationContext;
12 import org.springframework.context.ApplicationContextAware;
13 import org.springframework.data.redis.core.RedisTemplate;
14 import org.springframework.data.redis.core.ValueOperations;
15
16 import com.qsw.v1.util.BaseClass;
17
18 /**
19 * mapper proxy,结合pageHelper+MyBatisGenerator简直所向无敌
20 * 后期将加入redis缓存功能,结合nginx转发组成高并发后端
21 * @author Administrator
22 *
23 */
24 public class MapperProxy extends BaseClass implements ApplicationContextAware {
25
26 /************************************** 代理方法名 start ***************************************/
27 public static final String COUNT_BY_EXAMPLE_METHOD_NAME = "countByExample";
28 public static final String DELETE_BY_EXAMPLE_METHOD_NAME = "deleteByExample";
29 public static final String DELETE_BY_PRIMARY_KEY_METHOD_NAME = "deleteByPrimaryKey";
30 public static final String INSERT_METHOD_NAME = "insert";
31 public static final String INSERT_SELECTIVE_METHOD_NAME = "insertSelective";
32 public static final String SELECT_BY_EXAMPLE_METHOD_NAME = "selectByExample";
33 public static final String SELECT_BY_PRIMARY_KEY_METHOD_NAME = "selectByPrimaryKey";
34 public static final String UPDATE_BY_EXAMPLE_SELECTIVE_METHOD_NAME = "updateByExampleSelective";
35 public static final String UPDATE_BY_EXAMPLE_METHOD_NAME = "updateByExample";
36 public static final String UPDATE_BY_PRIMARYKEY_SELECTIVE_METHOD_NAME = "updateByPrimaryKeySelective";
37 public static final String UPDATE_BY_PRIMARYKEY_METHOD_NAME = "updateByPrimaryKey";
38 /************************************** 代理方法名 end ***************************************/
39
40 /**
41 * mapper所在包的前缀
42 */
43 private String mapperPackagePrifix;
44
45 /**
46 * application 实例
47 */
48 private ApplicationContext ctx;
49
50 public void setMapperPackagePrifix(String name) {
51 this.mapperPackagePrifix = name;
52 }
53
54 /**
55 * 根据一个对象查找Mapper,对象要么是实体类,要么是example
56 * @param obj
57 * @return
58 */
59 public Object findMapper(Object obj) {
60 try {
61 if (obj == null) {
62 logger.warn("the param obj instance can not be null !");
63 return null;
64 }
65 Class<?> clazz = obj.getClass();
66 String name = clazz.getSimpleName();
67 String mapperClassName = "";
68 if (name.endsWith("Example")) {
69 int index = name.indexOf("Example");
70 mapperClassName = mapperPackagePrifix + name.substring(0, index) + "Mapper";
71 } else {
72 mapperClassName = mapperPackagePrifix + name + "Mapper";
73 }
74 logger.debug("mapper class name is :" + mapperClassName );
75 Class<?> mapperClazz = Class.forName(mapperClassName);
76 logger.debug("mapper is found :" + mapperClazz );
77 return ctx.getBean(mapperClazz);
78 } catch (Exception e) {
79 logger.error("find mapper failed : " + e.getMessage());
80 }
81 return null;
82 }
83
84 /**
85 * 查找方法
86 * @param obj
87 * @return
88 */
89 public Method findMethod(Object mapper, String methodName, Object... parameters) {
90 try {
91 if (mapper == null) {
92 logger.warn("the mapper can not be null ! ");
93 }
94 Class<?> clazz = mapper.getClass();
95 Class<?>[] clazzArray = new Class[parameters.length];
96 for (int i = 0; i < parameters.length; i++) {
97 if (parameters[i] == null) {
98 continue;
99 }
100 clazzArray[i] = parameters[i].getClass();
101 }
102 logger.debug("the clazz array is " + Arrays.toString(clazzArray));
103 return clazz.getDeclaredMethod(methodName, clazzArray);
104 } catch (Exception e) {
105 logger.error(" find method : " + methodName + " is error," + e.getMessage());
106 }
107 return null;
108 }
109
110 /**
111 * 查询数量
112 *
113 * @param example
114 * @return
115 */
116 public int countByExample(Object example) {
117 try {
118 if (example == null) {
119 logger.warn("countByExample failed,the example can not be null !");
120 return -1;
121 }
122
123 Object mapper = findMapper(example);
124 logger.debug("mapper instance found for " + mapper.getClass());
125
126 Method method = findMethod(mapper, COUNT_BY_EXAMPLE_METHOD_NAME, example);
127 logger.debug("mapper instance found for " + mapper.getClass());
128
129 return (int) method.invoke(mapper, example);
130 } catch (Exception e) {
131 logger.error("countByExample failed, " + e.getMessage() );
132 }
133 return -1;
134 }
135
136 /**
137 * 代理方法
138 * @param example
139 * @return
140 */
141 public int deleteByExample(Object example) {
142 try {
143 if (example == null) {
144 logger.warn("deleteByExample failed,the example can not be null !");
145 return -1;
146 }
147
148 Object mapper = findMapper(example);
149 logger.debug("mapper instance found for " + mapper.getClass());
150
151 Method method = findMethod(mapper, DELETE_BY_EXAMPLE_METHOD_NAME, example);
152 logger.debug("mapper instance found for " + mapper.getClass());
153
154 return (int) method.invoke(mapper, example);
155 } catch (Exception e) {
156 logger.error("deleteByExample failed, " + e.getMessage() );
157 }
158 return -1;
159 }
160
161 /**
162 * 代理方法
163 * @param id
164 * @param beanClazz
165 * @return
166 */
167 public int deleteByPrimaryKey(Object id, Class<?> beanClazz) {
168 String mapperClassName = "";
169 try {
170 if (id == null) {
171 logger.warn("deleteByPrimaryKey failed,the id can not be null !");
172 return -1;
173 }
174
175 if (beanClazz == null) {
176 logger.warn("deleteByPrimaryKey failed,the beanClazz can not be null !");
177 return -1;
178 }
179
180 mapperClassName = mapperPackagePrifix + beanClazz.getSimpleName() + "Mapper";
181 Class<?> clazz = Class.forName(mapperClassName);
182 logger.debug("Mapper class found for " + mapperClassName);
183
184 Object mapper = ctx.getBean(mapperClassName);
185 logger.debug("mapper instance found for " + mapper.getClass());
186
187 Method method = clazz.getMethod(DELETE_BY_PRIMARY_KEY_METHOD_NAME, id.getClass());
188 logger.debug("mapper instance found for " + mapper.getClass());
189
190 return (int) method.invoke(mapper, id);
191 } catch (Exception e) {
192 logger.error("deleteByPrimaryKey failed, " + e.getMessage() );
193 }
194 return -1;
195 }
196
197 /**
198 * 代理方法
199 * @param record
200 * @return
201 */
202 public int insert(Object record) {
203 try {
204 if (record == null) {
205 logger.warn("insert faild,the object can not be null !");
206 return -1;
207 }
208
209 Object mapper = findMapper(record);
210 logger.debug("mapper instance found for " + mapper.getClass());
211
212 Method insertMethod = findMethod(mapper, INSERT_METHOD_NAME, record);
213 logger.debug("method found in " + mapper.getClass());
214
215 if (insertMethod == null) {
216 logger.warn("insert faild,the method dose not exits !");
217 return -1;
218 }
219
220 Object value = insertMethod.invoke(mapper, record);
221 logger.info("insert success,the return value is: " + value );
222 ValueOperations<String, Object> values = ctx.getBean(RedisTemplate.class).opsForValue();
223 List<Object> lst=(List<Object>) values.get("INSERT_LIST");
224 if (lst==null) {
225 lst=new ArrayList<Object>();
226 }
227 values.set("INSERT_LIST", lst, 3, TimeUnit.DAYS);
228 return (int) value;
229 } catch (Exception e) {
230 logger.error("insert faild," + e.getMessage() );
231 }
232 return -1;
233 }
234
235 /**
236 * 代理方法
237 * @param record
238 * @return
239 */
240 public int insertSelective(Object record) {
241
242 try {
243 if (record == null) {
244 logger.warn("insertSelective faild,the object can not be null !");
245 return -1;
246 }
247
248 Object mapper = findMapper(record);
249 logger.debug("mapper instance found for " + mapper.getClass());
250
251 Method insertMethod = findMethod(mapper, INSERT_SELECTIVE_METHOD_NAME, record);
252 logger.debug("method found in " + mapper.getClass());
253
254 if (insertMethod == null) {
255 logger.warn("insertSelective faild,the method dose not exits !");
256 return -1;
257 }
258
259 Object value = insertMethod.invoke(mapper, record);
260 ValueOperations<String, Object> values = ctx.getBean(RedisTemplate.class).opsForValue();
261 List<Object> lst=(List<Object>) values.get(record.getClass().getSimpleName()+"_List");
262 if (lst==null) {
263 lst=new ArrayList<Object>();
264 }
265 values.set(record.getClass().getSimpleName()+"_List", lst, 3, TimeUnit.DAYS);
266 logger.info("insertSelective success,the return value is: " + value );
267 return (int) value;
268 } catch (Exception e) {
269 e.printStackTrace();
270 logger.error("insertSelective faild," + e.getMessage() );
271 }
272 return -1;
273 }
274
275 /**
276 * 代理方法
277 * @param example
278 * @return
279 */
280 public List<?> selectByExample(Object example) {
281 try {
282 if (example == null) {
283 logger.warn("selectByExample failed,the example can not be null !");
284 return null;
285 }
286
287 Object mapper = findMapper(example);
288 logger.debug("mapper instance found for " + mapper.getClass());
289
290 Method method = findMethod(mapper, SELECT_BY_EXAMPLE_METHOD_NAME, example);
291 logger.debug("mapper instance found for " + mapper.getClass());
292
293 return (List<?>) method.invoke(mapper, example);
294 } catch (Exception e) {
295 logger.error("deleteByExample failed, " + e.getMessage() );
296 }
297 return null;
298 }
299
300 /**
301 * 代理方法,代理原selectByPrimaryKey方法,由于原方法只需要主键,此处无法只需要一个主键,故加入一个Type
302 * @param id
303 * @param clazz
304 * @return
305 */
306 @SuppressWarnings("unchecked")
307 public <T> T selectByPrimaryKey(Object id, Class<T> clazz) {
308 String mapperClassName = "";
309 try {
310 if (id == null) {
311 logger.warn("selectByPrimaryKey failed,the id can not be null !");
312 return null;
313 }
314
315 if (clazz == null) {
316 logger.warn("selectByPrimaryKey failed,the param clazz can not be null !");
317 return null;
318 }
319 mapperClassName = mapperPackagePrifix + clazz.getSimpleName() + "Mapper";
320 Class<?> clazzMapper = Class.forName(mapperClassName);
321 logger.debug("Mapper class found for " + mapperClassName);
322
323 Object mapper = ctx.getBean(clazzMapper);
324 logger.debug("mapper instance found for " + mapper.getClass());
325
326 Method method = clazzMapper.getMethod(SELECT_BY_PRIMARY_KEY_METHOD_NAME, id.getClass());
327 logger.debug("mapper instance found for " + mapper.getClass());
328
329 return (T) method.invoke(mapper, id);
330 } catch (Exception e) {
331 logger.error("selectByPrimaryKey failed, " + e.getMessage() );
332 }
333 return null;
334 }
335
336 /**
337 * 代理方法
338 * @param record
339 * @param example
340 * @return
341 */
342 public int updateByExampleSelective(@Param("record") Object record, @Param("example") Object example) {
343
344 try {
345 if (record == null) {
346 logger.warn("updateByExampleSelective faild,the object can not be null !");
347 return -1;
348 }
349
350 Object mapper = findMapper(record);
351 logger.debug("mapper instance found for " + mapper.getClass());
352
353 Method updateByExampleSelectiveMethod = findMethod(mapper, UPDATE_BY_EXAMPLE_SELECTIVE_METHOD_NAME, record,
354 example);
355 logger.debug("method found in " + mapper.getClass());
356
357 if (updateByExampleSelectiveMethod == null) {
358 logger.warn("updateByExampleSelectiveMethod faild,the method dose not exits !");
359 return -1;
360 }
361
362 Object value = updateByExampleSelectiveMethod.invoke(mapper, record, example);
363 logger.debug("updateByExampleSelective success,the return value is: " + value );
364 return (int) value;
365 } catch (Exception e) {
366 logger.error("updateByExampleSelective faild," + e.getMessage() );
367 }
368 return -1;
369 }
370
371 /**
372 * 代理方法
373 * @param record
374 * @param example
375 * @return
376 */
377 public int updateByExample(@Param("record") Object record, @Param("example") Object example) {
378
379 try {
380 if (record == null) {
381 logger.warn("updateByExample faild,the record can not be null !");
382 return -1;
383 }
384 if (example == null) {
385 logger.warn("updateByExample faild,the example can not be null !");
386 return -1;
387 }
388
389 Object mapper = findMapper(record);
390 logger.debug("mapper instance found for " + mapper.getClass());
391
392 Method updateByExampleSelectiveMethod = findMethod(mapper, UPDATE_BY_EXAMPLE_METHOD_NAME, record, example);
393 logger.debug("method found in " + mapper.getClass());
394
395 if (updateByExampleSelectiveMethod == null) {
396 logger.warn("updateByExample faild,the method dose not exits !");
397 return -1;
398 }
399
400 Object value = updateByExampleSelectiveMethod.invoke(mapper, record, example);
401 logger.debug("updateByExample success,the return value is: " + value );
402 return (int) value;
403 } catch (Exception e) {
404 logger.error("updateByExample faild," + e.getMessage() );
405 }
406 return -1;
407 }
408
409 /**
410 * 代理方法
411 * @param record
412 * @return
413 */
414 public int updateByPrimaryKeySelective(Object record) {
415
416 try {
417 if (record == null) {
418 logger.warn("updateByPrimaryKeySelective faild,the object can not be null !");
419 return -1;
420 }
421
422
423 Object mapper =findMapper(record);
424 logger.debug("mapper instance found for " + mapper.getClass());
425
426 Method updateByExampleSelectiveMethod = findMethod(mapper,UPDATE_BY_PRIMARYKEY_SELECTIVE_METHOD_NAME, record);
427 logger.debug("method found in " + mapper.getClass());
428
429 if (updateByExampleSelectiveMethod == null) {
430 logger.warn("updateByExampleSelectiveMethod faild,the method dose not exits !");
431 return -1;
432 }
433
434 Object value = updateByExampleSelectiveMethod.invoke(mapper, record);
435 logger.info("updateByExampleSelectiveMethod success,the return value is: " + value );
436 return (int) value;
437 } catch (Exception e) {
438 logger.error("updateByExampleSelectiveMethod faild," + e.getMessage() );
439 }
440 return -1;
441 }
442
443 /**
444 * 代理方法
445 * @param record
446 * @return
447 */
448 public int updateByPrimaryKey(Object record) {
449
450 try {
451 if (record == null) {
452 logger.warn("updateByPrimaryKey faild,the object can not be null !");
453 return -1;
454 }
455
456 Object mapper = findMapper(record);
457 logger.debug("mapper instance found for " + mapper.getClass());
458
459 Method updateByExampleSelectiveMethod =findMethod(mapper,UPDATE_BY_PRIMARYKEY_METHOD_NAME, record);
460 logger.debug("method found in " + mapper.getClass());
461
462 if (updateByExampleSelectiveMethod == null) {
463 logger.warn("updateByPrimaryKey faild,the method dose not exits !");
464 return -1;
465 }
466
467 Object value = updateByExampleSelectiveMethod.invoke(mapper, record);
468 logger.info("updateByPrimaryKey success,the return value is: " + value );
469 return (int) value;
470 } catch (Exception e) {
471 logger.error("updateByPrimaryKey faild," + e.getMessage() );
472 }
473 return -1;
474 }
475
476 @Override
477 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
478 ctx = applicationContext;
479 }
480
481 }

2.使用,如图:

mybatis generator生成mapper接口后的代理类,很方便使用。的更多相关文章

  1. mybatis中的mapper接口文件以及selectByExample类的实例函数详解

    记录分为两个部分,第一部分主要关注selectByExample类的实例函数的实现:第二部分讨论Mybatis框架下基本的实例函数. (一)selectByExample类的实例函数的实现 当你启动项 ...

  2. mybatis generator 生成带中文注释的model类

    将org.mybatis.generator.interal.DefaultCommentGenerator类的addFieldComment方法重写,代码如下: public void addFie ...

  3. mybatis中的mapper接口文件以及example类的实例函数以及详解

    ##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...

  4. mybatis如何根据mapper接口生成其实现类

    SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...

  5. Mybatis Generator生成工具配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  6. MyBatis:GeneratorConfig生成mapper以及pojo

    首先我们需要导入相应的依赖 之后需要针对的配置一些数据 接着我们需要针对性的写配置文件,在根目录下写mybatis的主要配置文件 如上图我们配置了数据库连接.对应的一些pojo.mapper.java ...

  7. mybatis generator生成文件大小写问题

    mybatis generator插件中,如果 mysql数据表中的字段是用下划线划分的(个人一般都是喜欢这么创建表的字段,如:company_name),那么生成的Vo中会自动对应为companyN ...

  8. Maven下用MyBatis Generator生成文件

    使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...

  9. 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

  10. 使用mybatis-generator插件结合tk.mybatis自动生成mapper

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

随机推荐

  1. audio currentTime 设置后,重置成0,解决方案(流文件-下载文件)

    audio currentTime 设置后,重置成0,解决方案 第一步-流文件-下载文件: 先查看你的mp3文件是 流文件,还是下载文件. 检测方式,就是放到浏览器回车.在线播放就是流文件,直接下载了 ...

  2. [VueJsDev] 基础知识 - Button的全局节流

    [VueJsDev] 目录列表 https://www.cnblogs.com/pengchenggang/p/17037320.html Button的全局节流 ::: details 目录 目录 ...

  3. Python使用os模块创建带时间戳的文件夹

    直接上源码: # 导入os模块 import os import time # 创建文件夹函数 def mkdir(path): # os.path.exists 函数判断文件夹是否存在 folder ...

  4. day07-1MySQL约束

    MySQL约束 基本介绍 约束用于确保数据库的数据满足特定的商业规则 在mysql中,约束包括:not null,unique,primary key,foreign key 和check 5种 1. ...

  5. (模板)Manacher算法:线性时间求字符串内回文子串的数量

    已通过leetcode647:https://leetcode-cn.com/problems/palindromic-substrings/ void get_d(vector<int> ...

  6. LINQ 学习之路

    一.为什么要使用 LINQ 要理解为什么使用 LINQ,先来看下下面的例子 例子:要统计字符串中每个字母出现的频率(忽略大小写),然后按照从高到低的顺序输出出现频率高于2次和其出现的的频率.如果用传统 ...

  7. ElasticSearch 准实时原理

    Elasticsearch 是一个基于 Lucene 库的搜索引擎.它提供了一个准实时的.分布式.支持多租户的全文搜索引擎. ----维基百科 那么问题来了,为啥 Elasticsearch 不是实时 ...

  8. Android线程池封装库

    目录介绍 1.遇到的问题和需求 1.1 遇到的问题有哪些 1.2 遇到的需求 1.3 多线程通过实现Runnable弊端 1.4 为什么要用线程池 2.封装库具有的功能 2.1 常用的功能 3.封装库 ...

  9. 利用Python 去重聚合Excel数据并对比两份数据的差异

    问题背景 在数据处理过程中,常常需要将多个数据表进行合并,并进行比对,以便找出数据的差异和共同之处.本文将介绍如何使用 Pandas 库对两个 Excel 数据表进行合并与比对,并将结果输出到新的 E ...

  10. 冰河开始对Dubbo下手了!

    写在前面 对冰河有一定了解的读者都知道,冰河经历了一个高并发电商系统用户从零到上亿的整个研发过程,后期也由此衍生出电商系统(商城+秒杀)和基于海量数据的实时精准商品推荐平台.部分核心知识已总结到我出版 ...