BaseController
package com.example.demo.controller; import com.example.demo.service.BaseService; import com.example.demo.utils.DaoUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureOrder; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController @RequestMapping("base") public class BaseController { @PostMapping("select") public void test1(@RequestBody Map<String,Object> t){ DaoUtils.select(t); } }
AbstractParams
package com.example.demo.entity; import java.util.Map; public abstract class AbstractParams { public abstract <T> T convertParams(Map map); }
Params1
package com.example.demo.entity; import java.util.Map; public class Params1 extends AbstractParams{ @Override public Params1 convertParams(Map map) { System.out.printf("Params1"); return new Params1(); } }
Params2
package com.example.demo.entity; import java.util.Map; public class Params2 extends AbstractParams{ @Override public Params1 convertParams(Map map) { System.out.printf("Params2"); return null; } }
BaseService
package com.example.demo.service; import com.example.demo.entity.AbstractParams; public interface BaseService<T extends AbstractParams>{ Object getList(T t); }
Service1 Service2
package com.example.demo.service; import com.example.demo.entity.Params1; public interface Service1 extends BaseService<Params1>{ }
package com.example.demo.service; import com.example.demo.entity.Params2; public interface Service2 extends BaseService<Params2>{ }
ServiceImpl
package com.example.demo.service.impl; import com.example.demo.entity.Params1; import com.example.demo.service.Service1; import org.springframework.stereotype.Service; @Service public class BaseServiceImpl implements Service1 { @Override public Object getList(Params1 abstractParams) { System.out.printf("this is BaseServiceImpl....."); return null; } }
package com.example.demo.service.impl; import com.example.demo.entity.Params2; import com.example.demo.service.BaseService; import org.springframework.stereotype.Service; @Service public class BaseServiceImpl2 implements BaseService<Params2> { @Override public Object getList(Params2 abstractParams) { System.out.printf("this is BaseServiceImpl2....."); return null; } }
DaoUtils
package com.example.demo.utils; import com.example.demo.entity.AbstractParams; import com.example.demo.service.BaseService; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class DaoUtils<T,P extends AbstractParams> { private static Map<String,String> serviceMap = null; private static Map<String,String> parametersMap = null; static{ serviceMap = new HashMap<>(); serviceMap.put("table1","baseServiceImpl"); serviceMap.put("table2","baseServiceImpl2"); parametersMap = new HashMap<>(); parametersMap.put("table1","com.example.demo.entity.Params1"); parametersMap.put("table2","com.example.demo.entity.Params2"); } public static <B,T,P extends AbstractParams> void select(Map<String,Object> params){ try { //1.获取实例 String serviceName = serviceMap.get(params.get("tableFlag")); // Class<B> clazz = (Class<B>) Class.forName(serviceName); B serviceBean = (B) SpringUtils.getBean(serviceName); //2.获取方法 Class<P> paramsType = getParamsType(params); Method method = serviceBean.getClass().getMethod("getList",paramsType); //3.设置参数 P convertedParams = convertParams(params,paramsType); //执行方法 method.invoke(serviceBean,convertedParams); } catch (Exception e) { e.printStackTrace(); } } private static <P> Class<P> getParamsType(Map<String,Object> params){ try { Class tClass = Class.forName(parametersMap.get(params.get("tableFlag"))); return tClass; } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } private static <P extends AbstractParams> P convertParams(Map<String,Object> params, Class<P> paramsType){ try { P bean = paramsType.newInstance(); P params1 = bean.convertParams(params); return params1; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } }
SpringUtils
package com.example.demo.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtils implements ApplicationContextAware { // Spring应用上下文环境 private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtils.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } }
工程结构