获取流解析
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
调用getResourceAsStream(String resource)方法
public static InputStream getResourceAsStream(String resource) throws IOException {
return getResourceAsStream(null, resource);
}
调用getResourceAsStream(ClassLoader loader, String resource)方法
public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
if (in == null) {
throw new IOException("Could not find resource " + resource);
}
return in;
}
通过classLoaderWrapper.getResourceAsStream(resource, loader)方法来获取流,我们再继续跟进getResourceAsStream(resource, loader)方法
public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
return getResourceAsStream(resource, getClassLoaders(classLoader));
}
再继续跟进getResourceAsStream(resource,getClassLoaders(classLoader))方法
InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
for (ClassLoader cl : classLoader) {
if (null != cl) {
// try to find the resource as passed
InputStream returnValue = cl.getResourceAsStream(resource);
// now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
if (null == returnValue) {
returnValue = cl.getResourceAsStream("/" + resource);
}
if (null != returnValue) {
return returnValue;
}
}
}
return null;
}
获取SqlSessionFactory对象解析
SqlSessionFactory sqlsessionfactor = new SqlSessionFactoryBuilder().build(is);
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
调用build(inputStream, null, null);方法
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
inputStream.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
跟进build(parser.parse())方法
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
获取SqlSession对象解析
SqlSession session = sqlsessionfactor.openSession();
SqlSessionFactory创建的时候实际上返回的是一个DefaultSqlSessionFactory对象
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
final Executor executor = configuration.newExecutor(tx, execType);
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
获取Mapper对象解析
UserMapper mapper = session.getMapper(UserMapper.class);
需要进入到getMapper()方法中去,这里调用的是DefaultSqlSession中的getMapper()
来获取UserMapper的时候,实际上是从configuration当中的MapperRegistry当中获取UserMapper的代理对象:
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) 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);
}
}
knownMappers属性里面的值,实际上就是我们在mappers扫描与解析的时候放进去的。
protected T newInstance(MapperProxy<T> mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
分页查询
结果展示
测试类
public class Test {
public static void main(String[] args) throws IOException { String resource="mybatis-config.xml";
InputStream is= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
SqlSession session= sqlSessionFactory.openSession();
T7mapper mapper= session.getMapper(T7mapper.class);
Map<String,Object> map = new HashMap<>();
map.put("startIndex",1);
map.put("pageSize",1);
List<T7> content=mapper.queryLimit(map);
System.out.println(content);
}