导航
核心组件
- SqlSessionFactoryBuilder
- 通过 xml 或 java 编码的方式获得资源来构建 SqlSessionFactory
- 一个 builder 可以构建多个 SqlSessionFactory
- 生命周期一般只存在与方法的局部,用完即可回收
1 | SqlSessionFactory build = new SqlSessionFactoryBuilder().build(configuration); |
- SqlSessionFactory
- 创建 sqlSession
- 应该在 mybatis 应用的整个生命周期中,一般使用单例模式创建
1 | SqlSession sqlSession = sqlSessionFactory.openSession(); |
- SqlSession
- 既可以执行 sql 也可以获取 mapper 对象
- 线程不安全,生命周期应该是请求数据库处理事务的过程中
1 | UserMapper mapper = sqlSession.getMapper(UserMapper.class); |
- Mapper
- 负责发送 sql 去执行,并返回结果
标签
- 九大动态标签
- if:单条件
- choose,when,otherwise:多条件
- trim,where,set:对拼接的 sql 进行处理
- bind:模糊匹配查询是使用,提高数据库的可移植性
- 其他标签举例
- select,updata,delete,insert
- resultMap,parameterMap,sql,include,selectKey
- trim,where,set,foreach,if,choose,when,otherwise,bind