基本上mybatis的官网已经提供了很完善的入门文档,参考这个基本就可以明白了

https://mybatis.org/mybatis-3/zh/getting-started.htmlz

主要的入门步骤有以下几步

1. 配置mybatis-config.xml文件,也是核心的文件,包含了类型转换器,别名注册器,环境等

2. 配置mapper文件,也就是映射数据表的文件,每张表都有一个映射文件

3. 根据mybatis-config.xml创建SqlSessionFactory

4. 根据SqlSessionFactory获取SqlSession

5. 操作


package com.zxc.study.test.utils;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class FactoryUtils {public static SqlSessionFactory sqlSessionFactory() {return sqlSessionFactory(null);}public static SqlSessionFactory sqlSessionFactory(Properties properties) {String resource = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);} catch (IOException e) {e.printStackTrace();}return new SqlSessionFactoryBuilder().build(inputStream, properties);}
}//调用测试package com.zxc.study.test.sqlsession;import com.zxc.study.test.bean.User;
import com.zxc.study.test.mapper.UserMapper;
import com.zxc.study.test.utils.FactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;import java.util.Properties;public class SqlSessionTest {public static void main(String[] args) {Properties properties = new Properties();SqlSessionFactory sqlSessionFactory = FactoryUtils.sqlSessionFactory(properties);SqlSession sqlSession = sqlSessionFactory.openSession();Object selectOne = sqlSession.selectOne("com.zxc.study.test.mapper.UserMapper.selectUser", 1);System.out.println(selectOne);//新的使用,通过接口获取UserMapper userMapper = sqlSession.getMapper(UserMapper.class);User user = userMapper.selectUser(1);System.out.println("通过接口获取" + user);}
}

关键的核心接口

SqlSessionFactory : 用于生成SqlSession的工厂

/***    Copyright 2009-2016 the original author or authors.**    Licensed under the Apache License, Version 2.0 (the "License");*    you may not use this file except in compliance with the License.*    You may obtain a copy of the License at**       http://www.apache.org/licenses/LICENSE-2.0**    Unless required by applicable law or agreed to in writing, software*    distributed under the License is distributed on an "AS IS" BASIS,*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*    See the License for the specific language governing permissions and*    limitations under the License.*/
package org.apache.ibatis.session;import java.sql.Connection;/*** Creates an {@link SqlSession} out of a connection or a DataSource* * @author Clinton Begin*/
public interface SqlSessionFactory {SqlSession openSession();SqlSession openSession(boolean autoCommit);SqlSession openSession(Connection connection);SqlSession openSession(TransactionIsolationLevel level);SqlSession openSession(ExecutorType execType);SqlSession openSession(ExecutorType execType, boolean autoCommit);SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);SqlSession openSession(ExecutorType execType, Connection connection);Configuration getConfiguration();}

SqlSession : 用于操作数据库的session接口

