首页  

mybatis中 DefaultSqlSessionFactory和SqlSessionManager的区别     所属分类 mybatis 浏览量 1118
public class SqlSessionManager implements SqlSessionFactory, SqlSession
public class DefaultSqlSessionFactory implements SqlSessionFactory 

SqlSessionManager 还继承了 SqlSession

SqlSessionManager集成了 sqlSessionFactory和session 的功能

sqlSessionFactory 先 openSession 再使用 
sqlSessionManager 可直接使用


String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 使用配置文件创建 SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
boolean autocommit = true;

SqlSession session = sqlSessionFactory.openSession(autocommit);
UserDO user = session.selectOne("user.getById", 1L);

user = sqlSessionManager.selectOne("user.getById", 1L);


public class SqlSessionManager implements SqlSessionFactory, SqlSession {

  private final SqlSessionFactory sqlSessionFactory;
  private final SqlSession sqlSessionProxy;

  private final ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<>();

  private SqlSessionManager(SqlSessionFactory sqlSessionFactory) {
    this.sqlSessionFactory = sqlSessionFactory;
    this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(
        SqlSessionFactory.class.getClassLoader(),
        new Class[]{SqlSession.class},
        new SqlSessionInterceptor());
  }

	

ThreadLocal  动态代理

上一篇     下一篇
cron表达式中 * 与 ? 的区别

SpringBoot定时任务 schedule

mybatis独立使用(不依赖spring)

mybatis 代码方式创建 SqlSessionFactory

springboot mybatis 多数据源实例

贝尔宾团队角色理论:优秀团队的9种角色