public<T> T getMapper(Class<T> type){return configuration.getMapper(type,this);}
configuration.getMapper
public<T> T getMapper(Class<T> type, SqlSession sqlSession){return mapperRegistry.getMapper(type, sqlSession);}
mapperRegistry.getMapper
public<T> T getMapper(Class<T> type, SqlSession sqlSession){//根据类型从缓存中获取MapperProxyFactoryfinal MapperProxyFactory<T> mapperProxyFactory =(MapperProxyFactory<T>) knownMappers.get(type);if(mapperProxyFactory == null){thrownewBindingException("Type "+ type +" is not known to the MapperRegistry.");}try{//通过MapperProxyFactory创建代理类return mapperProxyFactory.newInstance(sqlSession);}catch(Exception e){thrownewBindingException("Error getting mapper instance. Cause: "+ e, e);}}
创建mapper的代理类
public T newInstance(SqlSession sqlSession){//真实处理业务的代理对象final MapperProxy<T> mapperProxy =newMapperProxy<>(sqlSession, mapperInterface, methodCache);//创建代理returnnewInstance(mapperProxy);}protected T newInstance(MapperProxy<T> mapperProxy){//动态代理return(T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),newClass[]{ mapperInterface }, mapperProxy);}
public Object execute(SqlSession sqlSession, Object[] args){
Object result;//判断操作类型switch(command.getType()){case INSERT:{
Object param = method.convertArgsToSqlCommandParam(args);
result =rowCountResult(sqlSession.insert(command.getName(), param));break;}case UPDATE:{
Object param = method.convertArgsToSqlCommandParam(args);
result =rowCountResult(sqlSession.update(command.getName(), param));break;}case DELETE:{
Object param = method.convertArgsToSqlCommandParam(args);
result =rowCountResult(sqlSession.delete(command.getName(), param));break;}case SELECT:if(method.returnsVoid()&& method.hasResultHandler()){executeWithResultHandler(sqlSession, args);
result = null;}elseif(method.returnsMany()){
result =executeForMany(sqlSession, args);}elseif(method.returnsMap()){
result =executeForMap(sqlSession, args);}elseif(method.returnsCursor()){
result =executeForCursor(sqlSession, args);}else{//查询结果集为一个的//解析参数
Object param = method.convertArgsToSqlCommandParam(args);//查询单条记录,得到结果
result = sqlSession.selectOne(command.getName(), param);if(method.returnsOptional()&&(result == null ||!method.getReturnType().equals(result.getClass()))){
result = Optional.ofNullable(result);}}break;case FLUSH:
result = sqlSession.flushStatements();break;default:thrownewBindingException("Unknown execution method for: "+ command.getName());}if(result == null && method.getReturnType().isPrimitive()&&!method.returnsVoid()){thrownewBindingException("Mapper method '"+ command.getName()+" attempted to return null from a method with a primitive return type ("+ method.getReturnType()+").");}return result;}
selectOne
public<T> T selectOne(String statement, Object parameter){//调用selectList
List<T> list =this.selectList(statement, parameter);//返回一条记录,直接返回if(list.size()==1){return list.get(0);}elseif(list.size()>1){//多余1条,报错thrownewTooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: "+ list.size());}else{return null;}}