首先附上我扩展后项目的github地址:点击打开链接
为了项目的需要,需要在生成的Mapper里添加enableSelectNotDeleteAll、enableSelectNotDeleteByPrimaryKey方法。下面介绍实现的过程。
第一步:在generatorConfig.xml对应的dtd文件里添加table的属性,如下:
你需要什么属性就可以在这添加什么属性,添加完成后xml文件里就可以写对应的属性了,如:
第二步:第一步修改完成后需要对TableConfiguration和MyBatisGeneratorConfigurationParser两个类添加相关修改,在
TableConfiguration里添加属性,并加上set和get方法,
/** The insert statement enabled. */
private boolean insertStatementEnabled;
private boolean insertSelectiveStatementEnabled;
/** The select by primary key statement enabled. */
private boolean selectByPrimaryKeyStatementEnabled;
private boolean selectNotDeleteByPrimaryKeyStatementEnabled;
/** The select by example statement enabled. */
private boolean selectByExampleStatementEnabled;
/** The update by primary key statement enabled. */
private boolean updateByPrimaryKeyStatementEnabled;
/** The delete by primary key statement enabled. */
private boolean deleteByPrimaryKeyStatementEnabled;
/** The delete by example statement enabled. */
private boolean deleteByExampleStatementEnabled;
/** The count by example statement enabled. */
private boolean countByExampleStatementEnabled;
/** The update by example statement enabled. */
private boolean updateByExampleStatementEnabled;
private boolean selectAllStatementEnabled;
private boolean selectNotDeleteAllStatementEnabled;
private boolean updateByExampleSelectiveStatementEnabled;
然后在构造方法那里给上默认设置,true代表默认为生成,false代码默认不生成;
public TableConfiguration(Context context) {
super();
this.modelType = context.getDefaultModelType();
columnOverrides = new ArrayList();
ignoredColumns = new HashMap();
insertStatementEnabled = false;
insertSelectiveStatementEnabled = true;
selectByPrimaryKeyStatementEnabled = true;
selectNotDeleteByPrimaryKeyStatementEnabled = false;
selectByExampleStatementEnabled = true;
updateByPrimaryKeyStatementEnabled = false;
deleteByPrimaryKeyStatementEnabled = true;
deleteByExampleStatementEnabled = true;
countByExampleStatementEnabled = true;
updateByExampleStatementEnabled = false;
updateByExampleSelectiveStatementEnabled = true;
selectAllStatementEnabled = false;
selectNotDeleteAllStatementEnabled = false;
}
然后对下面方法做相关修改:
public boolean areAnyStatementsEnabled() {
return selectByExampleStatementEnabled
|| selectByPrimaryKeyStatementEnabled || insertStatementEnabled
|| insertSelectiveStatementEnabled
|| updateByPrimaryKeyStatementEnabled
|| deleteByExampleStatementEnabled
|| deleteByPrimaryKeyStatementEnabled
|| countByExampleStatementEnabled
|| updateByExampleStatementEnabled
|| updateByExampleSelectiveStatementEnabled
|| selectAllStatementEnabled
|| selectNotDeleteAllStatementEnabled
|| selectNotDeleteByPrimaryKeyStatementEnabled;
}
在MyBatisGeneratorConfigurationParser类里注意修改parseTable方法
protected void parseTable(Context context, Node node) {
TableConfiguration tc = new TableConfiguration(context);
context.addTableConfiguration(tc);
Properties attributes = parseAttributes(node);
String catalog = attributes.getProperty("catalog"); //$NON-NLS-1$
String schema = attributes.getProperty("schema"); //$NON-NLS-1$
String tableName = attributes.getProperty("tableName"); //$NON-NLS-1$
String boObjectName = attributes.getProperty("boObjectName"); //$NON-NLS-1$
String domainObjectName = attributes.getProperty("domainObjectName"); //$NON-NLS-1$
String alias = attributes.getProperty("alias"); //$NON-NLS-1$
String enableInsert = attributes.getProperty("enableInsert"); //$NON-NLS-1$
String enableInsertSelective = attributes.getProperty("enableInsertSelective"); //$NON-NLS-1$
String enableSelectByPrimaryKey = attributes
.getProperty("enableSelectByPrimaryKey"); //$NON-NLS-1$
String enableSelectNotDeleteByPrimaryKey = attributes
.getProperty("enableSelectNotDeleteByPrimaryKey"); //$NON-NLS-1$
String enableSelectByExample = attributes
.getProperty("enableSelectByExample"); //$NON-NLS-1$
String enableUpdateByPrimaryKey = attributes
.getProperty("enableUpdateByPrimaryKey"); //$NON-NLS-1$
String enableSelectAll = attributes
.getProperty("enableSelectAll");
String enableSelectNotDeleteAll = attributes
.getProperty("enableSelectNotDeleteAll");
String enableDeleteByPrimaryKey = attributes
.getProperty("enableDeleteByPrimaryKey"); //$NON-NLS-1$
String enableSelcetNotDeleteByPrimaryKey = attributes
.getProperty("enableSelectNotDeleteByPrimaryKey");
String enableDeleteByExample = attributes
.getProperty("enableDeleteByExample"); //$NON-NLS-1$
String enableCountByExample = attributes
.getProperty("enableCountByExample"); //$NON-NLS-1$
String enableUpdateByExample = attributes
.getProperty("enableUpdateByExample"); //$NON-NLS-1$
String enableUpdateByExampleSelective = attributes
.getProperty("enableUpdateByExampleSelective"); //$NON-NLS-1$
String selectByPrimaryKeyQueryId = attributes
.getProperty("selectByPrimaryKeyQueryId"); //$NON-NLS-1$
String selectByExampleQueryId = attributes
.getProperty("selectByExampleQueryId"); //$NON-NLS-1$
String modelType = attributes.getProperty("modelType"); //$NON-NLS-1$
String escapeWildcards = attributes.getProperty("escapeWildcards"); //$NON-NLS-1$
String delimitIdentifiers = attributes
.getProperty("delimitIdentifiers"); //$NON-NLS-1$
String delimitAllColumns = attributes.getProperty("delimitAllColumns"); //$NON-NLS-1$
String mapperName = attributes.getProperty("mapperName"); //$NON-NLS-1$
String sqlProviderName = attributes.getProperty("sqlProviderName"); //$NON-NLS-1$
if (stringHasValue(catalog)) {
tc.setCatalog(catalog);
}
if (stringHasValue(schema)) {
tc.setSchema(schema);
}
if (stringHasValue(tableName)) {
tc.setTableName(tableName);
}
if (stringHasValue(boObjectName)) {
tc.setBoObjectName(boObjectName);
}
if (stringHasValue(domainObjectName)) {
tc.setDomainObjectName(domainObjectName);
}
if (stringHasValue(alias)) {
tc.setAlias(alias);
}
if (stringHasValue(enableInsert)) {
tc.setInsertStatementEnabled(isTrue(enableInsert));
}
if (stringHasValue(enableInsertSelective)) {
tc.setInsertSelectiveStatementEnabled(isTrue(enableInsertSelective));
}
if (stringHasValue(enableSelectByPrimaryKey)) {
tc.setSelectByPrimaryKeyStatementEnabled(
isTrue(enableSelectByPrimaryKey));
}
if (stringHasValue(enableSelectNotDeleteByPrimaryKey)) {
tc.setSelectByPrimaryKeyStatementEnabled(
isTrue(enableSelectNotDeleteByPrimaryKey));
}
if (stringHasValue(enableSelectByExample)) {
tc.setSelectByExampleStatementEnabled(
isTrue(enableSelectByExample));
}
if (stringHasValue(enableSelectAll)) {
tc.setSelectAllStatementEnabled(
isTrue(enableSelectAll));
}
if (stringHasValue(enableSelectNotDeleteAll)) {
tc.setSelectNotDeleteAllStatementEnabled(
isTrue(enableSelectNotDeleteAll));
}
if (stringHasValue(enableSelcetNotDeleteByPrimaryKey)) {
tc.setSelectNotDeleteByPrimaryKeyStatementEnabled(isTrue(enableSelcetNotDeleteByPrimaryKey));
}
if (stringHasValue(enableUpdateByPrimaryKey)) {
tc.setUpdateByPrimaryKeyStatementEnabled(
isTrue(enableUpdateByPrimaryKey));
}
if (stringHasValue(enableDeleteByPrimaryKey)) {
tc.setDeleteByPrimaryKeyStatementEnabled(
isTrue(enableDeleteByPrimaryKey));
}
if (stringHasValue(enableDeleteByExample)) {
tc.setDeleteByExampleStatementEnabled(
isTrue(enableDeleteByExample));
}
if (stringHasValue(enableCountByExample)) {
tc.setCountByExampleStatementEnabled(
isTrue(enableCountByExample));
}
if (stringHasValue(enableUpdateByExample)) {
tc.setUpdateByExampleStatementEnabled(
isTrue(enableUpdateByExample));
}
if (stringHasValue(enableUpdateByExampleSelective)) {
tc.setUpdateByExampleSelectiveStatementEnabled(
isTrue(enableUpdateByExampleSelective));
}
if (stringHasValue(selectByPrimaryKeyQueryId)) {
tc.setSelectByPrimaryKeyQueryId(selectByPrimaryKeyQueryId);
}
if (stringHasValue(selectByExampleQueryId)) {
tc.setSelectByExampleQueryId(selectByExampleQueryId);
}
if (stringHasValue(modelType)) {
tc.setConfiguredModelType(modelType);
}
if (stringHasValue(escapeWildcards)) {
tc.setWildcardEscapingEnabled(isTrue(escapeWildcards));
}
if (stringHasValue(delimitIdentifiers)) {
tc.setDelimitIdentifiers(isTrue(delimitIdentifiers));
}
if (stringHasValue(delimitAllColumns)) {
tc.setAllColumnDelimitingEnabled(isTrue(delimitAllColumns));
}
if (stringHasValue(mapperName)) {
tc.setMapperName(mapperName);
}
if (stringHasValue(sqlProviderName)) {
tc.setSqlProviderName(sqlProviderName);
}
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
if (childNode.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
if ("property".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseProperty(tc, childNode);
} else if ("columnOverride".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseColumnOverride(tc, childNode);
} else if ("ignoreColumn".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseIgnoreColumn(tc, childNode);
} else if ("ignoreColumnsByRegex".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseIgnoreColumnByRegex(tc, childNode);
} else if ("generatedKey".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseGeneratedKey(tc, childNode);
} else if ("columnRenamingRule".equals(childNode.getNodeName())) { //$NON-NLS-1$
parseColumnRenamingRule(tc, childNode);
}
}
}
第三步:分别在XMLMapperGenerator和JavaMapperGenerator类里添加相关方法,对应方法的具体实现参考其他方法做简单修改即可,
在JavaMapperGenerator的getCompilationUnits方法里添加相关方法,
addCountByExampleMethod(interfaze);
addDeleteByExampleMethod(interfaze);
addDeleteByPrimaryKeyMethod(interfaze);
addInsertMethod(interfaze);
addInsertSelectiveMethod(interfaze);
addSelectByExampleWithBLOBsMethod(interfaze);
addSelectByExampleWithoutBLOBsMethod(interfaze);
addSelectByPrimaryKeyMethod(interfaze);
addUpdateByExampleSelectiveMethod(interfaze);
addUpdateByExampleWithBLOBsMethod(interfaze);
addUpdateByExampleWithoutBLOBsMethod(interfaze);
addUpdateByPrimaryKeySelectiveMethod(interfaze);
addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);
addSelectAllMethodGenerator(interfaze);
addSelectNotDeleteAllMethodGenerator(interfaze);
addSelectNotDeleteByPrimaryKeyMethod(interfaze);
List answer = new ArrayList();
XMLMapperGenerator的getSqlMapElement添加相关方法:
addResultMapWithoutBLOBsElement(answer);
addResultMapWithBLOBsElement(answer);
addExampleWhereClauseElement(answer);
addMyBatis3UpdateByExampleWhereClauseElement(answer);
addBaseColumnListElement(answer);
addBlobColumnListElement(answer);
addSelectByExampleWithBLOBsElement(answer);
addSelectByExampleWithoutBLOBsElement(answer);
addSelectByPrimaryKeyElement(answer);
addDeleteByPrimaryKeyElement(answer);
addDeleteByExampleElement(answer);
addInsertElement(answer);
addInsertSelectiveElement(answer);
addCountByExampleElement(answer);
addUpdateByExampleSelectiveElement(answer);
addUpdateByExampleWithBLOBsElement(answer);
addUpdateByExampleWithoutBLOBsElement(answer);
addUpdateByPrimaryKeySelectiveElement(answer);
addUpdateByPrimaryKeyWithBLOBsElement(answer);
addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
addSelectAllElement(answer);
addSelectNotDeleteAllElement(answer);
addSelectNotDeleteByPrimaryKeyElement(answer);