1 sql配置文件
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Aug 14 16:08:36 CST 2015.
-->
Id, menu_name, menu_remark, menu_parent_id, menu_url, is_show, is_delete, operation_user_name, operation_time
</sql>
2 代码
private void sqlElement(List<XNode> list) throws Exception {
// 当全局配置中数据库产品名称不为null,先遍历一般SQL
if (configuration.getDatabaseId() != null) {
sqlElement(list, configuration.getDatabaseId());
}
sqlElement(list, null);
}
private void sqlElement(List<XNode> list, String requiredDatabaseId) throws Exception {
for (XNode context : list) {
// null
String databaseId = context.getStringAttribute("databaseId");
// Base_Column_List
String id = context.getStringAttribute("id");
// cn.vansky.schedule.time.menu.dao.MenuMapper.Base_Column_List
id = builderAssistant.applyCurrentNamespace(id, false);
// 一致,需要添加到sqlFragments里面
if (databaseIdMatchesCurrent(id, databaseId, requiredDatabaseId)) sqlFragments.put(id, context);
}
}
// 匹配当前配置的数据库是否与默认的数据库一致
private boolean databaseIdMatchesCurrent(String id, String databaseId, String requiredDatabaseId) {
if (requiredDatabaseId != null) {
// 当全局配置的数据库产品名称与配置的产品名称不一样,就不需要添加内容到sqlFragment里面
if (!requiredDatabaseId.equals(databaseId)) {
return false;
}
} else {
if (databaseId != null) {
return false;
}
// skip this fragment if there is a previous one with a not null databaseId
if (this.sqlFragments.containsKey(id)) {
XNode context = this.sqlFragments.get(id);
if (context.getStringAttribute("databaseId") != null) {
return false;
}
}
}
return true;
}
对多个SQL配置进行遍历。这里不再过多分析,下面就是最终结果。
这里为什么一次put生成2个对象呢,是因为MyBatis使用自己的StrictMap,在put方法里进行了重写。