|
| 1 | +package tk.mybatis.mapper.generator; |
| 2 | + |
| 3 | +import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3SimpleImpl; |
| 4 | + |
| 5 | +import java.text.MessageFormat; |
| 6 | + |
| 7 | +import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; |
| 8 | + |
| 9 | +/** |
| 10 | + * 可以通过MBG1.3.4+版本提供的table元素的mapperName属性设置统一的名称,使用{0}作为实体类名的占位符。 |
| 11 | + * <p> |
| 12 | + * 用法: |
| 13 | + * <pre> |
| 14 | + * <context id="Mysql" targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3SimpleImpl" defaultModelType="flat"> |
| 15 | + * </context> |
| 16 | + * </pre> |
| 17 | + * </p> |
| 18 | + * |
| 19 | + * @author liuzh |
| 20 | + * @since 2016-09-04 09:57 |
| 21 | + */ |
| 22 | +public class TkMyBatis3SimpleImpl extends IntrospectedTableMyBatis3SimpleImpl { |
| 23 | + |
| 24 | + @Override |
| 25 | + protected String calculateMyBatis3XmlMapperFileName() { |
| 26 | + StringBuilder sb = new StringBuilder(); |
| 27 | + if (stringHasValue(tableConfiguration.getMapperName())) { |
| 28 | + String mapperName = tableConfiguration.getMapperName(); |
| 29 | + int ind = mapperName.lastIndexOf('.'); |
| 30 | + if (ind != -1) { |
| 31 | + mapperName = mapperName.substring(ind + 1); |
| 32 | + } |
| 33 | + //支持mapperName = "{0}Dao" 等用法 |
| 34 | + sb.append(MessageFormat.format(mapperName, fullyQualifiedTable.getDomainObjectName())); |
| 35 | + sb.append(".xml"); //$NON-NLS-1$ |
| 36 | + } else { |
| 37 | + sb.append(fullyQualifiedTable.getDomainObjectName()); |
| 38 | + sb.append("Mapper.xml"); //$NON-NLS-1$ |
| 39 | + } |
| 40 | + return sb.toString(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void calculateJavaClientAttributes() { |
| 45 | + if (context.getJavaClientGeneratorConfiguration() == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + StringBuilder sb = new StringBuilder(); |
| 50 | + sb.append(calculateJavaClientImplementationPackage()); |
| 51 | + sb.append('.'); |
| 52 | + sb.append(fullyQualifiedTable.getDomainObjectName()); |
| 53 | + sb.append("DAOImpl"); //$NON-NLS-1$ |
| 54 | + setDAOImplementationType(sb.toString()); |
| 55 | + |
| 56 | + sb.setLength(0); |
| 57 | + sb.append(calculateJavaClientInterfacePackage()); |
| 58 | + sb.append('.'); |
| 59 | + sb.append(fullyQualifiedTable.getDomainObjectName()); |
| 60 | + sb.append("DAO"); //$NON-NLS-1$ |
| 61 | + setDAOInterfaceType(sb.toString()); |
| 62 | + |
| 63 | + sb.setLength(0); |
| 64 | + sb.append(calculateJavaClientInterfacePackage()); |
| 65 | + sb.append('.'); |
| 66 | + if (stringHasValue(tableConfiguration.getMapperName())) { |
| 67 | + //支持mapperName = "{0}Dao" 等用法 |
| 68 | + sb.append(MessageFormat.format(tableConfiguration.getMapperName(), fullyQualifiedTable.getDomainObjectName())); |
| 69 | + } else { |
| 70 | + sb.append(fullyQualifiedTable.getDomainObjectName()); |
| 71 | + sb.append("Mapper"); //$NON-NLS-1$ |
| 72 | + } |
| 73 | + setMyBatis3JavaMapperType(sb.toString()); |
| 74 | + |
| 75 | + sb.setLength(0); |
| 76 | + sb.append(calculateJavaClientInterfacePackage()); |
| 77 | + sb.append('.'); |
| 78 | + if (stringHasValue(tableConfiguration.getSqlProviderName())) { |
| 79 | + //支持mapperName = "{0}SqlProvider" 等用法 |
| 80 | + sb.append(MessageFormat.format(tableConfiguration.getSqlProviderName(), fullyQualifiedTable.getDomainObjectName())); |
| 81 | + } else { |
| 82 | + sb.append(fullyQualifiedTable.getDomainObjectName()); |
| 83 | + sb.append("SqlProvider"); //$NON-NLS-1$ |
| 84 | + } |
| 85 | + setMyBatis3SqlProviderType(sb.toString()); |
| 86 | + } |
| 87 | +} |
0 commit comments