mybatis-plus的学习笔记

前提:本次笔记需要具备mybatis的知识,本人学习mybatis-plus只是因为项目需要

一.mybatis的开发流程

mybatis的开发流程(对于单表的增删改查这些简单的操作,也需要写mapper接口和mapper.xml映射文件,十分繁琐)

mapper接口

@Mapper
public interface UserMapper{

    void saveUser(User user);

    void deleteUser(Long id);

    void updateUser(User user);

    User queryUserById(@Param("id") Long id);

    List<User> queryUserByIds(@Param("ids") List<Long> ids);
}

mapper.xml映射文件

<?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.itheima.mp.mapper.UserMapper">
    <insert id="saveUser" parameterType="com.itheima.mp.domain.po.User">
        INSERT INTO `user` (`id`, `username`, `password`, `phone`, `info`, `balance`)
        VALUES
        (#{id}, #{username}, #{password}, #{phone}, #{info}, #{balance});
    </insert>
    <update id="updateUser" parameterType="com.itheima.mp.domain.po.User">
        UPDATE `user`
        <set>
            <if test="username != null">
                `username`=#{username}
            </if>
            <if test="password != null">
                `password`=#{password}
            </if>
            <if test="phone != null">
                `phone`=#{phone}
            </if>
            <if test="info != null">
                `info`=#{info}
            </if>
            <if test="status != null">
                `status`=#{status}
            </if>
            <if test="balance != null">
                `balance`=#{balance}
            </if>
        </set>
        WHERE `id`=#{id};
    </update>
    <delete id="deleteUser" parameterType="com.itheima.mp.domain.po.User">
        DELETE FROM user WHERE id = #{id}
    </delete>

    <select id="queryUserById" resultType="com.itheima.mp.domain.po.User">
        SELECT *
        FROM user
        WHERE id = #{id}
    </select>

    <select id="queryUserByIds" resultType="com.itheima.mp.domain.po.User">
        SELECT *
        FROM user
        <if test="ids != null">
            WHERE id IN
            <foreach collection="ids" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
        LIMIT 10
    </select>

</mapper>

可见,mybatis的一个单表的操作,所需要的步骤太过于繁琐

二.mybatis-plus的开发流程

1.引入MybatisPlus的起步依赖

MyBatisPlus官方提供了starter,其中集成了Mybatis和MybatisPlus的所有功能,并且实现了自动装配效果。 因此我们可以用MybatisPlus的starter代替Mybatis的starter:

        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.1</version>
        </dependency>

2.定义mapper接口

自定义的Mapper继承MybatisPlus提供的BaseMapper接口

//在对应的Mapper 接口上 基础基本的 BaseMapper<T> T是对应的pojo类
@Repository   //告诉容器你是持久层的 @Repository是spring提供的注释,能够将该类注册成Bean
public interface UserMapper extends BaseMapper<User> {
    //所有的crud都编写完成了
}

public interface UserMapper extends BaseMapper<T> 中的泛型要指定为表所对应的实体类类型

BaseMapper里面的封装的部分方法

insert :新增

delete :删除

update :更新

select : 查询

思考:为什么mybatis-plus知道我们要操作那一张表呢?

mybatis-plus通过扫描实体类,基于反射获取实体类的信息作为数据库表的信息

同时,执行约定大于配置的规则

  • 实体类类名驼峰转下划线 :UserSystem   -->  user_system
  • 实体类中的id属性作为表中字段id的属性
  • 实体类变量名驼峰转下划线作为表中的字段名 : userName  --> user_name
  • 实体类成员属性名以 is 开头且是Boolean值 需要注解指定表中的字段
  • 实体类成员属性名与数据库关键字冲突  需要注解指定表中的字段  指定的形式稍有不同
  • 实体类成员属性名不是表中的字段 需要添加@TableField(exist = false)指定

当实体类与表不符合约定规则,这时可以使用注解进行配置

  • @TableName : 指定表名
  • @TableId : 指定主键
  • @TableField : 指定普通字段信息

如实体类名User  而表名 tb_user  这就需要使用@TableName

常见的配置

三.核心功能

1.条件构造器Wrapper

Wrapper用于构建复杂的SQL语句

1.

2.

局部总结

说明:LambdaQueryWrapper 需要对 lambda 语法比较熟悉

2.自定义SQL

自定义SQL主要是为了代码规范:避免在业务层拼接字段

自定义SQL语句的步骤

1.先在业务层构建条件

2.在mapper接口创建方法 且指定Wrapper的变量名:Constants.WRAPPER

//也可以这么写
    void updateBalanceByIds(@Param(Constants.WRAPPER) QueryWrapper<User> wrapper, @Param("amount") int amount);

3.在mapper.xml映射文件自定义SQL片段

    <update id="updateBalanceByIds">
        UPDATE user SET balance = balance - #{amount} ${ew.customSqlSegment}
    </update>

本次案例

3.IService接口的基本用法

mybatis时,业务层为了解耦,通常会定义一个XXXService接口,然后去XXXServiceImpl 去实现它,但在mybatis-plus中,只要XXXService接口去实现 IService接口 ,而实现类去实现ServiceImpl   即可获取大量的方法

完整业务

  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值