/***    Copyright 2009-2016 the original author or authors.**    Licensed under the Apache License, Version 2.0 (the "License");*    you may not use this file except in compliance with the License.*    You may obtain a copy of the License at**       http://www.apache.org/licenses/LICENSE-2.0**    Unless required by applicable law or agreed to in writing, software*    distributed under the License is distributed on an "AS IS" BASIS,*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*    See the License for the specific language governing permissions and*    limitations under the License.*/
package org.apache.ibatis.session;import java.io.Closeable;
import java.sql.Connection;
import java.util.List;
import java.util.Map;import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;/*** The primary Java interface for working with MyBatis.* Through this interface you can execute commands, get mappers and manage transactions.** @author Clinton Begin*/
public interface SqlSession extends Closeable {/*** Retrieve a single row mapped from the statement key* @param <T> the returned object type* @param statement* @return Mapped object*/<T> T selectOne(String statement);/*** Retrieve a single row mapped from the statement key and parameter.* @param <T> the returned object type* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @return Mapped object*/<T> T selectOne(String statement, Object parameter);/*** Retrieve a list of mapped objects from the statement key and parameter.* @param <E> the returned list element type* @param statement Unique identifier matching the statement to use.* @return List of mapped object*/<E> List<E> selectList(String statement);/*** Retrieve a list of mapped objects from the statement key and parameter.* @param <E> the returned list element type* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @return List of mapped object*/<E> List<E> selectList(String statement, Object parameter);/*** Retrieve a list of mapped objects from the statement key and parameter,* within the specified row bounds.* @param <E> the returned list element type* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @param rowBounds  Bounds to limit object retrieval* @return List of mapped object*/<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);/*** The selectMap is a special case in that it is designed to convert a list* of results into a Map based on one of the properties in the resulting* objects.* Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")* @param <K> the returned Map keys type* @param <V> the returned Map values type* @param statement Unique identifier matching the statement to use.* @param mapKey The property to use as key for each value in the list.* @return Map containing key pair data.*/<K, V> Map<K, V> selectMap(String statement, String mapKey);/*** The selectMap is a special case in that it is designed to convert a list* of results into a Map based on one of the properties in the resulting* objects.* @param <K> the returned Map keys type* @param <V> the returned Map values type* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @param mapKey The property to use as key for each value in the list.* @return Map containing key pair data.*/<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);/*** The selectMap is a special case in that it is designed to convert a list* of results into a Map based on one of the properties in the resulting* objects.* @param <K> the returned Map keys type* @param <V> the returned Map values type* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @param mapKey The property to use as key for each value in the list.* @param rowBounds  Bounds to limit object retrieval* @return Map containing key pair data.*/<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);/*** A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.* @param <T> the returned cursor element type.* @param statement Unique identifier matching the statement to use.* @return Cursor of mapped objects*/<T> Cursor<T> selectCursor(String statement);/*** A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.* @param <T> the returned cursor element type.* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @return Cursor of mapped objects*/<T> Cursor<T> selectCursor(String statement, Object parameter);/*** A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.* @param <T> the returned cursor element type.* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @param rowBounds  Bounds to limit object retrieval* @return Cursor of mapped objects*/<T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);/*** Retrieve a single row mapped from the statement key and parameter* using a {@code ResultHandler}.* @param statement Unique identifier matching the statement to use.* @param parameter A parameter object to pass to the statement.* @param handler ResultHandler that will handle each retrieved row*/void select(String statement, Object parameter, ResultHandler handler);/*** Retrieve a single row mapped from the statement* using a {@code ResultHandler}.* @param statement Unique identifier matching the statement to use.* @param handler ResultHandler that will handle each retrieved row*/void select(String statement, ResultHandler handler);/*** Retrieve a single row mapped from the statement key and parameter* using a {@code ResultHandler} and {@code RowBounds}* @param statement Unique identifier matching the statement to use.* @param rowBounds RowBound instance to limit the query results* @param handler ResultHandler that will handle each retrieved row*/void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);/*** Execute an insert statement.* @param statement Unique identifier matching the statement to execute.* @return int The number of rows affected by the insert.*/int insert(String statement);/*** Execute an insert statement with the given parameter object. Any generated* autoincrement values or selectKey entries will modify the given parameter* object properties. Only the number of rows affected will be returned.* @param statement Unique identifier matching the statement to execute.* @param parameter A parameter object to pass to the statement.* @return int The number of rows affected by the insert.*/int insert(String statement, Object parameter);/*** Execute an update statement. The number of rows affected will be returned.* @param statement Unique identifier matching the statement to execute.* @return int The number of rows affected by the update.*/int update(String statement);/*** Execute an update statement. The number of rows affected will be returned.* @param statement Unique identifier matching the statement to execute.* @param parameter A parameter object to pass to the statement.* @return int The number of rows affected by the update.*/int update(String statement, Object parameter);/*** Execute a delete statement. The number of rows affected will be returned.* @param statement Unique identifier matching the statement to execute.* @return int The number of rows affected by the delete.*/int delete(String statement);/*** Execute a delete statement. The number of rows affected will be returned.* @param statement Unique identifier matching the statement to execute.* @param parameter A parameter object to pass to the statement.* @return int The number of rows affected by the delete.*/int delete(String statement, Object parameter);/*** Flushes batch statements and commits database connection.* Note that database connection will not be committed if no updates/deletes/inserts were called.* To force the commit call {@link SqlSession#commit(boolean)}*/void commit();/*** Flushes batch statements and commits database connection.* @param force forces connection commit*/void commit(boolean force);/*** Discards pending batch statements and rolls database connection back.* Note that database connection will not be rolled back if no updates/deletes/inserts were called.* To force the rollback call {@link SqlSession#rollback(boolean)}*/void rollback();/*** Discards pending batch statements and rolls database connection back.* Note that database connection will not be rolled back if no updates/deletes/inserts were called.* @param force forces connection rollback*/void rollback(boolean force);/*** Flushes batch statements.* @return BatchResult list of updated records* @since 3.0.6*/List<BatchResult> flushStatements();/*** Closes the session*/@Overridevoid close();/*** Clears local session cache*/void clearCache();/*** Retrieves current configuration* @return Configuration*/Configuration getConfiguration();/*** Retrieves a mapper.* @param <T> the mapper type* @param type Mapper interface class* @return a mapper bound to this SqlSession*/<T> T getMapper(Class<T> type);/*** Retrieves inner database connection* @return Connection*/Connection getConnection();
}

以下两个都是接口,而且应该是采用了抽象工厂的设计模式,有兴趣的可以了解一下

