zhss_springboot整合mybatis
2020:08:14 10:15
一、mybatis-spring-boot-starter的工作原理
(1)自动发现一个注册好的DataSource,之前SSM框架需要手动配置applicationContext.xml,springboot不需要直接@Configuration作用代替以前的xml配置文件
(2)自动创建一个SqlSessionFactory,并且将DataSource传入SqlSessionFactory中
(3)自动基入SqlSessionFactory创建一个SqlSessionTemplate
(4)扫描所有的Mapper,将SqlSessionTemplate注入其中,然后将Mapper注册到spring容器上下文中
二、pom.xml依赖引入
<!-- 引入mybatis-spring-boot-starter依赖,用于mybatis与spring boot整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 引入mysql驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 引入spring-boot-starter-data-jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入阿里的druid连接池依赖 -->