1、主要的类

1.1 SqlSessionFactoryBuilder

用于创建SqlSessionFactory,要通过配置文件也可以是代码。

主要的方法

SqlSessionFactory build(InputStream inputStream)
SqlSessionFactory build(InputStream inputStream, String environment)
SqlSessionFactory build(InputStream inputStream, Properties properties)
SqlSessionFactory build(InputStream inputStream, String env, Properties props)
SqlSessionFactory build(Configuration config)

创建的示例

String resource = "org/mybatis/builder/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(inputStream);
DataSource dataSource = BaseDataTest.createBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);Configuration configuration = new Configuration(environment);
configuration.setLazyLoadingEnabled(true);
configuration.setEnhancementEnabled(true);
configuration.getTypeAliasRegistry().registerAlias(Blog.class);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundBlogMapper.class);
configuration.addMapper(BoundAuthorMapper.class);SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(configuration);

1.2 SqlSessionFactory

用于创建SqlSession

主要方法

SqlSession openSession()
SqlSession openSession(boolean autoCommit)
SqlSession openSession(Connection connection)
SqlSession openSession(TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level)
SqlSession openSession(ExecutorType execType)
SqlSession openSession(ExecutorType execType, boolean autoCommit)
SqlSession openSession(ExecutorType execType, Connection connection)
Configuration getConfiguration();

1.3 SqlSession

持久层操作会话

常用方法

<T> T selectOne(String statement)
<E> List<E> selectList(String statement)
<K,V> Map<K,V> selectMap(String statement, String mapKey)
int insert(String statement)
int update(String statement)
int delete(String statement)

事务操作方法

void commit()
void commit(boolean force)
void rollback()
void rollback(boolean force)

2、配置文件

<properties resource="org/mybatis/example/config.properties"><property name="username" value="dev_user"/><property name="password" value="F2Fa3!33TYyg"/>
</properties>
<dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/>
</dataSource>
<settings><setting name="cacheEnabled" value="true"/><setting name="lazyLoadingEnabled" value="true"/><setting name="multipleResultSetsEnabled" value="true"/><setting name="useColumnLabel" value="true"/><setting name="useGeneratedKeys" value="false"/><setting name="autoMappingBehavior" value="PARTIAL"/><setting name="autoMappingUnknownColumnBehavior" value="WARNING"/><setting name="defaultExecutorType" value="SIMPLE"/><setting name="defaultStatementTimeout" value="25"/><setting name="defaultFetchSize" value="100"/><setting name="safeRowBoundsEnabled" value="false"/><setting name="mapUnderscoreToCamelCase" value="false"/><setting name="localCacheScope" value="SESSION"/><setting name="jdbcTypeForNull" value="OTHER"/><setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/></settings>
<typeHandlers><typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
</typeHandlers>
<environments default="development"><environment id="development"><transactionManager type="JDBC"><property name="..." value="..."/></transactionManager><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments>
<databaseIdProvider type="DB_VENDOR"><property name="SQL Server" value="sqlserver"/><property name="DB2" value="db2"/>        <property name="Oracle" value="oracle" />
</databaseIdProvider>
<mappers><mapper class="org.mybatis.builder.AuthorMapper"/><mapper class="org.mybatis.builder.BlogMapper"/><mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
<mappers><package name="org.mybatis.builder"/>
</mappers>

注:

配置文件元素与JAVA操作方法是可以相互对应的; 配置文件中元素是可以独立,如属性文件和注解。

详细参考官网地址:http://www.mybatis.org/mybatis-3/zh/configuration.html#environments

转载于:https://blog.51cto.com/881206524/1922470

