首先先看一下项目结构
com.wilson.nfc.controller - Controller 层
com.wilson.nfc.mapper - 数据操作层 DAO
com.wilson.nfc.model - 实体类
com.wilson.nfc.service - 业务逻辑层
NfcApplication - 应用启动类
application.properties - 应用配置文件,应用启动会自动读取配置
步骤
1. pom添加mybatis依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
2.在 application.properties 应用配置文件,增加 Mybatis 相关配置
##mybatis
mybatis.typeAliasesPackage=com.wilson.nfc.model
mybatis.mapperLocations=classpath:mapper/*.xml
mybatis.typeAliasesPackage 指向实体包路径
mybatis.mapperLocations 配置为 classpath 路径下 mapper 包下,* 代表会扫描所有 xml 文件。
在 Application 应用启动类添加注解 MapperScan
@MapperScan({"com.wilson.nfc.mapper"})
MapperScan :用这个注解可以注册 Mybatis mapper 接口类。
4.参考
https://www.bysocket.com/?p=1610
5.创建数据库
数据库文件如下,需要创建数据库(nfc_database)之后执行如下sql语句创建表
https://github.com/yxwandroid/springboot-mybatis/blob/master/nfc_database.sql