(1)SqlSessionDaoSupport 封装了SqlSession(通过动态代理SqlSession来选择数据源的方式实现分库策略),SqlSessionFactoryBean,Map<DataSource, SqlSessionTemplate>。
(2)SqlSessionFactoryBean封装了DataSource,SqlSessionFactory(封装了Configuration(封装环境元素(数据源和事务)、 属性、类型别名、typeHandler、mapper、setting、插件】),SqlSession(主要封装了Configuration对象、Executor对象、是否自动提交)),Map<String, ShardStrategy>(分库逻辑)
2. Mybatis3.06 核心类:Configuration和SqlSessionFactory,SqlSession,Executor
(1)真实干活的类:SqlSessionFactory主要是工作必须是提供获取SqlSession的方法,同时还提供了一个获取Configuration的方法。
(2)真实干活的类:SqlSession类主要封装了Configuration对象、Executor对象、是否自动提交
(3)Configuration对象,其实他就是对xml配置文件的对象映射,封装了:环境元素(数据源和事务)、 属性、类型别名、typeHandler、mapper、setting、插件。
(4)真实干活的类:Executor对象执行SQL语句, 可以通过配置插件修改SQL(插件保存在Configuration中,通过configuration.newStatementHandler调用),封装了:Configuration,Transaction(通过getConnection()获取连接)
(5) 真正处理业务逻辑的是StatementHandler(持有核心类MappedStatement,ms又持有DynamicSqlSource,DynamicContext,所有的sql解析,sql路由逻辑,插件增强功能等等都在这里完成。
/** * @author Clinton Begin */ public class SimpleExecutor extends BaseExecutor { public SimpleExecutor(Configuration configuration, Transaction transaction) { super(configuration, transaction); } @Override public int doUpdate(MappedStatement ms, Object parameter) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null); stmt = prepareStatement(handler, ms.getStatementLog()); return handler.update(stmt); } finally { closeStatement(stmt); } } @Override public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt = prepareStatement(handler, ms.getStatementLog()); return handler.<E>query(stmt, resultHandler); } finally { closeStatement(stmt); } } @Override public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException { return Collections.emptyList(); } private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; Connection connection = getConnection(statementLog); stmt = handler.prepare(connection); handler.parameterize(stmt); return stmt; }