Mybatis-plus和Mybatis的区别和详情讲解(附代码详解)

本文详细介绍了Mybatis与Mybatis-plus的区别,包括Mybatis的Mapper层、Service层和Controller层的使用,然后重点讲解了Mybatis-plus的优势,如简化XML配置、内置通用Mapper与Service。此外,还提供了如何使用Mybatis-plus以及实现特殊SQL语句的示例,适合开发者了解和学习。
摘要由CSDN通过智能技术生成

文章目录

1.简介

首先,在介绍Mybatis-plus之前呢,明怀我想给大家介绍一下Mybatis,其实mp(mybatis-plus)是对mybatis的一个增强,我更推荐大家在使用的时候可以结合着Mybatis和mp一起使用

2.Mybatis

在这里插入图片描述

2.1Mapper层

mybatis在使用的过程当中,这里我再简要地来做一下讲解吧,首先也是建立相应的数据库表,然后再通过建立对应的实体类,着我想没必要再写上了吧,然后,需要做的就是书写对应的接口:

package com.example.qhb.model.mapper;

import com.example.qhb.model.entity.RedDetail;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

/*
2022.1.18
 */

@Service

public interface RedDetailMapper {
    int deleteByPrimaryKey(Integer id);//根据主键id删除
    int insert(RedDetail record);//插入数据记录
    int insertSelective(RedDetail record);//插入数据记录
    RedDetail selectByPrimaryKey(Integer id);//根据主键id查询记录
    int updateByPrimaryKeySelective(RedDetail record);//更新数据记录
    int updateByPrimaryKey(RedDetail record);//更新数据记录

}

然后,我们又该怎么做呢,那就是针对这个接口设置相应的相应的xml文件,在这里面就是设置的是:

1.建立结果集映射,也就是代码中的resultMap。
2.根据具体的sql语句,书写出来对应的sql

<?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.example.qhb.model.mapper.RedDetailMapper">
    <!--定义查询结果集映射-->
    <resultMap id="BaseResultMap" type="com.example.qhb.model.entity.RedDetail">
        <result column="id" property="id" jdbcType="INTEGER"/>
        <result column="record_id" property="recordId" jdbcType="INTEGER"/>
        <result column="amount" property="amount" jdbcType="DECIMAL"/>
        <result column="is_active" property="isActive" jdbcType="TINYINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,record_id,amount,is_active,create_time
    </sql>

    <!--根据主键查询数据库-->
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select
             <include refid="Base_Column_List"/>
        from red_detail
        where id=#{id,jdbcType=INTEGER}
    </select>

    <!--根据主键删除数据记录-->
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
           delete from red_detail
           where id = #{id, jdbcType=INTEGER}
    </delete>
    <!--新增数据记录(不带判断条
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值