目录
1、目的:梳理一下MyBatis运行时的几个对象,我们需要搞清楚他们的作用,进而需要理解mybatis的整个工作流程和执行原理。
2、简要概括各个类
2.1 Resources
作用:编写资源加载类,使用类加载器加载 配置文件(mybatis-config.xml ),即:读取mybatis-config.xml
2.2 SqlSessionFactoryBuilder
作用:将配置资源封装成Configuration对象,并且将该资源对象传到工厂对象中
2.2.1 Configuration
作用:配置类 --> 把所有的配置信息 都装进去
2.2.2 MappedStatement
作用:用来封装 sql语句 和 查询结果集(即:返回值类型)
2.3 SqlSessionFactory
2.4 SqlSession
2.5 ProxyFactory
3、测试
1、目的:梳理一下MyBatis运行时的几个对象,我们需要搞清楚他们的作用,进而需要理解mybatis的整个工作流程和执行原理。
mybatis-config.xml
mapper.xml mapper.xml mapper.xml...
|
|
Resources
|
|---->InputStream
|
SqlSessionFactoryBuilder
|
|---->Configuration(conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>)
|
SqlSessionFactory
|
|---->Configuration(conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>)
|
SqlSession
|
|---->conn, Map<namespace+"."+id, MappedStatement(sql, resultType)>
|
ProxyFactory
|
|---->MappedStatement(sql, resultType), conn
|
Executor
| |
| |
| |
输入出映射
2、简要概括各个类
2.1 Resources
作用:编写资源加载类,使用类加载器加载 配置文件(mybatis-config.xml ),即:读取mybatis-config.xml
package com.by.io;
import java.io.InputStream;
public class Resources {
//根据文件名称,加载类路径下面的配置文件
public static InputStream getResourceAsStream(String filePath){
return Resources.class.getClassLoader().getResourceAsStream(filePath);
}
}
2.2 SqlSessionFactoryBuilder
作用:将配置资源封装成Configuration对象,并且将该资源对象传到工厂对象中
package com.by.builder;
import com.by.cfg.Configuration;
import com.by.factory.DefaultSqlSessionFactory;
import com.by.factory.SqlSessionFactory;
import com.by.utils.XMLConfigBuilder;
import java.io.InputStream;
public class SqlSessionFactoryBuilder {
/**
* 构建SqlSessionFactory对象
* @param in
* @return
*/
public SqlSessionFactory build(InputStream in){
Configuration configuration = XMLConfigBuilder.loadConfiguration(in);
return new DefaultSqlSessionFactory(configuration);
}
}
2.2.1 Configuration
作用:配置类 --> 把所有的配置信息 都装进去
package com.by.cfg;
import com.by.mapping.MappedStatement;
import java.util.HashMap;
import java.util.Map;
public class Configuration {
private String driver;
private String url;
private String username;
private String password;
/**
Map<"com.by.dao.UserDao.findAll", MappedStatement>
*/
private Map<String, MappedStatement> mappers = new HashMap<String,MappedStatement>();
public Map<String, MappedStatement> getMappers() {
return mappers;
}
public void setMappers(Map<String, MappedStatement> mappers) {
this.mappers.putAll(mappers);//此处需要使用追加的方式
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.2.2 MappedStatement
作用:用来封装 sql语句 和 查询结果集(即:返回值类型)
package com.by.mapping;
public class MappedStatement {
private String queryString;//SQL
private String resultType;//实体类的全限定类名
public String getQueryString() {
return queryString;
}
public void setQueryString(String queryString) {
this.queryString = queryString;
}
public String getResultType() {
return resultType;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
}
2.3 SqlSessionFactory
package com.by.factory;
public interface SqlSessionFactory {
//获取SQLSession对象
public SqlSession openSession();
}
package com.by.factory;
import com.by.cfg.Configuration;
import com.by.session.DefaultSqlSession;
import com.by.session.SqlSession;
public class DefaultSqlSessionFactory implements SqlSessionFactory {
private Configuration cfg;
public DefaultSqlSessionFactory(Configuration cfg) {
this.cfg = cfg;
}
/**
* 获取一个SqlSession对象
* @return
*/
@Override
public SqlSession openSession() {
return new DefaultSqlSession(cfg);
}
}
2.4 SqlSession
public interface SqlSession {
//获取代理对象
public <T> T getMapper(Class<T> tClass);
//释放资源
void close();
}
package com.by.session;
import com.by.utils.DataSourceUtil;
import com.by.cfg.Configuration;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
public class DefaultSqlSession implements SqlSession {
private Configuration cfg;
private Connection conn;
public DefaultSqlSession(Configuration cgf){
this.cfg = cgf;
this.conn = DataSourceUtil.getConnection(cfg);
}
/*
* 创建代理对象
*/
@Override
public <T> T getMapper(Class<T> tClass) {
/**
* tClass.getClassLoader():类加载器
* new Class[]{tClass}:Class数组,让代理对象和被代理对象有相同的行为
* new ProxyFactory:调用真实角色,附加自己的操作
*/
return (T) Proxy.newProxyInstance(tClass.getClassLoader(),
new Class[]{tClass},
new ProxyFactory(cfg.getMappers(),conn));
}
@Override
public void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 ProxyFactory
package com.by.session;
import com.by.mapping.MappedStatement;
import com.by.utils.Executor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Map;
public class ProxyFactory implements InvocationHandler {
private Map<String, MappedStatement> mappers;
private Connection conn;
public ProxyFactory(Map<String, MappedStatement> mappers, Connection conn){
this.mappers = mappers;
this.conn = conn;
}
//调用代理对象的任何方法,都会在这执行
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//1.获取方法名
String methodName = method.getName();
//2.获取方法所在类的名称
String className = method.getDeclaringClass().getName();
//3.组合key
String key = className+"."+methodName;
//4.获取mappers中的Mapper对象
MappedStatement mappedStatement = mappers.get(key);
//5.判断是否有mapper
if(mappedStatement == null){
throw new IllegalArgumentException("传入的参数有误");
}
//6.调用工具类执行查询所有
return new Executor().selectList(mappedStatement,conn);
}
}
3、测试
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
System.out.println("-----" + factory);
//3.使用工厂生产SqlSession对象
SqlSession session = factory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
UserDao userDao = session.getMapper(UserDao.class);
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user : users){
System.out.println(user);
}
//6.释放资源
session.close();
in.close();