文章目录
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>
<!--新增数据记录(不带判断条