目录
- 一、工作流程
- 二、说明
- 2.1 构建SqlSessionFactory
- 2.2 SqlSession的获取
- 2.3 SqlSession执行语句
- 三、源码结构
- 3.1 接口层
- 3.2 核心处理
- 3.3 核心处理层
- 四、代码示例
- 4.1 通过inputStream构建SqlSessionFactory
- 4.2 通过configuration构建SqlSessionFactory
- 4.3 mybatis-config.xml示例
一、工作流程
- 1.系统启动会加载解析全局配置文件(如mybatis-config.xml),加载解析的信息存储在Configuration对象中
- 2.通过建造者模式完成配置文件的解析与SqlSessionFactory对象的创建
- 3.通过SqlSessionFactory对象(是单例的)获取SqlSession对象
- 4.通过SqlSession中的api来操作数据库,有两种方式,可以通过名称空间和标签的id拼接成一个唯一的statement,还可以通过sqlSesion.getMapper拿到代理对象,通过代理对象去操作
- 5.关掉sqlSession,清空缓存
二、说明
2.1 构建SqlSessionFactory
- 1.每个基于mybatis的应用都是以一个SqlSessionFactory的实例为核心的。SqlSessionFactory的实例可以通过SqlSessionFactoryBuilder获得
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
-
2.SqlSessionFactoryBuilder可以从XML配置文件或一个预先配置的Configuration实例来构建出SqlSessionFactory实例
-
3.从XML文件中构建SqlSessionFactory实例,建议使用类路径下的资源文件进行配置。
-
4.也可以使用任意的输入流(InputStream)实例,例如文件路径字符串(file://)或者数据库URL构造输入流(xml格式与configuration格式)
-
5.使用mybatis中Resource工具类,能简化从类路径或其它位置加载资源文件
-
6.xml配置文件包括:获取数据库连接实例的数据源(DataSource)、决定事务作用域、控制方法的事务管理器(TransactionManager)。environment元素包含事务管理和连接池的配置。mappers元素包含映射器(mapper),映射器包含了sql代码和映射定义信息
2.2 SqlSession的获取
- 1.通过SqlSession sqlSession = sqlSessionFactory.openSession();得到sqlSession
2.3 SqlSession执行语句
- 1.通过sqlSession.api(“名称空间.标签名”, 1);
SqlSession sqlSession = sqlSessionFactory.openSession();
Car car = (Car)sqlSession.selectOne("org.apache.ibatis.learning.mapper.CarMapper.selectCar", 1);
System.out.println(car.getName());
- 2.通过sqlSession.getMapper(CarMapper.class);获取代理对象,通过代理对象调用方法
package org.apache.ibatis.learning.mapper;
public interface CarMapper {
Car selectCar(int id);
}
SqlSession sqlSession = sqlSessionFactory.openSession();
CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
car = carMapper.selectCar(1);
System.out.println(car.getName());
三、源码结构
3.1 接口层
- 1.SqlSession
3.2 核心处理
- 1. 配置解析
- 2. 参数映射
- 3. sql解析
- 4. sql执行
- 5. 结果集映射
- 6. 插件
3.3 核心处理层
- 1. 数据源模块
- 2. 事务管理模块
- 3. 缓存模块
- 4. Binding模块
- 5.反射模块
- 6.类型转换模块
- 7.日志模块
- 8.资源加载
- 9.解析器模块
四、代码示例
4.1 通过inputStream构建SqlSessionFactory
package org.apache.ibatis.learning.sqlsessionfactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.learning.mapper.Car;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
/**
* @Author wangyouhui
* @Description 通过inputStream构建SqlSessionFactory
**/
public class SqlSessionFactoryByInputStream {
public static void main(String[] args) throws Exception {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Car car = (Car)sqlSession.selectOne("org.apache.ibatis.learning.mapper.CarMapper.selectCar", 1);
System.out.println(car.getName());
}
}
4.2 通过configuration构建SqlSessionFactory
package org.apache.ibatis.learning.sqlsessionfactory;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.learning.mapper.Car;
import org.apache.ibatis.learning.mapper.CarMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
/**
* @Author wangyouhui
* @Description 通过configuration构建SqlSessionFactory
**/
public class SqlSessionFactoryByConfiguration {
public static void main(String[] args) throws Exception {
DataSource dataSource = createDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
// configuration.setLazyLoadingEnabled(true);
// configuration.setUseActualParamName(false);
// configuration.getTypeAliasRegistry().registerAlias(Car.class);
configuration.addMapper(CarMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession sqlSession = sqlSessionFactory.openSession();
Car car = (Car)sqlSession.selectOne("org.apache.ibatis.learning.mapper.CarMapper.selectCar", 1);
System.out.println(car.getName());
}
private static DataSource createDataSource() throws IOException {
String resource = "jdbc.properties";
Properties props = Resources.getResourceAsProperties(resource);
PooledDataSource ds = new PooledDataSource();
ds.setDriver(props.getProperty("driver"));
ds.setUrl(props.getProperty("url"));
ds.setUsername(props.getProperty("username"));
ds.setPassword(props.getProperty("password"));
ds.setDefaultAutoCommit(false);
return ds;
}
}
4.3 mybatis-config.xml示例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></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>
<mappers>
<mapper resource="org/apache/ibatis/learning/mapper/CarMapper.xml"></mapper>
</mappers>
</configuration>