spring boot整合mybatis框架
1、推荐使用intellij idea作为开发工具,使用intellij idea创建spring boot框架,如下图所示,添加maven依赖,代码如下
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

2、创建一个TestMapper.java文件,代码如下
/**
* Created by zuli on 2018/10/10.
*/
public interface TestMapper {
int test();
}
3、在resources目录下创建TestMapper.xml文件,代码如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zuli.mapper.TestMapper">
<select id="test" resultType="int">
SELECT 1 FROM DUAL
</select>
</mapper>

4、添加@MapperScan()注解,配置basePackages属性,代码如下
@SpringBootApplication
@MapperScan(basePackages = "com.zuli.mapper")
public class SpringBootMybatisApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(SpringBootMybatisApplication.class, args);
TestMapper testMapper = context.getBean(TestMapper.class);
System.out.println(testMapper.test());
}
}

5、修改application.properties配置文件,配置数据源,mapper xml文件路径等代码如下
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath:*Mapper.xml

6、启动应用程序,调用TestMapper的test()方法,打印日志,如下图所示

7、整体demo的项目结构如下图所示
