mybatis表名反射实体

 1 package com.eshore.wbtimer.executor.service.impl;
 2 
 3 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 4 import com.baomidou.mybatisplus.mapper.SqlHelper;
 5 import com.eshore.wbtimer.executor.service.CommonService;
 6 import com.eshore.wbtimer.executor.util.field.SpringContextUtil;
 7 import org.springframework.stereotype.Service;
 8 import org.springframework.util.ReflectionUtils;
 9 
10 import java.lang.reflect.Method;
11 
12 /**
13  * 此方法利用动态代理和反射实现通用插入,根据bean类获取相应mapper接口,利用代理注入mapper,再反射调用响应方法
14  */
15 @Service
16 public class CommonServiceImpl implements CommonService {
17 
18     @Override
19     public boolean commonMapperSave(Class<?> bean, Object dataBean) {
20         String mapperPath = SqlHelper.table(bean).getCurrentNamespace();
21         Class<?> dataMapperClass = null;
22         try {
23             dataMapperClass = Class.forName(mapperPath);
24         } catch (ClassNotFoundException e) {
25             e.printStackTrace();
26             return false;
27         }
28         try {
29             Object mapperServiceBean = SpringContextUtil.getBean(dataMapperClass);
30             Method mapperServiceBeanMethod = ReflectionUtils.findMethod(mapperServiceBean.getClass(), "insert", Object.class);
31             // 执行方法
32             ReflectionUtils.invokeMethod(mapperServiceBeanMethod, mapperServiceBean, dataBean);
33             return true;
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37         return false;
38     }
39 
40     @Override
41     public boolean commonMapperSelect(String className) {
42         String mapperPath = "com.eshore.wbtimer.executor.mapper.bean."+className;
43         Class<?> dataMapperClass = null;
44         try {
45             dataMapperClass = Class.forName(mapperPath);
46         } catch (ClassNotFoundException e) {
47             e.printStackTrace();
48             return false;
49         }
50         String dataPath = SqlHelper.table(dataMapperClass).getCurrentNamespace();
51         Class<?> mapperClass = null;
52         try {
53             mapperClass = Class.forName(dataPath);
54         } catch (ClassNotFoundException e) {
55             e.printStackTrace();
56         }
57         try {
58             Object mapperServiceBean = SpringContextUtil.getBean(mapperClass);
59             EntityWrapper ew = new EntityWrapper();
60             ew.setEntity(mapperServiceBean);
61             ew.where("UPLOAD_TIMESTAMP < DATA_TIMESTAMP").or("UPLOAD_TIMESTAMP is  null");
62             if(className.startsWith("SJ")) {
63                 ew.where(" and CITY='SYGNGD' ");
64             }
65             Method mapperServiceBeanMethod = ReflectionUtils.findMethod(mapperServiceBean.getClass(), "select", EntityWrapper.class);
66             // 执行方法
67             Object obj = ReflectionUtils.invokeMethod(mapperServiceBeanMethod, mapperServiceBean, ew);
68             return true;
69         } catch (Exception e) {
70             e.printStackTrace();
71         }
72         return false;
73     }
74 }

接口CommonService;

 1 package com.eshore.wbtimer.executor.service;
 2 
 3 /**
 4  * @author 
 5  * @Description:
 6  * @date 2019/3/21 14:48
 7  */
 8 public interface CommonService {
 9     public boolean commonMapperSave(Class<?> bean, Object dataBean);
10 
11     public boolean commonMapperSelect(String className);
12 }
 
原文地址:https://www.cnblogs.com/lifuhei/p/10598821.html