栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MybatisPlus常用知识点总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MybatisPlus常用知识点总结

依赖:


 mysql
 mysql-connectorjava
 


 
 
 com.baomidou
 mybatis-plus-bootstarter
 3.4.1
 

配置:

server.port=8884
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=truecharacterEncoding=utf-8&useSSL=false
spring.datasource.username =admin
spring.datasource.password =123456

启动类配置扫描路径:@MapperScan("com.miao.test.mapper")

PO实体类添加: @TableName("student")

mapper :

public interface StudentMapper extends baseMapper {
}

QueryWrapper   查询包装类,可以封装多数查询条件

如: List list =studentMapper .selectList(newQueryWrapper());

多案例API查询
        selectById
        selectBatchIds
        selectOne
        selectCount
        selectList

 Mybatis plus 常⽤注解

@TableName ⽤于定义表名

@TableId ⽤于定义表的主键

        属性:     value ⽤于定义主键字段名
                        type ⽤于定义主键类型

                       IdType.AUTO 主键⾃增,系统分配,不需要⼿动输⼊
                        IdType.NONE 未设置主键
                        IdType.INPUT 需要⾃⼰输⼊ 主键值
                        IdType.ASSIGN_ID 系统分配 ID,⽤于数值型数据(Long,对应 mysql 中 BIGINT 类型)
                        IdType.ASSIGN_UUID 系统分配 UUID,⽤于字符串型数据(String,对应 mysql 中 varchar(32) 类型)

@TableField ⽤于定义表的⾮主键字段

属性:value ⽤于定义⾮主键字段名,⽤于别名匹配,假如java对象属性和数据库属性不⼀样
          exist ⽤于指明是否为数据表的字段, true 表示是,false 为不是,假如某个java属性在数据库没对应的字段则要标记为false
          fill ⽤于指定字段填充策略(FieldFill,⽤的不多)
         字段填充策略:⼀般⽤于填充 创建时间、修改时间等字段
         FieldFill.DEFAULT 默认不填充
         FieldFill.INSERT 插⼊时填充
         FieldFill.UPDATE 更新时填充
         FieldFill.INSERT_UPDATE 插⼊、更新时填充。

删除操作

int rows = studentMapper .deleteById(4);

int rows = studentMapper .deleteByMap(Map);

更新操作

studentMapper .update(StudentDO,newQueryWrapper().eq("id","2"));

UpdateWrapper updateWrapper = newUpdateWrapper();
updateWrapper.set("img","uuuu");
updateWrapper.eq("id",1);
studentMapper .update(null,updateWrapper);

 QueryWrapper模糊查询

eq 等于
ne 不等于
gt ⼤于
ge ⼤于等于
lt ⼩于
le ⼩于等于
or 拼接or
between 两个值中间
notBetween 不在两个值中间

like 模糊匹配
notLike 不像

likeLeft 左匹配
likeRight 右边匹配
isNull 字段为空
in in查询
groupBy 分组
orderByAsc 升序
orderByDesc 降序
having   having查询

如:

List list = studentMapper.selectList(new QueryWrapper().eq("id", "11").or().ne("age", 13));

分页插件配置类

@Configuration
public class MybatisPlusPageConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

分页使用:

QueryWrapper wrapper = newQueryWrapper<>();
 wrapper.eq("age",4);
 //第1⻚,每⻚10条
 Page page = new Page<>(1, 10);
 IPage iPage=studentMapper.selectPage(page, wrapper);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324672.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号