前言
现在使用mybatis只需引入如下依赖,mybatis自动导入四个包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
依赖出现的时间线和作用
mybatis-->mybatis-spring-->mybatis-spring-boot-starter,按时间线使用mybatis
mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
try (SqlSession session = sqlSessionFactory.openSession()) {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
}
可以看到需要使用的核心类SqlSessionFactory、SqlSession都是手动生成,显示使用
mybatis+mybatis-spring
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${project.version}</version>
</dependency>
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
return factoryBean.getObject();
}
@Bean
public UserMapper userMapper() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
return sqlSessionTemplate.getMapper(UserMapper.class);
}
}
需要编码注入SqlSessionFactory作为bean,手动使用SqlSessionFactory得到需要的Mapper供业务注入。可以看到比单独使用mybatis更方便了,不用在业务使用时显示获取SqlSession,可以猜测SqlSessionTemplate已经封装或实现了SqlSession的功能
mybatis+mybatis-spring+mybatis-spring-boot-starter
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
@Mapper
public interface CityMapper {
@Select("SELECT * FROM CITY WHERE state = #{state}")
City findByState(@Param("state") String state);
}
引入依赖后只需要在对应的接口上添加@Mapper就可使用,方便多了,说明mybatis-spring-boot-starter内部已经把前面需要的SqlSessionFactory生成并注入bean,需要的SqlSessionTemplate在@Mapper扫描接口或调用方法内部已经封装进去,无需在手动定义每个业务的mapper
包的官方解析
通过上面使用方式的进化已经做了一些猜测,下面看看每个包的官方解释
mybatis-spring
MyBatis-Spring integrates MyBatis seamlessly with Spring. This library allows MyBatis to participate in Spring transactions, takes care of building MyBatis mappers and SqlSessions and inject them into other beans, translates MyBatis exceptions into Spring DataAccessExceptions, and finally, it lets you build your application code free of dependencies on MyBatis, Spring or MyBatis-Spring.
从代码使用示例可以看到:takes care of building MyBatis mappers and SqlSessions and inject them into other beans。mybatis-spring负责构建业务需要使用的mapper和SqlSession,并且让你的业务代码不再依赖mybatis的相关依赖。code free of dependencies on...。在单独使用mybatis会看到业务代码中要手动获取SqlSession,没有解耦
mybatis-spring-boot-starter
The MyBatis-Spring-Boot-Starter help you build quickly MyBatis applications on top of the Spring Boot. As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at least one mapper interface.
MyBatis-Spring-Boot-Starter will:
- Autodetect an existing
DataSource - Will create and register an instance of a
SqlSessionFactorypassing thatDataSourceas an input using theSqlSessionFactoryBean - Will create and register an instance of a
SqlSessionTemplategot out of theSqlSessionFactory - Auto-scan your mappers, link them to the
SqlSessionTemplateand register them to Spring context so they can be injected into your beans
从使用方式的进化上已能看出如上作用,不再赘述,每个所有文档也有对应的翻译