1. 程式人生 > 實用技巧 >springboot 整合mybatis

springboot 整合mybatis

一、前提條件:連線資料庫

二、步驟

1、導包

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

2、建立pojo實體類

3、建立mapper 介面

作用:寫CRUD方法

4、建立mapper.xml檔案

作用:寫SQL語句

5、配置yml

# 整合 mybatis
mybatis:
  mapper-locations: classpath:mybatis/*.xml
  type-aliases-package: com.wt.pojo

注意:mappper 和 locations 踩過坑

6、開啟掃描

在啟動入口新增掃描註解

@MapperScan("com.wt.mapper")

三、案例

1、建立資料庫 新增資料

CREATE TABLE employee(
    id int auto_increment PRIMARY KEY,
    gender int COMMENT "0 女 1男",
    name 
VARCHAR(20) COMMENT "員工姓名", email VARCHAR(50) COMMENT "員工郵箱", birth date COMMENT "生日", depart_id int COMMENT "對應部門表的id" )ENGINE=INNODB DEFAULT charset=utf8 create table department( id int auto_increment PRIMARY KEY, name VARCHAR(20) )ENGINE=INNODB default charset=utf8

2、待續,