Mybatis V2版本基础, 基于V1提取框架
1.综述
- 配置文件升级为 xml 格式
- 使用面向过程方式优化代码
- 使用面向对象思想构造配置类封装对象
- 示例代码
- 主体函数如下
public class MybatisV2 {
private static Configuration configuration;
public static void main(String[] args) throws Exception {
MybatisV2 mybatisV2 = new MybatisV2();
mybatisV2.loadXml("v2-mybatis-config.xml");
User query = new User();
query.setName("测试");
query.setSex("测试");
List<User> users = mybatisV2.selectList("userMapper.queryUserList", query);
System.out.printf("users: %s", users);
}
}
2.核心流程
private <R, T> List<R> selectList(String statementId, T parameter) throws Exception {
List<R> result = new LinkedList<>();
MappedStatement mappedStatement = configuration.getMappedStatementsById(statementId);
Connection connection = getConnection();
String sql = getSql(mappedStatement);
Statement statement = createStatement(mappedStatement, sql, connection);
setParameters(parameter, statement, mappedStatement);
ResultSet resultSet = executeQuery(statement);
handlerResultSet(resultSet, mappedStatement, result);
return result;
}
3.主流程核心对象解析
- Configuration
- org.apache.ibatis.session.Configuration
- Mybatis 全局配置
- 构造时会默认注册一些别名
- XMLLanguageDriver、RawLanguageDriver 会在构造时注册
public class Configuration {
protected Environment environment;
protected Properties variables = new Properties();
protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>();
protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");
}
- MappedStatement
- org.apache.ibatis.mapping
- 存储 Sql Statement 相关信息
public final class MappedStatement {
private Configuration configuration;
private String id;
private StatementType statementType;
private ResultSetType resultSetType;
private ParameterMap parameterMap;
private SqlSource sqlSource;
}
- ParameterMapping
- org.apache.ibatis.mapping
- 参数映射对象
public class ParameterMapping {
private Configuration configuration;
private String property;
private Class<?> javaType = Object.class;
private JdbcType jdbcType;
private String jdbcTypeName;
}
- XmlConfigBuilder
- org.apache.ibatis.builder.xml
- xml 配置文件解析构造器
public class XMLConfigBuilder extends BaseBuilder {
protected final Configuration configuration;
protected final TypeAliasRegistry typeAliasRegistry;
private final XPathParser parser;
private void parseConfiguration(XNode root) {
try {
propertiesElement(root.evalNode("properties"));
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
loadCustomLogImpl(settings);
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
}
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
Properties defaults = context.getChildrenAsProperties();
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}