通常mybatis的操作步骤:
1.SqlSession sqlSession = DBUtil.getSessionFactory().openSession();
System.out.println(sqlSession.getClass().getName());//org.apache.ibatis.session.defaults.DefaultSqlSession
2.UserDao userDao = sqlSession.getMapper(UserDao.class);
3.User u = userDao.selectUserById(1);
那么UserDao userDao = sqlSession.getMapper(UserDao.class)是怎么得到的?以及如何和xml定义的查询方法关联起来的?
具体主要流程步骤:
首先加载配置文件mybatis-config.xml => UserMapper.xml
当加载到UserMapper.xml文件时,通过namespace="mybatis.demo.dao.UserDao" 创建UserDao对象,并且UserDao对象以key的方式放到MapperRegistry(mapper注册器)中(见名知意),
那么既然有key,value传入的是什么呢?通过查看源码,value是一个UserDao的代理对象,且实现了UserDao接口;
源码:
final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory)knownMappers.get(type);
if (mapperProxyFactory == null)
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
mapperProxyFactory:mapper代理工厂,生成的对象就是代理对象;
至于MapperRegistry对象里面的value为什么是代理对象且实现了UserDao接口?
UserDao userDao = sqlSession.getMapper(UserDao.class);
userDao该对象通过debug可得知为:org.apache.ibatis.binding.MapperProxy@5c5eefef类型,既然能用UserDao接收,所以应该实现了UserDao接口;
UserMapper.xml的作用:
通过该文件可以生成一个实现了UserDao接口的代理对象;并把UserDao对象当做key,代理对象当做value放到MapperRegistry中;