On the basis of Mybatis introduction (a), integrated into SpringBoot.
A, rely on
Compared with the Mybatis version, the dependency is changed to Mybatis -spring-boot-starter, which already contains the Mybatis dependency.
<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>
Copy the code
SpringBootMybatis rely on
<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>
Copy the code
Configuration file
In SpringBoot, the configuration of the previous version of mybatis-config.xml is integrated in Spring. The configuration includes data source and Mybatis Mapper mapping
# data source
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
Copy the code
Note: Mapper-locations is the function of mybatis-config. XML. By configuring the mapper. XML path, it automatically scans the matching mapper. XML file. An error will be reported if there is no configuration:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.stopping.springmybatis.mapper.UserMapper.selectUser
Copy the code
Configure an alias
<select id="selectUser" resultType="com.stopping.springmybatis.model.User">
select * from user order by id;
</select>
Copy the code
For example, when compiling resultType in mapper. XML, the full pathname of entity class should always be written down. In order to improve efficiency, type-aliases-package can be configured in the configuration file to follow the package pathname.
<select id="selectUser" resultType="User">
select * from user order by id;
</select>
Copy the code
mybatis:
type-aliases-package: com.stopping.springmybatis.model
Copy the code
Hump nomenclature
mybatis:
configuration:
map-underscore-to-camel-case: true
Copy the code
When map-underscore-to camel-case: True is turned on, the underscores of the database fields are automatically mapped to the camel name of the entity class. The entity returned by resultType is the same as the database field by default. Mybatis will automatically map the corresponding data type and field.
Mybatis will automatically map to userId if the database table field is user_id according to the specification we use hump command.
Three, test,
@SpringBootTest(classes = SpringMybatisApplication.class) class UserMapperTest { @Resource UserMapper userMapper; @Test void selectUser() { userMapper.selectUser().stream().forEach(user -> { System.out.println(user.getUsername()); }); }}Copy the code
The results of
admin
tom
job
Copy the code