Mybatis介绍及原理简介
1. 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.
译:
Mybatis是一个一流的持久框架,支持自定义SQL,存储过程和高级Mappings。 Mybatis减少了很多JDBC代码和手工设置的参数和提取结果步骤。
Mybatis可以使用一个简单的XML配置文件或者Annotation来配置和匹配实体,把接口和对应数据库记录的JAVA POJOs匹配起来。
Wiki:
Unlike ORM frameworks, MyBatis does not map Java objects to database tables but Java methods to SQL statements.
MyBatis lets you use all your database functionality like stored procedures, views, queries of any complexity and vendor proprietary features. It is often a good choice for legacy or de-normalized databases or to obtain full control of SQL execution.
It simplifies coding compared to JDBC. SQL statements are executed with a single line.
MyBatis provides a mapping engine that maps SQL results to object trees in a declarative way.
SQL statements can be built dynamically by using a built-in language with XML-like syntax or with Apache Velocity using the Velocity integration plugin.
MyBatis integrates with Spring Framework and Google Guice. This feature allows one to build business code free of dependencies.
MyBatis supports declarative data caching. A statement can be marked as cacheable so any data retrieved from the database will be stored in a cache and future executions of that statement will retrieve the cached data instead hitting the database. MyBatis provides a default cache implementation based on a Java HashMap and default connectors for integrating with: OSCache, Ehcache, Hazelcast and Memcached. It provides an API to plug other cache implementations.
译:
不像其他持久框架一样,Mybatis并不把JAVA POJO和数据库表对应起来而是把JAVA代码中的办法与SQL语句匹配起来。
Mybatis让你使用数据库所用的功能,比如存储过程、视图、包含不同数据库复杂的查询语句。它经常是一个比较好的选择对于遗留或者非规范的数据库,并且拥有应用于全部的SQL执行能力。
相比JDBC而言,它简化了变成。SQL语句可以只使用一行来执行。
Mybatis提供一个Mapping的引擎,这个引擎可以以一种声明的方式将SQL语句结果匹配JAVA对象上。
SQL语句可以动态的创建,者需要使用类似XML语法的内建语言或者apache 整合插件(没翻译明白).
Mybatis可以与Spring框架和Google Guice一起使用。这可以使你不用考虑依赖的问题创建出业务代码。
Mybatis支持声明的data缓存。一个语句可以打上标记为需要缓存,这样这个语句的结果就会记录到缓存中,这样将来的执行就不用再去数据库中查询。Mybatis支持一个默认的基于JAVA HashMap和默认整合系统缓存,Ehcache,Hazelcast和Memcached的缓存实现。它提供一个API,可以把其他缓存实现的方法以插件形式插入进来。
MyBatis is a fork from iBATIS, and according to Wikipedia most of iBATIS’ developers moved over to MyBatis too. The iBATIS project is currently marked as Inactive, therefore you should go with MyBatis for new projects and only use iBATIS if you’re maintaining an existing project which already uses iBATIS.(引入依据Mybatis的区别)
2.原理理解
主要完成两件事:
1. 根据JDBC规范建立数据库的连接
2. 通过反射打通对象和数据库之间的关系
其实它是一种典型的交互性框架,先准备好必须的交互条件,然后构建一个交互的环境。交互环境还划分为会话,每次的会话都有一个环境。当这些都准备好后,剩下的就是交互数据。其实涉及到网络通信,一般都是类似的处理方式。
主要的类介绍:
SqlMapClient接口定义了客户端的操作行为,包括select,insert,update和delete.
SqlMapSession主要定义了语句执行环境。
SqlMapClientImpl继承SqlMapClient和SqlMapSession的实现类,然后就可以操作了。