1. 程式人生 > >SpringBoot整合MyBatis(iBatis),基於註解和XML兩種方式

SpringBoot整合MyBatis(iBatis),基於註解和XML兩種方式

工具

  1. IDEA
  2. Maven

專案建立

1. 通過IDEA建立SpringBoot專案

這裡寫圖片描述

2. 結構目錄和JAVA版本選擇

這裡寫圖片描述

3. 新增MySQL和MyBatis支援

這裡寫圖片描述

4. 新增Lombok外掛,簡化GET、SET方法

這裡寫圖片描述

5. WEB支援和啟動類

這裡寫圖片描述

6. 專案名和路徑

這裡寫圖片描述

啟動類

package com.attendance;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.
SpringBootApplication; @SpringBootApplication @MapperScan("com.attendance.mapper") public class ProjectApplication { public static void main(String[] args) { SpringApplication.run(ProjectApplication.class, args); } }

@MapperScan註解指明同一掃描路徑

基於XML

1. application.yml(採用更簡潔的yml檔案配置方式,與properties大同小異)

資料庫配置

server:
  port: 7070
spring:
  datasource:
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/attendance?useUnicode=true&useSSL=false&characterEncoding=UTF-8

MyBatis配置

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package
: com.attendance.entity

mapper-locations:指明MyBatis的xml檔案所在位置
type-aliases-package: 指明實體類所在位置

2. Mapper

這裡寫圖片描述
在java檔案路徑和resources路徑下新建mapper資料夾
**com.attendance.mapper.VocationMapper **

package com.attendance.mapper;

import com.attendance.entity.Vocation;
import org.springframework.stereotype.Repository;

@Repository
// @Mapper 可以使用@Mapper註解,但是每個類加註解較麻煩,所以統一配置@MapperScan在application啟動類中
public interface VocationMapper {

    void addVocation(Vocation vocation);
}

classpath:mapper/VocationMapper(resources下的mapper路徑下)

<?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.attendance.mapper.VocationMapper"> <!--將這個介面指向對應的實現類,在介面中引入方法,和com.attendance.mapper.VocationMapper相對應-->
    <resultMap id="vocation" type="Vocation">
        <id property="id" column="id"/>
        <!-- <result property="實體類屬性" column="資料表字段名"/> -->
        <result property="applicant" column="applicant"/>
        <result property="admin" column="admin"/>
        <result property="date" column="date"/>
        <result property="time" column="time"/>
        <result property="leave_days" column="leave_days"/>
        <result property="leave_date" column="leave_date"/>
        <result property="leave_reason" column="leave_reason"/>
        <result property="all_content" column="all_content"/>
        <result property="read_state" column="read_state"/>
    </resultMap>

    <insert id="addVocation" parameterType="vocation"> <!--id和com.attendance.mapper.VocationMapper介面中的方法一一對應-->
        INSERT INTO
        vocation(applicant,admin,date,time,leave_days,leave_date,leave_reason)
        VALUES
        (#{applicant},#{admin},#{date},#{time},#{leave_days},#{leave_date},#{leave_reason})
    </insert>
</mapper>

3. 實體類Vocation

package com.attendance.entity;

import lombok.Data;

@Data
public class Vocation {

    private Integer id;
    private String applicant;
    private String admin;
    private Date date;
    private String time;
    private String leave_days;
    private String leave_date;
    private String leave_reason;
    private String all_content;
    private String read_state;
}

4. Service和ServiceImpl

這裡寫圖片描述
VocationService

package com.attendance.service;

import com.attendance.entity.Vocation;

public interface VocationService {

    void addVocation(Vocation vocation);
}

VocationServiceImpl

package com.attendance.service.impl;

import com.attendance.entity.Vocation;
import com.attendance.mapper.VocationMapper;
import com.attendance.service.VocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VocationServiceImpl implements VocationService {

    @Autowired // 自動裝配
    private VocationMapper vocationMapper;

    @Override
    public void addVocation(Vocation vocation) {
        vocationMapper.addVocation(vocation);
    }
}

基於註解形式

1. application.yml

mybatis:
  type-aliases-package: com.attendance.entity

2. mapper

package com.attendance.mapper;

import com.attendance.entity.Staff;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
public interface StaffRoleMapper {

    @Insert("INSERT INTO staff(staff_name,staff_date) " +
            "VALUES (#{staff.staffName},#{staff.staffDate})")
    @Options(useGeneratedKeys = true, keyProperty = "staff.id")
    void addStaff(@Param("staff") Staff staff);

    @Select("SELECT id FROM role WHERE role = #{role} ")
    int getRoleId(String role);

    @Insert("INSERT INTO staff_roles(staff_id,roles_id) VALUES (#{staff_id},#{role_id})")
    void addStaffWithRole(@Param("staff_id") int staffId, @Param("role_id") int roleId);

    @Delete("DELETE FROM staff_roles WHERE staff_id = #{staff_id}")
    void delStaffWithRole(@Param("staff_id") int staffId);

    @Delete("DELETE FROM staff WHERE id = #{staff_id}")
    void delStaff(@Param("staff_id") int staffId);
}

Service服務類程式碼與配置檔案形式一樣