您的位置:首页 > 运维架构 > 网站架构

Mybatis架构设计及源码分析-MappedStatement

2018-05-26 23:16 525 查看
一个MappedStatement对应mapper配置的xml文件中的一条sql语句节点,比如:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>

<select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id}</select>这条语句就是用一个MappedStatement对象来描述,在Configuration对象中会维护namespace+id与MappedStatement的映射关系,配置文件初始化过程是会解析并设置到下面变量中。

protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");

下面我们简要分析下MappedStatement的主要内容:

private String resource;
private Configuration configuration;
private String id;
private Integer fetchSize;
private Integer timeout;
private StatementType statementType;
private ResultSetType resultSetType;
private SqlSource sqlSource;
private Cache cache;
private ParameterMap parameterMap;
private List<ResultMap> resultMaps;
private boolean flushCacheRequired;
private boolean useCache;
private boolean resultOrdered;
private SqlCommandType sqlCommandType;
private KeyGenerator keyGenerator;
private String[] keyProperties;
private String[] keyColumns;
private boolean hasNestedResultMaps;
private String databaseId;
private Log statementLog;
private LanguageDriver lang;
private String[] resultSets;

上面很多属性都和<select id="selectBlog" resultType="Blog"> select * from Blog where id = #{id}</select>这种语句有类似对应关系,暂时不一一介绍,重要的几个参数是parameterMaps和resultMaps以及sqlSource。其中parameterMaps是封装了sql语句的参数信息,resultMaps封装的是返回值信息。SqlSource封装的是sql语句信息,SqlSource内部只有一个接口根据传入的参数计算好BoundSql信息。BoundSql又是什么呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: