mybatis generator生成mapper接口后的代理类,很方便使用。
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接口后的代理类,很方便使用。的更多相关文章
- mybatis中的mapper接口文件以及selectByExample类的实例函数详解
记录分为两个部分,第一部分主要关注selectByExample类的实例函数的实现:第二部分讨论Mybatis框架下基本的实例函数. (一)selectByExample类的实例函数的实现 当你启动项 ...
- mybatis generator 生成带中文注释的model类
将org.mybatis.generator.interal.DefaultCommentGenerator类的addFieldComment方法重写,代码如下: public void addFie ...
- mybatis中的mapper接口文件以及example类的实例函数以及详解
##Example example = new ##Example(); example.setOrderByClause("字段名 ASC"); //升序排列,desc为降序排列 ...
- mybatis如何根据mapper接口生成其实现类
SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- MyBatis:GeneratorConfig生成mapper以及pojo
首先我们需要导入相应的依赖 之后需要针对的配置一些数据 接着我们需要针对性的写配置文件,在根目录下写mybatis的主要配置文件 如上图我们配置了数据库连接.对应的一些pojo.mapper.java ...
- mybatis generator生成文件大小写问题
mybatis generator插件中,如果 mysql数据表中的字段是用下划线划分的(个人一般都是喜欢这么创建表的字段,如:company_name),那么生成的Vo中会自动对应为companyN ...
- Maven下用MyBatis Generator生成文件
使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
随机推荐
- [.Net]使用Soa库+Abp搭建微服务项目框架(三):项目改造
上一章我们说道,如果要使各模块之间解耦,使得各自独立成服务,首先要解除各个模块之间的引用关系. 还记得上一章我们的小项目吗 ?们回到之前的代码上来,当前的项目架构如下图: 这次的任务是将它改造成 ...
- Java处理子父级菜单的方式二
处理存在子父级关系的数据是写代码的过程中常见的操作,前面讲解过使用递归的方法来做, 可以参考这篇博客 https://www.cnblogs.com/yilangcode/p/16831867.htm ...
- WPF之模板
目录 模板的内涵 数据的外衣DataTemplate UserControl例子 DataTemplate例子 控件的外衣 ControlTemplate 解剖控件 ItemsControl的Pane ...
- Windows逆向之配置虚拟机环境
安装虚拟机环境 首先下载吾爱破解论坛专用版虚拟机软件VMWare Workstation 12,注册成功:再通过虚拟机启动论坛提供的WinXP系统镜像,在我的win11上完美运行. 实现文件互传 为了 ...
- python元组(tuple)循环遍历实例分析
一 概念: 元组是有序且不可更改的集合.在 Python 中,元组是用圆括号编写的. 二 使用方法: 1 基本创建: thistuple = ("apple", "ba ...
- 修改阿里云DNS 解决蓝奏云无法访问问题
某些地区的宽带连接不上蓝奏云服务器,需要手动改一下DNS配置,改为阿里云的即可 PS:阿里云DNS服务器地址为223.5.5.5 和 223.6.6.6 下面以win10系统为例,具体步骤如下 1.进 ...
- cmd查看环境变量
1.查看当前所有可用的环境变量:输入 set 即可查看. 2.查看某个环境变量:输入 "set 变量名"即可,比如想查看path变量的值,即输入 set path 3.修改环境变量 ...
- Python 汇总列数据到行
Python汇总Excel列数据到行(方法一) import pandas as pd # 读取Excel文件 df = pd.read_excel('C:\\Users\\liuchunlin2\\ ...
- 主nginx和子nginx-------域名-端口-解答
主nginx和子nginx-------域名-端口-解答 想象一下Nginx是一个接待员,每个端口就像接待员的一个电话线,而server_name就像是客户拨打的不同号码. 当你在Nginx配置文件里 ...
- linux学习指令与现有环境解决问题笔记
linux学习指令与现有环境笔记 注意:我将pytorch和cuda安装在了pytorch这个虚拟环境中 pytorch安装及注意问题 注意版本对应,稳定版2.0.1对应cuda11.7,别按错了 按 ...