SpringBoot整合MyBatis(iBatis),基于注解和XML两种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SWPU_Lipan/article/details/80385377

工具

  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服务类代码与配置文件形式一样

展开阅读全文

没有更多推荐了,返回首页