一、spring boot整合mybatis首先需要添加依赖,pom需要的部分如下:
<!-- spring boot的配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、启动类设置:
@SpringBootApplication 启动项目设置
@ComponentScan(basePackages={"com.unicom.springboot.controller","com.unicom.springboot.service"}) controller、service扫描相关package
@MapperScan(value="com.unicom.springboot.mapper") 扫描mapper的接口
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
三、在application.properties中设置mybatis属性(数据库部分略):
mybatis.typeAliasesPackage=com.unicom.springboot.model //相当于设置mybatis的config.xml属性,扫哪个包
mybatis.mapperLocations=classpath\:mapper/*.xml //寻找mapper下的mapper.xml
四、对UserMapper进行处理,加上@Mapper
@Mapper
public interface UserMapper {
如此配置就可以实现spring boot与mybatis的整合。