mybatis的基本API

	@BeforeClass
	public static void init() throws IOException {
		//第一步、把全局配置文件mybatis-config.xml读入流中
		is = Resources.getResourceAsStream("mybatis-config.xml");
		//第二步、通过流(全局配置文件)创建一个sqlsession的工厂构建器
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		//第三步、通过sqlsession工厂,创建sqlsession对象
		sqlSession = sqlSessionFactory.openSession();
		//第四步、通过sqlsession获取StudentDao的对象
		adminDao = sqlSession.getMapper(AdminDao.class);
	}
1,is = Resources.getResourceAsStream(“mybatis-config.xml”);
  1. 加载配置文件,将配置文件读到流中(从硬盘读到内存中)
2,SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
  1. 创建 SqlSessionFactoryBuilder接口的实现类对象
  2. 在调用bulid()方法创建一个构建工厂
  3. 并将流里面的数据信息装到 Configuration对象中
3, sqlSession = sqlSessionFactory.openSession();
  1. 创建sqlSession对象(DefaultSqlSession)

  2. 设置事务管理,是否设置自动提交事务

  3. 创建一个Excutor(SimpleExcutor),连接数据库,执行sql语句

    StatementHandler,P

4,adminDao = sqlSession.getMapper(AdminDao.class);
  1. 生成代理类MapperProxy,实现指定的接口
  2. 调用了接口的方法,实际上是执行了代理类的方法
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