学MybatisPlus

1.设置MySql的数据库

XML 复制代码
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: MySQL123
logging:
  level:
    com.itheima: debug
  pattern:
    dateformat: HH:mm:ss

logging : level : com.itheima 是来设置日志的输出模式,debug可以看到MySql的语句。

依赖:mybatis-plus-boot-starter

2.在Mapper中使用:需要在mapper接口中中使用extends BaseMapper<实体类>(一般是简单的)

3.常见注解,一般是来实体类

  1. @TableName
  2. @TableField
  3. @TableId (Auto , Input , Assign_id)

4.常见配置

XML 复制代码
mybatis-plus:
  type-aliases-package: com.itheima.mp.domain.po //实体类
  global-config:
    db-config:
      id-type: auto # 全局id类型为自增长

MySql的手写:

XML 复制代码
mybatis-plus:
  mapper-locations: "classpath*:/mapper/**/*.xml" # Mapper.xml文件地址,当前这个是默认值。

5.条件构造器(了解)

6.Service的接口:

使用:在Service的接口中extends IService<实体类>,在ServiceImpl中使用implements Service

extends ServiceImpl<UserMapper,User>.

7.使用lambda,LambdaQuery<实体类>wrapper = new QueryWrapper<User>,可以去指定条件的

for example: .like(username !=null ,User::getUsername,username)

8.批量新增:

修改项目中的application.yml文件,在jdbc的url后面添加参数&rewriteBatchedStatements=true

速度有非常明显的提升,使用了jdbc的预编译模式。

9.代码生成

使用了MybatisPlus的插件

10.**静态工具的使用:**为了避免出现循环依赖问题,向IServiceImpl的使用(Db)

11.逻辑删除:

  • 在表中添加一个字段标记数据是否被删除

  • 当删除数据时把标记置为true

  • 查询时过滤掉标记为true的数据

XML 复制代码
mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: deleted # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

12.通用枚举

java 复制代码
package com.itheima.mp.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;

@Getter
public enum UserStatus {
    NORMAL(1, "正常"),
    FREEZE(2, "冻结")
    ;
    private final int value;
    private final String desc;

    UserStatus(int value, String desc) {
        this.value = value;
        this.desc = desc;
    }
}

使用@EnumValue来去数据库的数据,@JsonValue来去注解标记JSON序列化时展示的字段

12.JSON 类型处理器

使用类处理器:@TableField(typeHandle = JcaksonTypeHandle.class),要去定义一个实体类,

来去处理json的数据。

相关推荐
阿里云云原生10 小时前
AI Agent 上线容易稳定难?阿里云 AgentLoop 推出“经验自进化”闭环治理方案
人工智能·阿里云·mybatis·agentscope
CodeStats1 天前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
Nuanyt1 天前
SSM 学习记录 第二部分 Spring整合Mybatis&Junit AOP核心概念 Spring事务管理 SpringMVC 请求与响应 Rest风格
java·spring·junit·mybatis·restful
米码收割机1 天前
【SSM】Spring MVC_MyBatis SSM商城系统(源码+论文)【独一无二】
spring·mvc·mybatis
Devin~Y3 天前
互联网大厂 Java 面试实录:Spring Boot、MyBatis、Redis、Kafka、Spring Security、RAG 与 MCP 全链路问答
java·redis·kafka·mybatis·spring security·spring mvc·sprint boot
VX_bysjlw9853 天前
基于微信小程序的宠物用品商城系统-后端74346-计算机毕设原创(免费领源码+带部署教程)
java·redis·微信小程序·eclipse·mybatis·idea·微信开发者工具
hexu_blog3 天前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis
乐观的Terry3 天前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
聆听。。花开雨落4 天前
mybatis的typeHandler 作用
数据库·mybatis
天若有情6736 天前
SpringBoot4 + MyBatis 前后端分离实战|从零实现坏习惯管理系统,支持局域网手机访问CRUD
智能手机·mybatis