1 源码解析
1.1 连接源码解析
1.2 mybaties 的sqlsession源码解析
1 源码解析
1.1 连接源码解析
Connection.java(两个方法属于同一个产品族,这是连接的父类)
mysql获取的是同一产品族下的statement和同一产品族下的preparestatement
oracle也是
Statement createStatement() throws SQLException; /** * Creates a <code>PreparedStatement</code> object for sending * parameterized SQL statements to the database. * <P> * A SQL statement with or without IN parameters can be * pre-compiled and stored in a <code>PreparedStatement</code> object. This * object can then be used to efficiently execute this statement * multiple times. * * <P><B>Note:</B> This method is optimized for handling * parametric SQL statements that benefit from precompilation. If * the driver supports precompilation, * the method <code>prepareStatement</code> will send * the statement to the database for precompilation. Some drivers * may not support precompilation. In this case, the statement may * not be sent to the database until the <code>PreparedStatement</code> * object is executed. This has no direct effect on users; however, it does * affect which methods throw certain <code>SQLException</code> objects. * <P> * Result sets created using the returned <code>PreparedStatement</code> * object will by default be type <code>TYPE_FORWARD_ONLY</code> * and have a concurrency level of <code>CONCUR_READ_ONLY</code>. * The holdability of the created result sets can be determined by * calling {@link #getHoldability}. * * @param sql an SQL statement that may contain one or more '?' IN * parameter placeholders * @return a new default <code>PreparedStatement</code> object containing the * pre-compiled SQL statement * @exception SQLException if a database access error occurs * or this method is called on a closed connection */ PreparedStatement prepareStatement(String sql) throws SQLException; /** * Creates a <code>CallableStatement</code> object for calling * database stored procedures. * The <code>CallableStatement</code> object provides * methods for setting up its IN and OUT parameters, and * methods for executing the call to a stored procedure. * * <P><B>Note:</B> This method is optimized for handling stored * procedure call statements. Some drivers may send the call * statement to the database when the method <code>prepareCall</code> * is done; others * may wait until the <code>CallableStatement</code> object * is executed. This has no * direct effect on users; however, it does affect which method * throws certain SQLExceptions. * <P> * Result sets created using the returned <code>CallableStatement</code> * object will by default be type <code>TYPE_FORWARD_ONLY</code> * and have a concurrency level of <code>CONCUR_READ_ONLY</code>. * The holdability of the created result sets can be determined by * calling {@link #getHoldability}. * * @param sql an SQL statement that may contain one or more '?' * parameter placeholders. Typically this statement is specified using JDBC * call escape syntax. * @return a new default <code>CallableStatement</code> object containing the * pre-compiled SQL statement * @exception SQLException if a database access error occurs * or this method is called on a closed connection */
Statement.java(executeQuery方法和executeUpdate方法属于同一个产品族)
ResultSet executeQuery(String sql) throws SQLException; /** * Executes the given SQL statement, which may be an <code>INSERT</code>, * <code>UPDATE</code>, or <code>DELETE</code> statement or an * SQL statement that returns nothing, such as an SQL DDL statement. *<p> * <strong>Note:</strong>This method cannot be called on a * <code>PreparedStatement</code> or <code>CallableStatement</code>. * @param sql an SQL Data Manipulation Language (DML) statement, such as <code>INSERT</code>, <code>UPDATE</code> or * <code>DELETE</code>; or an SQL statement that returns nothing, * such as a DDL statement. * * @return either (1) the row count for SQL Data Manipulation Language (DML) statements * or (2) 0 for SQL statements that return nothing * * @exception SQLException if a database access error occurs, * this method is called on a closed <code>Statement</code>, the given * SQL statement produces a <code>ResultSet</code> object, the method is called on a * <code>PreparedStatement</code> or <code>CallableStatement</code> * @throws SQLTimeoutException when the driver has determined that the * timeout value that was specified by the {@code setQueryTimeout} * method has been exceeded and has at least attempted to cancel * the currently running {@code Statement} */ int executeUpdate(String sql) throws SQLException; /** * Releases this <code>Statement</code> object's database * and JDBC resources immediately instead of waiting for * this to happen when it is automatically closed. * It is generally good practice to release resources as soon as * you are finished with them to avoid tying up database * resources. * <P> * Calling the method <code>close</code> on a <code>Statement</code> * object that is already closed has no effect. * <P> * <B>Note:</B>When a <code>Statement</code> object is * closed, its current <code>ResultSet</code> object, if one exists, is * also closed. * * @exception SQLException if a database access error occurs */
1.2 mybaties 的sqlsession源码解析
sqlsessionfactory:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.apache.ibatis.session; import java.sql.Connection; public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean var1); SqlSession openSession(Connection var1); SqlSession openSession(TransactionIsolationLevel var1); SqlSession openSession(ExecutorType var1); SqlSession openSession(ExecutorType var1, boolean var2); SqlSession openSession(ExecutorType var1, TransactionIsolationLevel var2); SqlSession openSession(ExecutorType var1, Connection var2); Configuration getConfiguration(); }
子类1:SqlSessionManager
public Configuration getConfiguration() {
return this.sqlSessionFactory.getConfiguration();
}
public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
return this.sqlSessionFactory.openSession(execType, level);
}
子类2:
public Configuration getConfiguration() { return this.configuration; } public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) { return this.openSessionFromDataSource(execType, level, false); } private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; DefaultSqlSession var8; try { Environment environment = this.configuration.getEnvironment(); //获取环境变量 TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment); //初始化事务工厂 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //通过工厂获得事务对象 Executor executor = this.configuration.newExecutor(tx, execType); //通过事务入参获取执行器 var8 = new DefaultSqlSession(this.configuration, executor, autoCommit); //返回defaultsqlsession } catch (Exception var12) { this.closeTransaction(tx); throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12); } finally { ErrorContext.instance().reset(); }
诸葛