■ SQL模板的定义
public enum SqlMethod {
INSERT_ONE("insert", "插入一条数据(选择字段插入)", ""),
UPSERT_ONE("upsert", "Phoenix插入一条数据(选择字段插入)", ""),
DELETE_BY_ID("deleteById", "根据ID 删除一条数据", ""),
DELETE_BY_MAP("deleteByMap", "根据columnMap 条件删除记录", ""),
DELETE("delete", "根据 entity 条件删除记录", ""),
DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量删除数据", ""),
LOGIC_DELETE_BY_ID("deleteById", "根据ID 逻辑删除一条数据", ""),
LOGIC_DELETE_BY_MAP("deleteByMap", "根据columnMap 条件逻辑删除记录", ""),
LOGIC_DELETE("delete", "根据 entity 条件逻辑删除记录", ""),
LOGIC_DELETE_BATCH_BY_IDS("deleteBatchIds", "根据ID集合,批量逻辑删除数据", ""),
UPDATE_BY_ID("updateById", "根据ID 选择修改数据", ""),
UPDATE("update", "根据 whereEntity 条件,更新记录", ""),
LOGIC_UPDATE_BY_ID("updateById", "根据ID 修改数据", ""),
SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s} %s"),
SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", ""),
SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", ""),
SELECT_ONE("selectOne", "查询满足条件一条数据", ""),
SELECT_COUNT("selectCount", "查询满足条件总记录数", ""),
SELECT_LIST("selectList", "查询满足条件所有数据", ""),
SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", ""),
SELECT_MAPS("selectMaps", "查询满足条件所有数据", ""),
SELECT_MAPS_PAGE("selectMapsPage", "查询满足条件所有数据(并翻页)", ""),
SELECT_OBJS("selectObjs", "查询满足条件所有数据", "");
……
}
■ SQL的拼接 & 生成
public class DeleteById extends AbstractMethod {
public DeleteById() {
}
/** 【自动生成并注入SQL语句】
* @param mapperClass 当前操作的接口:XXXMapper
* @param modelClass 当前操作的实体类对象:Model
* @param tableInfo:数据表相关的信息,拼接SQL时需获取表名、主键
* @return MappedStatement
*/
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
// 获取枚举类SQLMethod中定义的模板
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
String sql;
SqlSource sqlSource;
if (tableInfo.isLogicDelete()) {
// 拼接逻辑删除(update)的SQL语句
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
// sqlSource 里有生成的sql语句等信息
sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Object.class);
return this.addUpdateMappedStatement(mapperClass, modelClass, this.getMethod(sqlMethod), sqlSource);
} else {
// 拼接物理删除的SQL语句
sqlMethod = SqlMethod.DELETE_BY_ID;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
// sqlSource 里有生成的sql语句等信息
sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Object.class);
return this.addDeleteMappedStatement(mapperClass, this.getMethod(sqlMethod), sqlSource);
}
}
}
return前最后一步是addDeleteMappedStatement,其底层实现为:
public abstract class AbstractMethod implements Constants {
……
/**
*【映射Mapper中的方法 与 SQL语句】
* @param mapperClass 自定义的Mapper类
* @param sqlSource SQL语句处理对象
* @param SqlCommandType SQL语句类型
* @param
*/
protected MappedStatement addMappedStatement(Class> mapperClass, String id, SqlSource sqlSource, SqlCommandType sqlCommandType, Class> parameterType, String resultMap, Class> resultType, KeyGenerator keyGenerator, String keyProperty, String keyColumn) {
// 生成statementName,如com.xx.xx.EmployeeMapper.deleteById
String statementName = mapperClass.getName() + "." + id;
// 判断是否已缓存进map的statement
if (this.hasMappedStatement(statementName)) {
logger.warn("[" + statementName + "] Has been loaded by XML or SqlProvider or Mybatis's Annotation, so ignoring this injection for [" + this.getClass() + "]");
return null;
} else {
// 缓存逻辑处理
boolean isSelect = false;
if (sqlCommandType == SqlCommandType.SELECT) {
isSelect = true;
}
// 将生成的MappedStatement加入Configuration
return this.builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType, (Integer)null, (Integer)null, (String)null, parameterType, resultMap, resultType, (ResultSetType)null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn, this.configuration.getDatabaseId(), this.languageDriver, (String)null);
}
}
……
}
可见项目启动时,MP会逐个分析自定义Mapper中的方法,
并构造好对应的Sql,再缓存至Configuration中的mappedstatement中。