1. package com.example.aninbatis.executor;
    2. import com.example.aninbatis.config.Configuration;
    3. import com.example.aninbatis.config.MappedStatement;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. /**
    8. * 基本执行器,主要处理一级缓存
    9. */
    10. public abstract class BaseExecutor implements Executor {
    11. private Map<String, List<Object>> oneLevelCache = new HashMap<>();
    12. @SuppressWarnings("unchecked")
    13. @Override
    14. public <T> List<T> query(MappedStatement mappedStatement, Configuration configuration, Object param) {
    15. // 获取带有值的sql语句
    16. String sql = mappedStatement.getSqlSource().getBoundSql(param).getSql();
    17. // 从一级缓存去根据sql语句获取查询结果
    18. List<Object> result = oneLevelCache.get(sql);
    19. if (result != null) {
    20. return (List<T>) result;
    21. }
    22. // 如果没有结果,则调用相应的处理器去处理
    23. result = queryFromDataBase(mappedStatement, configuration, param);
    24. oneLevelCache.put(sql, result);
    25. return (List<T>) result;
    26. }
    27. public abstract List<Object> queryFromDataBase(MappedStatement mappedStatement, Configuration configuration,
    28. Object param);
    29. }