SpringBoot整合MyBatis(配置文件版)

本文最后更新于:10 个月前

说明

此文章正在升级中,仅供参考

简述

在之前的文章中,已经配置好了使用Druid连接池的数据源,接下来就在SpringBoot中集成一下Mybatis。集成MyBatis的流程可以说是非常的简单了。

编写MyBatis的配置文件

首先在resources文件加下创建一个config文件夹,在config中新建mybaits-config.xml配置文件,在此配置文件中,主要配置mybatis的一些常用属性,以及别名的配置等等,这里我选择了几个比较常用到的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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>
<settings>
<!--配置缓存关闭-->
<!--<setting name="cacheEnable" value="false"/>-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="REUSE"/>
<!--事务超时时间-->
<setting name="defaultStatementTimeout" value="600"/>
</settings>
<!--配置com.javafeng.site.models.Account的别名-->
<typeAliases>
<typeAlias type="com.javafeng.site.models.Account" alias="account"/>
</typeAliases>
</configuration>

创建mapper.xml文件夹

在resources中创建mapper文件夹,存放所有的Mapper映射文件

创建mapper接口包

在Application入口类的同级目录下创建包mapper(我习惯使用dao,因此我这里是dao包),其中存放所有的mapper接口文件。

为了SpringBoot启动时,能正确找到所有的Mapper接口类,在启动类上使用@MapperScan注解指定mapper接口文件的包路径,程序启动后会直接扫描这个包下的@Mapper注解,来找到mapper接口,写法如下:

1
2
3
4
5
6
7
@SpringBootApplication
@MapperScan("com.javafeng.site.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

YML配置文件

在YML配置文件中,需要指定Mybatis配置问价的加载路径以及Mapper映射文件的加载路径,使程序启动之后能正确加载Mybatis的配置文件和找到Mapper.xml映射文件。

1
2
3
mybatis:
config-location: classpath:/config/mybatis-config.xml
mapper-locations: classpath:/mapper/*.xml

逆向工程生成文件

这里使用了逆向工程来生成所有的model实体类、mapper接口和mapper.xml映射文件,逆向工程工具包可以在我SSM的那篇文章末尾下载。生成之后,将所有的文件放到各自的包中,注意,需要在接口上使用@Mapper注解。

测试

具体的Mybatis使用方法,可以移步本站SSM框架搭建文章。

这里以Account用户表的查询操作来示例。

在生成的AccountMapper.xml中编写查询所有用户的Sql:

1
2
3
<select id="selectAll"  resultType="account">
select * from account
</select>

注意这里的resultType属性,就是用了我上述mybatis-config.xml中配置的别名

AccountMapper.java接口类中添加对应的接口方法

1
2
3
4
5
@Mapper
public interface AccountMapper {
……
List<Account> selectAll();
}

在启动类同级创建service包,编写service接口和实现类,

AccountService.java接口,查询所有用户

1
2
3
public interface AccountService {
public List<Account> getAllAccount();
}

编写AccountService的实现类:AccountServiceImpl类,注入Mapper,调用Mapper中的selectAll()方法,并在AccountServiceImpl上使用@Service注解来注册Service Bean。

1
2
3
4
5
6
7
8
9
10
11
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
AccountMapper accountMapper;

@Override
public List<Account> getAllAccount() {
return accountMapper.selectAll();
}
}

编写Controller方法,调用Service中的getAllAccount()方法查询所有用户:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RestController
public class OneController {

@Autowired
AccountService accountService;

@RequestMapping("/find")
public String all(ModelMap modelMap) {
List<Account> accounts= accountService.getAllAccount();
System.out.println(accounts.get(0).getName());
return "查询成功";
}
}

以上就是SpringBoot整合Mybatis的过程。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!