【MyBatisPlus】快速入门、常用注解、常用配置


🐌个人主页: 🐌 叶落闲庭

💨我的专栏:💨
c语言
数据结构
javaEE
操作系统
Redis

石可破也,而不可夺坚;丹可磨也,而不可夺赤。


MyBatisPlus

  • 一、快速入门
    • [1.1 引入MyBatisPlus起步依赖](#1.1 引入MyBatisPlus起步依赖)
    • [1.2 自定义的Mapper继承MyBatisPlus的BaseMapper接口](#1.2 自定义的Mapper继承MyBatisPlus的BaseMapper接口)
    • [1.3 对比Mybatis](#1.3 对比Mybatis)
    • [1.4 MyBatisPlus的增删改查方法](#1.4 MyBatisPlus的增删改查方法)
  • 二、MyBatisPlus常用注解
    • [2.1 MyBatisPlus常用注解如下](#2.1 MyBatisPlus常用注解如下)
  • 三、MyBatisPlus常用配置

一、快速入门

MyBatisPlus官方提供了starter,其中集成了Mybatis和MybatisPlus的所有功能,并且实现了自动装配效果。

1.1 引入MyBatisPlus起步依赖

xml 复制代码
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.4.2</version>
</dependency>

1.2 自定义的Mapper继承MyBatisPlus的BaseMapper接口

java 复制代码
public interface UserMapper extends BaseMapper<User> {
}

1.3 对比Mybatis

  • 要操作的数据库表结构:
sql 复制代码
create table user
(
    id      int not null primary key,
    account int null
);
  • Mybatis的UserMapper.java:
java 复制代码
public interface UserMapper {
    int insert(User row);

    int insertSelective(User row);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User row);

    int updateByPrimaryKey(User row);
}
  • Mybatis的UserMapper .xml:
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.demo.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.demo.po.User">
  </resultMap>
  <sql id="Base_Column_List">
    id, account
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <insert id="insert" parameterType="com.demo.po.User">
    insert into user (id, account)
    values (#{id,jdbcType=INTEGER}, #{account,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.demo.po.User">
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="account != null">
        account,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="account != null">
        #{account,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.demo.po.User">
    update user
    <set>
      <if test="account != null">
        account = #{account,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.demo.po.User">
    update user
    set account = #{account,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>
  • MyBatisPlus只需UserMapper.java继承 BaseMapper<>即可:
java 复制代码
public interface UserMapper extends BaseMapper<User> {
}

1.4 MyBatisPlus的增删改查方法

java 复制代码
@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    void testInsert() {
        User user = new User();
        user.setId(5);
        user.setAccount(2000);
        userMapper.insert(user);
    }

    @Test
    void testSelectById() {
        User user = userMapper.selectById(5);
        System.out.println(user);
    }

    @Test
    void testUpdateById() {
        User user = new User();
        user.setId(5);
        user.setAccount(8000);
        userMapper.updateById(user);
    }

    @Test
    void testDeleteById() {
        userMapper.deleteById(5);
    }

}

二、MyBatisPlus常用注解

MyBatisPlus:通过扫描实体类,并基于反射获取实体类信息作为数据库表信息。

  • 类名驼峰转下划线作为表名
  • 名为id的字段作为主键
  • 变量名驼峰转下划线作为表的字段名

2.1 MyBatisPlus常用注解如下

  • @TableName:用来指定表名
  • @TableId:用来指定表中的主键字段信息
  • @TableFiled:用来指定表中的普通字段信息

MyBatisPlus官网:https://www.baomidou.com/pages/223848/#tablename






三、MyBatisPlus常用配置

MyBatisPlus中的配置大都是默认配置好的,我们使用的时候基本不用修改大量的配置,除非遇到特殊的情况需要设置一些配置,可以参考MyBatisPlus的官方文档进行修改。

yaml 复制代码
mybatis-plus:
  type-aliases-package: com.demo.po
  mapper-locations: classpath*:mapper/**/*.xml  # 默认
  global-config:
    db-config:
      id-type: auto # id类型自增长




相关推荐
从心归零6 分钟前
sshj使用代理连接服务器
java·服务器·sshj
一个诺诺前行的后端程序员39 分钟前
springcloud微服务实战<1>
spring·spring cloud·微服务
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Ylucius2 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe2 小时前
分布式系统实战经验
java·分布式
是梦终空2 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss2 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸2 小时前
flink doris批量sink
java·前端·flink
Monodye3 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
一丝晨光3 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符