¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.lf.server.mapper.all.BasicMapper; |
| | | import com.lf.server.mapper.all.GeomBaseMapper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.aop.support.AopUtils; |
| | | |
| | | import java.lang.reflect.Type; |
| | | |
| | | /** |
| | | * 类帮å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class ClassHelper { |
| | | private final static Log log = LogFactory.getLog(ClassHelper.class); |
| | | |
| | | /** |
| | | * æ ¹æ®ç±»åå建å®ä¾ |
| | | * |
| | | * @param className ç±»å |
| | | * @return å®ä½ |
| | | */ |
| | | public static Object createInstance(String className) { |
| | | try { |
| | | Class clazz = Class.forName(className); |
| | | Object obj = clazz.newInstance(); |
| | | |
| | | return obj; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åBean |
| | | * |
| | | * @param className ç±»å |
| | | * @return Bean |
| | | */ |
| | | public static Object getBean(String className) { |
| | | try { |
| | | Object obj = SpringContextHelper.getBean(className); |
| | | |
| | | return obj; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åç¶Mapper |
| | | * |
| | | * @param name Mapperå |
| | | * @return BaseMapper |
| | | */ |
| | | public static BasicMapper getBasicMapper(String name) { |
| | | if (StringHelper.isEmpty(name)) { |
| | | return null; |
| | | } |
| | | |
| | | Object obj = getBean(name.trim() + "Mapper"); |
| | | if (!(obj instanceof BasicMapper)) { |
| | | return null; |
| | | } |
| | | |
| | | return (BasicMapper) obj; |
| | | } |
| | | |
| | | /** |
| | | * è·å空é´ç¶Mapper |
| | | * |
| | | * @param name Mapperå |
| | | * @return GeomBaseMapper |
| | | */ |
| | | public static GeomBaseMapper getGeoBaseMapper(String name) { |
| | | if (StringHelper.isEmpty(name)) { |
| | | return null; |
| | | } |
| | | |
| | | Object obj = getBean(name.trim() + "Mapper"); |
| | | if (!(obj instanceof GeomBaseMapper)) { |
| | | return null; |
| | | } |
| | | |
| | | return (GeomBaseMapper) obj; |
| | | } |
| | | |
| | | /** |
| | | * è·åç±»å |
| | | * |
| | | * @param baseMapper ç¶Mapper |
| | | * @return ç±»å |
| | | */ |
| | | public static String getClassName(BaseMapper baseMapper) { |
| | | Type[] genericInterfaces = AopUtils.getTargetClass(baseMapper).getGenericInterfaces(); |
| | | |
| | | return genericInterfaces[0].getTypeName(); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®Mapperç±»åè·åå®ä½ç±»çClass |
| | | * |
| | | * @param className Mapperç±»å |
| | | * @return å®ä½ç±»çClass |
| | | */ |
| | | public static Class getEntityClass(String className) { |
| | | try { |
| | | className = className.replace(".mapper", ".entity").replace("Mapper", "Entity"); |
| | | Class clazz = Class.forName(className); |
| | | |
| | | return clazz; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ ¹æ®BaseMapperå建å®ä½ç±» |
| | | * |
| | | * @param baseMapper ç¶Mapper |
| | | * @return å®ä½ç±» |
| | | */ |
| | | public static Object createEntityByMapper(BaseMapper baseMapper) { |
| | | String className = getClassName(baseMapper); |
| | | className = className.replace(".mapper", ".entity").replace("Mapper", "Entity"); |
| | | |
| | | return createInstance(className); |
| | | } |
| | | } |