当前位置:   article > 正文

MyBatis-Plus使用——配置yml参数 & 常用的注解@Table,@TableId,@IdType,@TableField,CRUD的API接口_mybatis-plus yml配置

mybatis-plus yml配置

在这里插入图片描述

前言

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

本系列博客结合实际应用场景,阐述MyBatis-Plus实际应用中的问题以及使用方法。

本篇博客介绍MyBatis-Plus 的配置,常用的注解,API接口,以及查询的LambdaQueryWrapper的使用方法。

官网:https://baomidou.com/

其他关于MyBatis-Plus的博客文章如下:

MyBatis-Plus多数据源——如何在一个项目中使用多个MySQL数据库

引出


1.MyBatis-Plus 的配置;
2.常用的注解,API接口;
3.查询的LambdaQueryWrapper的使用方法;

mybatisplus的配置

ibatis—>mybatis

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

在这里插入图片描述

数据源配置

基本数据源配置: url、username、password、driver-class-name

spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/bookshop_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      username: root
      password: 1234
      driver-class-name: com.mysql.cj.jdbc.Driver
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

数据源其它配置 inital-size、max-active、min-idle、max-wait等

   datasource:
        # 初始化时建立的物理连接数。初始化发生在显式调用init方法,或者第一次getConnection时.
        initial-size: 5
        # 连接池最大物理连接数量。
        max-active: 50
        # 连接池最小物理连接数量。
        min-idle: 5
        # 获取连接时最大等待时间,单位为毫秒。
        # 配置之后,缺省启用公平锁,并发效率会有所下降,若需要可以通过配置useUnfairLock属性为true使用非公平锁。
        max-wait: 6000
        # 是否缓存preparedStatement,也就是PSCache。
        # PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
        pool-prepared-statements: true
        # 要启用PSCache,其值必须大于0,当大于0时,poolPreparedStatements自动触发修改为true。
        # 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100。
        max-pool-prepared-statement-per-connection-size: 20
        # 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
        # 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
        validation-query: select 1 from dual
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

mybatisplus配置

#配置数据源
spring:
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/book_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      username: root
      password: 123
      driver-class-name: com.mysql.cj.jdbc.Driver
      
#mybatisplus配置
mybatis-plus:
  global-config:
    db-config:
	  #配置id自增长
      id-type: auto
  configuration:
    #配置mybatisplus日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:/mapper/*.mapper.xml      #xml文件的位置(resources下的mapper文件夹)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Mybatis-Plus常用注解

@Table

表名注解,标识实体类对应的表。

属性类型必须指定默认值描述
valueString“”表名:例如 value=”book_tab”
resultMapString“”xml 中 resultMap 的 id(用于满足特定类型的实体类对象绑定)
autoResultMapbooleanfalse是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建与注入)
excludePropertyString[]{}需要排除的属性名 @since 3.3.1

@TableId

主键注解

属性类型必须指定默认值描述
valueString“”主键字段名
typeEnumIdType.NONE指定主键类型

@IdType

public enum IdType {
    AUTO(0),            //数据库自增长,mysql的自增长主键
    NONE(1),            //未设置
    INPUT(2),           //自定义设置
    ASSIGN_ID(3),   //分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),
                      //使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
    ASSIGN_UUID(4); //分配 UUID,主键类型为 String(since 3.3.0),
                        //使用接口IdentifierGenerator的方法nextUUID(默认default 方法)
    private final int key;
    private IdType(int key) {
        this.key = key;
    }
    public int getKey() {
        return this.key;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

@TableField

数据库不建议使用-号作为分隔符,也不能使用驼峰法作为字段名称。

属性类型必须指定默认值描述
valueString“”数据库字段名
updateString“”字段 update set 部分注入,例如:当在version字段上注解update="%s+1" 表示更新时会 set version=version+1 (该属性优先级高于 el 属性)
updateStrategyEnumFieldStrategy.DEFAULT举例:IGNORED update table_a set column=#{columnProperty}
selectbooleantrue是否进行 select 查询

@Version

乐观锁注解、标记 @Version 在字段上

Mybatis-Plus接口API

dao接口继承BaseMapper

在这里插入图片描述

insert

int insert(T entity);
  • 1

delete

// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

update

// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper<T> whereWrapper);
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
  • 1
  • 2
  • 3
  • 4

select

// 根据 ID 查询
T selectById(Serializable id);

// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

Lamda的条件LambdaQueryWrapper

构建LambdaQueryWrapper对象

LambdaQueryWrapper lambda = new LambdaQueryWrapper<>();

常用方法

  • eq 相等
  • in 范围查询
  • like 模糊查询
  • likeRight 右侧通配符查询
  • likeLeft 左侧通配符查询
  • or() 或
  • and() 并且
lambdaQueryWrapper.like(User::getUsername,"zh")
                          .or()
                        .likeLeft(User::getName,"张");
List<User> users = userDao.selectList(lambdaQueryWrapper);
  • 1
  • 2
  • 3
  • 4

总结

1.MyBatis-Plus 的配置;
2.常用的注解,API接口;
3.查询的LambdaQueryWrapper的使用方法;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/525248
推荐阅读
相关标签
  

闽ICP备14008679号