基于上篇Mybatis入门(一)的基础上,整合到SpringBoot中。
一、依赖
相较于Mybatis版本,这里的依赖换成了mybatis-spring-boot-starter
在该依赖中已经包含了Mybatis的依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
</dependencies>
SpringBootMybatis 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、配置文件
在SpringBoot中,将上一个版本的mybatis-config.xml的配置在Spring中进行整合。包括数据源、mybatis mapper映射的配置
# 数据源
spring:
datasource:
password: root
username: root
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis相关配置
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
注:mapper-locations相当于mybatis-config.xml中的作用,通过配置mapper.xml路径将自动扫描匹配的mapper.xml文件。如果没有配置将会报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.stopping.springmybatis.mapper.UserMapper.selectUser
配置别名
<select id="selectUser" resultType="com.stopping.springmybatis.model.User">
select * from user order by id;
</select>
像之前在Mapper.xml编写resultType的时候总是要将实体类的全路径名都写上,为了提高效率可以在配置文件中配置type-aliases-package
跟上package路径名。
<select id="selectUser" resultType="User">
select * from user order by id;
</select>
mybatis:
type-aliases-package: com.stopping.springmybatis.model
驼峰命名法
mybatis:
configuration:
map-underscore-to-camel-case: true
在开启map-underscore-to-camel-case: true
之后,数据库字段的下划线会自动映射为实体类的驼峰命名。resultType返回的实体,默认是与数据库字段一致,mybatis会自动映射对应的数据类型和字段。
但是如果数据库表字段是user_id
根据规范我们使用驼峰命令的话,需要开启该配置,mybatis就会自动映射到userId上。
三、测试
@SpringBootTest(classes = SpringMybatisApplication.class)
class UserMapperTest {
@Resource
UserMapper userMapper;
@Test
void selectUser() {
userMapper.selectUser().stream().forEach(user -> {
System.out.println(user.getUsername());
});
}
}
结果
admin
tom
job