当前位置:首页 > 服务端 > mybatis generator生成mapper接口后的代理类,很方便使用。

mybatis generator生成mapper接口后的代理类,很方便使用。

1.spring 配置:

<bean id="superMapperProxy" class="com.qws.v1.daoImpl.MapperProxy">
<property name="mapperPackagePrifix" value="com.qws.v1.mapper."></property>
</bean>

mybatis generator生成mapper接口后的代理类,很方便使用。 _ JavaClub全栈架构师技术笔记
  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 }
View Code

2.使用,如图:

mybatis generator生成mapper接口后的代理类,很方便使用。 _ JavaClub全栈架构师技术笔记

 

作者:漫漫人生路总会错几步
来源链接:https://www.cnblogs.com/swtjavaspace/p/6179650.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125367.html

分享给朋友:

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