mybatis 基础理解相关推荐

  1. MyBatis基础篇

    MyBatis基础篇 简介 注意:本篇文章主讲语法,jar包导入大家可以在网上搜索(有问题一定要学会利用搜索引擎解决!!!),全程代码会写到文章中. 1. MyBatis是一个半自动ORM框架(作为解 ...

  2. MyBatis基础学习知识点2

    本文衔接MyBatis基础学习知识点1,继续对以下两个问题进行探讨 1.dao配置文件主要是用来干什么的?如何进行配置? 2.使用测试方法测试程序运行是如何实现的?每条语句起什么作用? 目录 dao配 ...

  3. java mybatis基础

    java mybatis基础 1.1 什么是mybatis? mybatis是一个优秀的持久层框架. 避免几乎所有的JDBC代码和手动设置参数以及获取结果集的过程. 可以使用简单的xml或者注解来配置 ...

  4. # vue.js 之 对vue.js基础理解

    vue.js 之 对vue.js基础理解 Vue构造器 1 . Vue.js是一个构造函数,编程中称之为构造器 2 . 每一个new Vue() 都是一个Vue构造函数的实例,这个过程叫做实例化 3 ...

  5. MyBatis基础入门《九》ResultMap自动匹配

    MyBatis基础入门<九>ResultMap自动匹配 描述: Mybatis执行select查询后,使用ResultMap接收查询的数据结果. 实体类:TblClient.java 接口 ...

  6. Mybatis基础:增删改查、模糊查询、多条件查询

    Mybatis基础:增删改查.模糊查询.多条件查询http://www.bieryun.com/3132.html 1.新建测试数据库,根据实体类属性创建 2.实体类 [java] view plai ...

  7. javaweb实训第六天下午——Mybatis基础

    Mybatis基础 1.课程介绍 2.为什么需要Mybatis 3.初识Mybatis 3.1.Mybatis是什么 3.1.1.什么是框架 3.1.2.什么叫数据库持久化 3.1.3.什么是ORM ...

  8. PCA与2DPCA及2D-2DPCA零基础理解(下)

    有小伙伴催了好久的这篇文章,我也是一拖再拖,PCA与2DPCA及2D-2DPCA零基础理解(上)已经发布了两年了,这几天由于疫情的原因,我专门找出时间来写这篇博文.以前感觉:怎样确定哪张人脸是属于哪个 ...

  9. Mybatis基础学习之万能的Map和模糊查询

    前言: 小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师. 这个Mybatis ...

最新文章

  1. Redis系列(七):缓存只是读写回种这么简单吗?如果是,那么请你一定看看这篇文章!...
  2. linux和windows测评,Windows Server 2016 与 Linux同台PK
  3. java swing 例子(一些)
  4. 【c++】26.浅谈“事件驱动”、select、poll、epoll
  5. samba   服务
  6. 七、深入JavaScript函数,对象和作用域(三)
  7. hdfs 数据迁移_对象存储BOS发布全新工具,加速自建HDFS到云端的访问速度
  8. c# 设为首页和加入收藏代码
  9. 什么是 JxBrowser
  10. comms-logging 输出级别设置
  11. 在服务器创建并进入虚拟环境
  12. 话题中的Publisher和Subscriber
  13. 计算机excel必备知识,2017职称计算机考试EXCEL知识点:创建图表
  14. 分享一个网易云会员包项目刷下载量的脚本
  15. 2021-09-10
  16. 《缠中说禅108课》22:将 8 亿的大米装到 5 个庄家的肚里
  17. linux write的行为
  18. 计算机桌面太大了,电脑显示器显示太大怎么办
  19. QT制作软件---窗口跟随鼠标进行移动
  20. IT视频教程百度云盘链接分享

热门文章

  1. Winform跨线程调用简洁办法
  2. 十二省联考2019游记
  3. GO语言-基础语法:循环
  4. 华科高级软件测试技术1704班-02组 如何计算团队成员贡献分
  5. 使用 HTML5 canvas 绘制精美的图形
  6. SCSF 系列:Smart Client Software Factory 中的 MVP 模式概述
  7. CodeSmith基础(二)
  8. Qt-调用dll动态链接库
  9. Python—实训day11—Pyecharts绘图
  10. codis配置_分布式缓存Codis集群安装手册