有两种方式,分为 mybatis-spring 的方式和 mybatis-spring-boot-starter 的方式
1.1.1、mybatis-spring 方式
引入依赖 和 编写配置信息
**步骤一:**引入依赖。在 pom 文件中引入与 MyBatis 相关的依赖
<dependency>
<artifactId>mybatis</artifactId>
<groupId>org.mybatis</groupId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<artifactId>mysql-connector-java</artifactId>
<groupId>mysql</groupId>
<version>5.1.38</version>
</dependency>
<dependency>
<artifactId>spring-boot-starter-jdbc</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
**步骤二:**编写配置信息。需要配置的信息有 DataSource、SqlSessionFactoryBean、MapperScannerConfiguration
-
配置 DataSource
-
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot username: root password: root
-
配置 SqlSessionFactoryBean
-
@Configuration public class DataSourceUtil { @Autowired private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactoryBean(){ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); try { //设置数据源 sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setTypeAliasesPackage("com.example.chapter_four.entity"); return sqlSessionFactoryBean.getObject(); }catch (Exception e){ e.printStackTrace(); throw new RuntimeException(); } } }
setTypeAliasesPackage() 方法,用于指定的包下的实体类起别名。
如果要指定 MyBatis 配置文件的位置,则需要使用另一个类——PathMatchingRescourcePatternResolver.java,此类提供了 getResource() 方法,可以指定 MyBatis 配置文件的位置。
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource mybatisConfXml = resolver.getResource("com.example.chapter_four.mapper"); sqlSessionFactoryBean.setConfigLocation(mybatisConfXml);
-
配置 MapperScannerConfigurer
-
@Configuration public class MyBatisConf { @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ //定义扫描器实例 MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); //加载 SqlSessionFactory,通过 DataSourceUtil 中配置的 SqlSessionFactoryBean 生产 SqlSessionFactory 实例 mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); //定义扫描的包 mapperScannerConfigurer.setBasePackage("com.example.chapter_four.mapper"); return mapperScannerConfigurer; } }
-
控制器
-
@RestController public class UserController { @Resource private UserService userService; @RequestMapping(value = "/getUserById", method = RequestMethod.GET) public String getUserById(@RequestParam String id)throws Exception { return userService.getUserById(Integer.parseInt(id)).getUserName(); } }
@RestController 标注 Controller。是一个组合注解,是 @Controller 和 @ResponseBody 的组合。
1.1.2、mybatis-spring-boot-starter 方式
更加精简
第一步:引入依赖
<dependency>
<artifactId>mysql-connector-java</artifactId>
<groupId>mysql</groupId>
<version>5.1.38</version>
</dependency>
<dependency>
<artifactId>spring-boot-starter-jdbc</artifactId>
<groupId>org.springframework.boot</groupId>
</dependency>
由于 spring-boot-starter-parent 没有指定 mybatis-spring-boot-starter 的版本号,所以在引入此依赖时,需要手动指定版本号。
第二步:编写配置
Spring Boot 已经通过自动配置,注入了 DataSource,需要在 application.yml 文件中配置数据源、指定 mapper 映射文件和接口的位置、MyBatis 配置文件的位置和需要起别名的实体所在的包的位置。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot
username: root
password: root
enabled: false
mybatis:
# Mapper 映射文件的位置
mapper-locations: classpath:com/example/chapter_four/mapper/*.xml
# 需要起别名的 Entity 所在包的位置
type-aliases-package: com.example.chapter_four.entity
还缺少了指定 Mapper 接口位置的配置。
@Mapper:标注到 Mapper 接口
@MapperScan:标注到启动类上,或者某个配置类上(即被 @Configuration 标注的类)
使用以上两个注解的任一个都可指定 Mapper 的接口位置。
数据源和事务
Spring Boot 有内置数据库:H2、HSQL、Derby。这三种数据库都是内存数据库,不提供对数据持久化的支持。
如果对数据库连接池进行配置,Spring Boot 也有默认支持,比如 Hikari 和 Tomcat 连接池。也可以使用其他连接池。
<dependency>
<artifactId>commons-dbcp2</artifactId>
<groupId>org.apache.commons</groupId>
</dependency>
spring-boottg-starter-parent 指定了 commons-dbcp2 的版本号,在引入此依赖时不需要手动指定版本号
application.yml 中配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot
username: root
password: root
# 数据源的类型
type: org.apache.commons.dbcp2.BasicDataSource
dbcp2:
max-idle: 10
max-total: 50
注意:使用 DBCP2 配置连接池时,需要指定 spring.datasource.type 为 org.apache.commonns.dbcp2.BasicDataSource。否则,仍会使用内置的数据源,而不会使用 DBCP2 数据源。
可以使用 @Transactional 注解管理事务,可以添加到业务类上,也可以添加到业务方法上。