1、What is MyBatis
keyword 摘自官网:
1、persistence framework
2、costum SQL
3、stored procedures
4、advanced mapping
----
5、eliminate almost JDBC code
6、manual setting param and retrival of results
----
7、use xml or annotation and map primitives
8、map interface and JOPO to DB
2、官网学习
2.1、装载过程
1、引入package
2、装配congfiguration
3、SqlSessionFactoryBuilder.build(configuration),装载SqlSessionFactory实例
4、sqlSessionFactory实例获取sqlSession
4.1、可以sqlSession.SelectOne(namespace,param)操作db
4.2、sqlSession 获取SqlSession.getMapper(),获取mapper 并操作实例
说明:mapper sql的调用形式:xml ,annotation
2.2 Scope
Bean 名称 | 作用域 | 作用 |
---|---|---|
SQLSessionFactoryBuilder | method 最佳 | 只为创建sqlsessionFactory存在 |
SQLSessionFactory | Application 最佳 | 单例模式或者静态单例模式 |
SqlSession | request、method最佳 | finally中关闭;确保每个请求中只有一个sqlSession;SqlSessionFactory.openSQLSession()获取session 对象 |
custom mapper | request、method最佳 | 和SqlSession的作用域相同;获取:session.getMapper(namespace) |
2.3 mybatis.xml config param
各个属性信息的说明见官网
http://www.mybatis.org/mybatis-3/zh/configuration.html#
configuration
1、properties
2、settings
3、typeAliases
4、typeHandlers :自定义TypeHandle extends more TypeHanders
5、plugins:Executor ,ParameterHandler ,ResultSetHandler ,StatementHandler 谨慎修改
6、environments
7、mappers
2.4 mapper.xml configuration
元素说明:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
1、select
2、insert,update,delete
3、参数
4、resultMap
4.1、嵌套查询
4.2、嵌套结果
4.3、关联多结果集
4.4、
5、集合 collection
6、缓存
2.5、动态sql标签
1、where
2、choose
3、if
4、set
5、trim
6、forEach
7、script