0
点赞
收藏
分享

微信扫一扫

Mybatis的底层源码解析

佳简诚锄 2022-01-09 阅读 80
public class UserMapper {
    public User selectOne() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User  user = sqlSession.selectOne("com.wxz.domain.User.selectOne");
        return  user;
    }

    public static void main(String[] args) throws IOException {
        User user  = new UserMapper().selectOne();
        System.out.println(user);
    }
}
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/1009-test"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>
<?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="com.wxz.domain.User">
  <select id="selectOne" resultType="com.wxz.domain.User" >
    select id as id,name as name , address as address ,age as age from user where id = 3
  </select>
</mapper>
第一步,解析xml文件
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);

 第二步:将xml信息转化为Configuration类对象
parser.parse()

 

 

第三步:将Configuration类对象作为参数传入,创建一个SqlSessionFactory对象

 public SqlSessionFactory build(Configuration config) {
        return new DefaultSqlSessionFactory(config);
    }

 第四步:实际上 new DefaultSqlSessionFactory(config)

 第五步:根据sqlSessionFactory获取sqlSession

SqlSession sqlSession = sqlSessionFactory.openSession();

 第六步:具体的数据库操作

User  user = sqlSession.selectOne("com.wxz.domain.User.selectOne")
 public <T> T selectOne(String statement, Object parameter) {
        List<T> list = this.selectList(statement, parameter);
        if (list.size() == 1) {
            return list.get(0);
        } else if (list.size() > 1) {
            throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
        } else {
            return null;
        }
    }
statement就是namespace+id  在前面就将namespace+id 作为key放入map中,value是mappedStatent根据key查询mappedStatent

第七步:根据statement找到对应的MappedStatement
MappedStatement ms = this.configuration.getMappedStatement(statement);

 第八步:调用executor去查询,从MappedStatement获取到sql语句

var6 = this.executor.query(ms, this.wrapCollection(parameter), rowBounds, handler); 
BoundSql boundSql = ms.getBoundSql(parameterObject);
第九步:查询缓存,从缓存中获取,若缓存没有,则查询数据库,再放入缓存中

 

 

 

 第十步:根据结果映射关系将查询的结果转化为具体的指定的类 ,此处将查询到的结果转化为User对象

 

 

 

 

举报

相关推荐

0 条评论