一、工厂方法模式实现sqlsession
(从别人博客复制的图)
1.Sqlsession接口
在sqlsession接口中包含了所有可能执行的sql语句。而Defaultsqlsession是他的实现类,实现了其中的方法。
1 public interface SqlSession extends Closeable { 2 3 /** 4 * Retrieve a single row mapped from the statement key 5 * @param <T> the returned object type 6 * @param statement 7 * @return Mapped object 8 */ 9 <T> T selectOne(String statement); 10 /** 11 * Retrieve a list of mapped objects from the statement key and parameter. 12 * @param <E> the returned list element type 13 * @param statement Unique identifier matching the statement to use. 14 * @return List of mapped object 15 */ 16 <E> List<E> selectList(String statement);
2.DefaultSqlSession
1 public class DefaultSqlSession implements SqlSession { 2 3 private Configuration configuration; 4 private Executor executor; 5 6 private boolean dirty; 7 8 public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) { 9 try { 10 MappedStatement ms = configuration.getMappedStatement(statement); 11 List<E> result = executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); 12 return result; 13 } catch (Exception e) { 14 throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); 15 } finally { 16 ErrorContext.instance().reset(); 17 } 18 }
3.SqlSessionFactory接口
sqlsessionfactory类中有opsession方法,用来创建sqlsession。
4、DefaultSqlSessionFactory类实现了SqlSessionFactory