SpringBoot整合MyBatis开发框架
1、如果要进行mybatis的配置一定要导入spring-boot所支持的mybatis开发包。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

2、随后要去修改application.yml配置文件,追加mybatis的相关配置项:
server:
port: 8080
spring:
messages:
basename: i18n/Message,i18n/Pages #资源文件的名称
datasource:
#配置当前要使用的数据源的操作类型
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/mldn_1
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.gwolf.vo
mapper-locations:
- classpath:mybatis/mapper/**/*.xml

3、建立一个Dept的vo类:
package com.gwolf.vo;
import java.io.Serializable;
public class Dept implements Serializable {
private Long deptno;
private String dname;
public Long getDeptno() {
return deptno;
}
public void setDeptno(Long deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
@Override public String toString() { return "Dept{" + "deptno=" + deptno + ", dname='" + dname + '\'' + '}'; }
}

4、在src/main/resources目录下建立有一个mybatis/mybatis.cfg.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <!-- 进行Mybatis的相应的环境的属性定义 -->
<settings> <!-- 在本项目之中开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>

5、建立IDeptDAO接口,注意接口所在的包:
package com.gwolf.dao;
import com.gwolf.vo.Dept;
@Mapper
public interface IDeptDAO {
public List<Dept> findAll();
}
在定义DAO接口的时候由于需要自动生成实现子类,所以在接口声明处一定要编写一个“@Mapper”的注解,否则你的DAO的接口和*.xml的Mapper文件无法整合在一起。

6、在src/main/resources/mybatis目录下建立有一个mapper子目录,而后在里面定义有com/gwolf/Dept.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.gwolf.dao.IDeptDAO">
<select id="findAll" resultType="Dept">
SELECT deptno,dname,loc FROM dept ;
</select>
</mapper>

7、建立一个IDeptService接口,作为服务使用。
package com.gwolf.service.impl;
import com.gwolf.dao.IDeptDAO;
import com.gwolf.service.IDeptService;
import com.gwolf.vo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements IDeptService{
@Autowired
private IDeptDAO deptDAO;
@Override
public List<Dept> getAll() {
return this.deptDAO.findAll();
}
}

8、进行代码测试类的编写:
package com.gwolf.test;
import com.gwolf.StartSpringBootMain;
import com.gwolf.service.IDeptService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestDeptService {
@Resource
private IDeptService deptService;
@Test
public void testList() throws Exception{
System.out.println(this.deptService.getAll());
}
}

9、此时测试通过,则springboot与mybatis已经可以成功的整合在一起进行项目开发,此时的配置要比之前使用spring+mybatis直接配置简单N多倍。
