一.数据存贮对象
mybatis-config.xml --> Configuration
Configuration(核心对象):
1.封装了mybatis-config.xml
2.封装了mapper 文件 MapperStatement
对应的就是mapper.xml中 的一个标签
3.创建了Mybatis其他相关对象
xxxDaoMapper.xml --> MapperStatement
MapperStatement 封装sql语句 --> BoundSql
核心代码:
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//1.代理实现
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> users = userMapper.queryAllUsers();
//2.调用底层sqlSession方法
List<User> users = sqlSession.selectList("com.mybatis.mapper.UserMapper.queryAllUsers");
二.操作类型的对象
Excutor(接口)--处理功能的核心
作用:1.增删改update 查query 2.事务 3.缓存
实现类:
BatchExcutor:JDBC中的批处理操作
ReuseExcutor:复用 Statement
SimpleExcutor:常用的Excutor mybatis推荐 (默认)
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
StatementHandler(接口)
Mybatis封装了JDBC Statement,StatementHandler是真正Mybatis进行数据库访问操作的核心
实现:SimpleStatementHandler,PreparedStatementHandler,CallableStatement
ParameterHandler
目的:将Mybatis参数转换为JDBC使用的参数
@Param --> #{} --> ?
ResultSetHandler
目的:对JDBC中查询结果集 ResultSet 进行封装
TypeHandler
Java类型 --> 数据库类型
例如:String --> varchar
MappedStatement
封装的是XXXMapper.xml文件中对数据库操作的标签<select/insert/update/delete>
三. Mapper接口创建实现类
interface UserDao{
List<USer> queryAllUsers();
}
UserDao userDao = SqlSession.getMapper(UserMapper.class);
UserDao接口的实现类:
动态字节码技术,在JVM运行时创建 JVM运行结束后消失
1.如何创建 UserDao接口的实现类
代理(动态代理)
2.实现类如何实现方法
UserDapImpl implements UserDao{
queryAllUsers(){
sqlSession.select("namespace.id",参数)
|-Excutor
|-StatementHandler
|-ParameterHandler,ResultSetHandler
TypeHandler
}
}
MapperRegistry-->MapperProxyFactory-->MapperProxy
MapperProxyFactory:
Proxy.newProxyInstrace(ClassLoader,Class[]{xxxDaoMapper.class},mapperProxy)
四.解析xml文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
1.解析 mybatis-config.xml --> Configuration对象
xPathParser XNode --> Configuration
bulid() --> parseConfiguration(parser.evalNode("/configuration"))
xPathParser XNode --> MapperStatement --> namespace.id
并且把MapperStatement存放在了Configuration中
parseConfiguration --> mapperElement(root.evalNode("mappers"));
XMLMapperBuilder mapperParser = new XMLMapperBuilder(..);
mapperParser.parse();
configurationElement(parser.evalNode("/mapper"));
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
statementParser.parseStatementNode();
builderAssistant.addMappedStatement
MappedStatement.Builder statementBuilder = new MappedStatement.Builder
SqlSession sqlSession = sqlSessionFactory.openSession();
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
return new DefaultSqlSession(configuration, executor, autoCommit);