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. Github无法读取远程仓库

    主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ Git无法访问 今早起来访问Github炸了,Git不能用了,提示: ...

  2. centos 目录磁盘使用情况

    最简单的查看方法可以使用ls -ll.ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB.MB等为单位进行显示,这样比较直观一些.   通过命令du -h –max- ...

  3. C#移除List中特定元素

    在List里面移除其中一个元素之后,原有的索引以及索引对应的值会发生改变,如果按照原有的索引值删除,就会误删除其它元素. 1.实现思路 原始List为A,将需要删除的元素放到一个List B里面,遍历 ...

  4. MindSpore自定义算子中的张量维度问题

    技术背景 在前面的几篇博客中,我们介绍了MindSpore框架下使用CUDA来定义本地算子的基本方法,以及配合反向传播函数的使用,这里主要探讨一下MindSpore框架对于CUDA本地算子的输入输出的 ...

  5. nowrap - table td 列 宽度 不被挤 - 大表格制作

    nowrap - table td 列 宽度 不被挤 - 大表格制作 表格前几列 设置完宽度,会被右侧动态数据挤没有宽度,加上nowrap,就保证宽度了

  6. Vue3.x知识图谱

    Vue3.x知识图谱 https://gitee.com/jishupang/vue3-knowledge-map/tree/master 回头有时间再贴图吧

  7. 【深度学习】神经网络正则化方法之Dropout

    前言 正则化是一种广泛用于机器学习和深度学习的手段,它的目的就是阻碍模型过度学习(过拟合),从而提升算法的泛化能力. Dropout 是一种常见的缓解过拟合的方法.接下来,本文将从原理和实践来介绍Dr ...

  8. python面向对象(基础)

    一 面向对象介绍 面向过程: 核心是:"过程"二字 过程的终极奥义就是将程序流程化 过程是"流水化",用来分步骤解决问题的 面向对象: 核心是"对象& ...

  9. 记录--如何在H5中实现OCR拍照识别身份证功能

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 业务背景 由于当前项目中需要实现身份证拍照识别的功能,如果是小程序可以使用微信提供的 ocr-navigator 插件实现,但是在企业微信 ...

  10. 【面试】将 95% 求职者拒之门外的BAT大数据面试题-附解题方法(文末有福利)

    写在前面 最近不少读者找我要大数据面试题,我整理了很久,筛选出这10道容易出错的大数据面试题,希望对大家有所帮助.题目与解答整理自互联网,感谢分享这些面经的技术大牛们! 题目概览 如何从大量的 URL ...