First, the key class
Configuration
The configuration class.
MapperProxy
Proxy object for the Mapper interface of Mybatis. Perform invoke.
Creation process:
MapperMethod
The mapper proxy object finally executes MapperMethod.
SqlSession
SqlSession is created by SqlSessionFactory and contains all interfaces to manipulate data. The default implementation is DefaultSqlSession, and the Spring-managed implementation is SqlSessionTemplate.
The SqlSessionFactory interface is responsible for opening sessions.
SqlSession contains the following methods:
Relationship with SqlSessionFactory and default implementation class:
Executor
The execution engine of SQL, which is responsible for adding, deleting, modifying, and querying. Each Sqlsession has an Executor object. Equivalent to the encapsulated version of STATEMENT in JDBC.
BaseExecutor has three subclasses that adopt the adapter pattern. BaseExecutor’s Query checks the level 1 cache first,
-
SimpleExecutor is basically closed when statement runs out.
-
ReuseExecutor reuses statement pairs.
-
BatchExecutor processes batch SQL.
CachingExecutor is a level 2 cache for executors.
Inheritance relationship:
LocalCache level 1 cache call procedure
The cache
PerpetualCache lives in executors, one Executor for every SQLSession, which is destroyed over the life of the SQLSession session.
-
Level 1 cache is used by default and stores results based on the KEY level and Hash. The same session can share caches. Not in multiple sessions or distributed environments.
-
Level 2 cache is manually enabled. It is the namespace level. Same as the application life cycle.
Query data in the following order: Level 2 cache – > Level 1 Cache – > database.
Other cache
BoundSql
Object that holds SQL and parameters.
StatementHandler interface
JDBC statement encapsulation, used for SQL parameter transfer. Exists in Executor.
Four implementations:
-
Roting** Main creation and invocation of the other three.
-
Simple** Simple SQL processing. No parameters.
-
Prepared** preprocess SQL. Parameter, by DefaultParameterHandler setParameters processing parameters.
-
Call** is associated with stored procedures.
Preprocessing parameters.
ResultSetHandler
Results handler. The BaseStatementHandler executor is then used to obtain the results.
Call the process
SqlSessionTemplate agent DefaultSqlSession object.
SqlSession thread security problem
Conclusion: Using DefaultSqlSession directly is not thread-safe, SqlSessionTemplate, SqlSessionManager is safe, but uses proxy mode to consume memory.
DefaultSqlSession is not secure
DefaultSqlSession is not a thread-safe, SqlSession dimension connection. A SqlSession shared by two threads. Closing one thread causes the other thread to become unavailable. Get executor from SqlSession.
DefaultSqlSession is used safely
The Spring-managed SqlSessionTemplate is thread-safe. When getting a Connection, get sesson each time using a JDK proxy. Keep sesson in sessionHolder.
DefaultSqlSession is created in DefaultSqlSessionFactory, so all spring-managed CRUD calls will eventually be implemented in DefaultSqlSession.
SqlSessionManager is thread safe
SqlSessionManager is thread-safe, but maintains the current SQLSession using ThreadLocal. Also in proxy mode, the SQLSession is obtained each time.
Knowledge of agency
The JDK proxy proxies the interface and specifies the proxy class. So the SqlSession interface of the SqlSessionTemplate instance agent is finally implemented in DefaultSqlSession.
Equivalent to SqlSessionTemplate for DefaultSqlSession agent. DefaultSqlSession is the proxy object.
public class JdkProxy implements InvocationHandler { private Object target ; @override public Object invoke(Object proxy, Method Method, Object[] args) throws Throwable {system.out.println ("JDK dynamic proxy, listening start!" ); Object result = method.invoke(target, args); System.out.println("JDK dynamic proxy, listening end!" ); return result; } private Object getJDKProxy(Object targetObject){this.target = targetObject; //JDK dynamic proxies can only be used for classes that implement interfaces, Return proxy.newProxyInstance (targetobject.getClass ().getClassLoader(), targetObject.getClass().getInterfaces(), this); } public static void main(String[] args) { JdkProxy jdkProxy = new JdkProxy(); UserManager user = (UserManager) jdkProxy.getjdkProxy (new UserManagerImpl()); // Get the proxy object user.adduser ("admin", "123123"); // execute the new method}}Copy the code
Refer to the connection
Segmentfault.com/a/119000002…