首先要了解mybatis是什么?
mybatis是一款半自动的ORM持久层框架(也就是dao层或者mapper层),具有较高的SQL灵活性,支持高级映射(一对一,一对多),动态SQL,延迟加载和缓存等特性,但它的数据库无关性较低。
先在pom.xml中引入依赖
//引入mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
//连接mysql
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
//热部署
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
需要注意的是看看自己电脑上的mysql是多少版本的,如果是5.7以下的就需要更换连接mysql的依赖,8.0的可以直接用我这个。
先看目录结构
application.properties和application.yml两个配置文件选择一个就好了,application.yml相对简单,简化了很多代码。
#连接数据库 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test_database?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username: root password: 123456 mvc: pathmatch: matching-strategy: ant_path_matcher #设置需要扫描的映射文件和实体类 mybatis: mapper-locations: classpath:mapper/*Mapper.xml type-aliases-package: com.example.springboot_demo.bean configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #自定义端口号 server: port: 8088
根据自己数据库的情况改动,主要改动连接的端口号,用户名,密码。
根据自己的映射文件放置目录和实体类放置目录更改mybatis的配置。
我的目录结构已经放在上面。。。