上面的都是mybatis的源码,也只是贴出来一下

mybatis为了方便我们创建工厂对象,还提供了一个

SqlSessionFactoryBuilder对象用于给我们方便创建对象,以及提供了Resources对象方便加载流数据

这一章主要就是说了一下核心的接口,当然了还有很多其他的,只不过照顺序看下来目前就这几个比较重要,下一章准备说下SqlSessionFactoryBuilder的parse方法,也是比较核心的方法,会分为几帐来说,一点一点记录

第二篇:mybatis核心接口相关推荐

  1. MyBatis核心接口和类

    三大对象: 1.SqlSessionFactoryBuilder:负责构建SqlSessionFactory,并且提供了多个build()方法的重载 2.SqlSessionFactory:创建Sql ...

  2. Core Animation 文档翻译 (第二篇)—核心动画基础要素

    前言 核心动画为我们APP内Views动画和其他可视化元素动画提供了综合性的实现体系.核心动画不是我们APP内Views的替代品,相反,它是一种结合Views来提供更好性能和支持Content动画的技 ...

  3. Mybatis源码阅读(四):核心接口4.1——StatementHandler

    *************************************优雅的分割线 ********************************** 分享一波:程序员赚外快-必看的巅峰干货 如 ...

  4. Mybatis(第二篇:联表查询)

    Mybatis(第二篇:联表查询) 目录 Mybatis(第二篇:联表查询) 一.前期 项目的搭建 1.数据库 2.IDEA项目架构搭建 2.1 pom.xml 2.2 domain包 2.2.1 D ...

  5. Mybatis源码阅读(四):核心接口4.2——Executor(上)

    *************************************优雅的分割线 ********************************** 分享一波:程序员赚外快-必看的巅峰干货 如 ...

  6. 黑客攻防技术宝典-Web实战篇——第二章、核心防御机制(一)

    第二章.核心防御机制(一) 在 上一章 中我们讲到web应用程序的核心问题在于用户可以提交任何输入,那么相对应的防御机制也大都是针对用户的请求进行处理防御 下面是防御机制的核心因素: 处理用户访问应用 ...

  7. Raspberry-Pi-PICO系列--第八篇 高级篇使用SWD接口下载和调试(第二章)

    一.目的 在上一篇<Raspberry-Pi-PICO系列--第八篇 高级篇使用SWD接口下载和调试(第一章)> 我们已经介绍了如何准备openocd.如何编译picoprobe.如何进行 ...

  8. 第二篇:python基础之核心风格

    第三篇:python基础之数据类型与变量 阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整 ...

  9. 第二篇|腾讯开源项目盘点:ncnn、xLua、libco等

    开源展示了人类共同协作,成果分享的魅力,每一次技术发展都是站在巨人的肩膀上,技术诸多创新和发展往往就是基于开源发展起来的,没有任何一家网络公司可以不使用开源技术,仅靠自身技术而发展起来. 腾讯开源了个 ...

最新文章

  1. mysql 单选字段_mysql字段类型
  2. LightOJ 1095 Arrange the Numbers(容斥原理)
  3. 说说CSRF的***
  4. 任艳频老师关于信息与大数据课题组的讨论材料的意见
  5. Make sure the device specification refers to a valid device
  6. Pidgin Portable 使用点滴
  7. Android LaunchMode
  8. NSGA2算法及其代码
  9. python列表_Python列表列表的所有组合
  10. iOS 开发之解析url中的参数
  11. apache和php结合、apache的默认虚拟主机
  12. windows7系统适合哪个python_windows7如何下载python系统
  13. dfema规则_DFMEA失效分析简介
  14. colorui开发文档_小程序原生高颜值组件库--ColorUI
  15. windowns server 2008 R2激活工具(含win7、vista)
  16. 香港云服务器选阿里云好还是腾讯云好?
  17. Ubuntu利用Xorg.conf配置双屏扩展显示
  18. 用CSS设计艺术字集锦
  19. STM32 VCP PC端安装驱动失败的问题解决
  20. 浏览器工作流程_浏览器如何工作

热门文章

  1. 火车卧铺, 乘客自己叠床单,过完年又长了见识!!
  2. Python 大作业 网易云歌单数据分析及可视化(参考多位博主文章)
  3. Java基础深化和提高 ---- 网络编程
  4. Python爬取某音乐网站
  5. 从观麦前端框架的角度看css
  6. cef使用缓存_c# - CefSharp清除缓存路径崩溃应用程序 - 堆栈内存溢出
  7. 【总结】1198- 总结 2021 年 JavaScript 新一代构建工具对比
  8. canal部署、原理和使用介绍
  9. java 注入依赖_依赖注入(Dependency Injection)
  10. Java设计模式--代理模式