MyBatis学习笔记

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 也是用完就扔(方法内变量生命周期即可)


至此,大体脉络就完事了。 后续再往细节瞅瞅

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值