MyBatis3 用的很多了,没有仔细梳理过。刚好忙中偷闲,有点空,慢慢体系化梳理一下。
mybatis官宣自己是个啥
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.
官网getting start
-
Installation
jar 引入 -
Building SqlSessionFactory from XML
-
Building SqlSessionFactory without XML
-
Acquiring a SqlSession from SqlSessionFactory
-
Exploring Mapped SQL Statements
通过官网的大体描述获知:
1.可以通过xml和代码配置
2.可以通过sqlSession查找mapper,mapper执行具体方法;也可以通过mapper的命名空间直接通过sqlSession执行。
官方更建议过mapper调用具体业务方法
BlogMapper mapper = session.getMapper(BlogMapper.class);
The second approach has a lot of advantages. First, it doesn’t depend on a string literal, so it’s much safer. Second, if your IDE has code completion, you can leverage that when navigating your mapped SQL statements.
###对上述描述的理解画图
Scope and Lifecycle
SqlSessionFactoryBuilder
This class can be instantiated, used and thrown away. There is no need to keep it around once you’ve created your SqlSessionFactory. Therefore the best scope for instances of SqlSessionFactoryBuilder is method scope (i.e. a local method variable). You can reuse the SqlSessionFactoryBuilder to build multiple SqlSessionFactory instances, but it’s still best not to keep it around to ensure that all of the XML parsing resources are freed up for more important things.
SqlSessionFactory
Once created, the SqlSessionFactory should exist for the duration of your application execution. There should be little or no reason to ever dispose of it or recreate it. It’s a best practice to not rebuild the SqlSessionFactory multiple times in an application run. Doing so should be considered a “bad smell”. Therefore the best scope of SqlSessionFactory is application scope. This can be achieved a number of ways. The simplest is to use a Singleton pattern or Static Singleton pattern.
SqlSession
Each thread should have its own instance of SqlSession. Instances of SqlSession are not to be shared and are not thread safe. Therefore the best scope is request or method scope. Never keep references to a SqlSession instance in a static field or even an instance field of a class. Never keep references to a SqlSession in any sort of managed scope, such as HttpSession of the Servlet framework. If you’re using a web framework of any sort, consider the SqlSession to follow a similar scope to that of an HTTP request. In other words, upon receiving an HTTP request, you can open a SqlSession, then upon returning the response, you can close it. Closing the session is very important. You should always ensure that it’s closed within a finally block.
上述几段清楚的告诉咱们:
SqlSessionFactoryBuilder 和 SqlSession 不要缓存啥的,用完就扔,(本地变量)
SqlSessionFactory 最好少创建,最佳实战就是 单利模式
Mapper Instances
Mappers are interfaces that you create to bind to your mapped statements. Instances of the mapper interfaces are acquired from the SqlSession. As such, technically the broadest scope of any mapper instance is the same as the SqlSession from which they were requested. However, the best scope for mapper instances is method scope. That is, they should be requested within the method that they are used, and then be discarded. They do not need to be closed explicitly. While it’s not a problem to keep them around throughout a request, similar to the SqlSession, you might find that managing too many resources at this level will quickly get out of hand. Keep it simple, keep Mappers in the method scope
mapper 也是用完就扔(方法内变量生命周期即可)
至此,大体脉络就完事了。 后续再往细节瞅瞅