|
| 1 | +package com.isea533.mybatis; |
| 2 | + |
| 3 | +import com.isea533.mybatis.mapperhelper.MapperHelper; |
| 4 | +import org.apache.ibatis.builder.annotation.ProviderSqlSource; |
| 5 | +import org.apache.ibatis.mapping.MappedStatement; |
| 6 | +import org.mybatis.spring.SqlSessionTemplate; |
| 7 | +import org.springframework.beans.BeansException; |
| 8 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 9 | + |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Properties; |
| 12 | + |
| 13 | +/** |
| 14 | + * @author liuzh |
| 15 | + */ |
| 16 | +public class MappperBeanPostProcessor implements BeanPostProcessor { |
| 17 | + |
| 18 | + private final MapperHelper mapperHelper = new MapperHelper(); |
| 19 | + |
| 20 | + private boolean runed = false; |
| 21 | + |
| 22 | + public void setProperties(Properties properties) { |
| 23 | + String UUID = properties.getProperty("UUID"); |
| 24 | + if (UUID != null && UUID.length() > 0) { |
| 25 | + mapperHelper.setUUID(UUID); |
| 26 | + } |
| 27 | + String IDENTITY = properties.getProperty("IDENTITY"); |
| 28 | + if (IDENTITY != null && IDENTITY.length() > 0) { |
| 29 | + mapperHelper.setIDENTITY(IDENTITY); |
| 30 | + } |
| 31 | + String seqFormat = properties.getProperty("seqFormat"); |
| 32 | + if (seqFormat != null && seqFormat.length() > 0) { |
| 33 | + mapperHelper.setSeqFormat(seqFormat); |
| 34 | + } |
| 35 | + String ORDER = properties.getProperty("ORDER"); |
| 36 | + if (ORDER != null && ORDER.length() > 0) { |
| 37 | + mapperHelper.setBEFORE(ORDER); |
| 38 | + } |
| 39 | + String cameHumpMap = properties.getProperty("cameHumpMap"); |
| 40 | + if (cameHumpMap != null && cameHumpMap.length() > 0) { |
| 41 | + mapperHelper.setCameHumpMap(cameHumpMap); |
| 42 | + } |
| 43 | + int mapperCount = 0; |
| 44 | + String mapper = properties.getProperty("mappers"); |
| 45 | + if (mapper != null && mapper.length() > 0) { |
| 46 | + String[] mappers = mapper.split(","); |
| 47 | + for (String mapperClass : mappers) { |
| 48 | + if (mapperClass.length() > 0) { |
| 49 | + mapperHelper.registerMapper(mapperClass); |
| 50 | + mapperCount++; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + if (mapperCount == 0) { |
| 55 | + throw new RuntimeException("通用Mapper没有配置任何有效的通用接口!"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 62 | + return bean; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 67 | + if (!runed && bean instanceof SqlSessionTemplate) { |
| 68 | + SqlSessionTemplate sqlSessionTemplate = (SqlSessionTemplate)bean; |
| 69 | + Collection<MappedStatement> collection = sqlSessionTemplate.getConfiguration().getMappedStatements(); |
| 70 | + for (Object object : collection) { |
| 71 | + if (object instanceof MappedStatement) { |
| 72 | + MappedStatement ms = (MappedStatement)object; |
| 73 | + if (mapperHelper.isMapperMethod(ms.getId())) { |
| 74 | + if (ms.getSqlSource() instanceof ProviderSqlSource) { |
| 75 | + mapperHelper.setSqlSource(ms); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + runed = true; |
| 81 | + } |
| 82 | + return bean; |
| 83 | + } |
| 84 | +} |
0 commit comments