mybatis-plus

1.MyBatis-Plus

mybatis-plus(opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。 但是对于联表操作还必须使用mybatis.

2.mybatis-plus-特性

强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错

支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库

内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

3.如何使用

 

3.1. 创建一个springboot工程并加入相关的依赖
  <!--①引入相关的依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

3.2. 配置文件

#连接数据库
 

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?serverTimezone=Asia/Shanghai
spring.datasource.password=root
spring.datasource.username=root

#打印日志
logging.level.com.ykq.mybatisplus.dao=debug
```

3. 实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TabDept {
    //主键自增
    @TableId(type = IdType.AUTO)
    private Integer id;
    @TableField(value = "dept_name")
    private String deptname;
    @TableField(value = "dept_location")
    private String deptlocation;
    //逻辑删除
    @TableLogic
    private Integer deleted;
    //在添加时自动填充时间
    @TableField(fill = FieldFill.INSERT )
    private Date createTime;
    //在添和修改时加时自动填充时间
    @TableField(fill = FieldFill.INSERT_UPDATE )
    private Date updateTime;
    //构造

    public TabDept(Integer id, String deptname, String deptlocation, Integer deleted) {
        this.id = id;
        this.deptname = deptname;
        this.deptlocation = deptlocation;
        this.deleted = deleted;
    }
}

4. 接口mapper

③ 创建一个接口并继承BaseMapper
public interface UserMapper extends BaseMapper<TabDept> {
}

```

5. 在主启动类上mapper的扫描
package com.ykq.mybatisplus;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = {"com.ykq.mybatisplus.mapper"})
public class MybatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }
 

6. 测试


@SpringBootTest
class MaybaitesPiusApplicationTests {
    @Resource
    private TestMapper testMapper;
    @Test
    void contextLoads() {
        TabDept tabDept = testMapper.selectById(1);
        System.out.println(tabDept);
    }
    @Test
    void insert(){
        TabDept tabDept=new TabDept(null,"林妹1","在此11",0);
        int i= testMapper.insert(tabDept);
        System.out.println(i);
    }
    //修改
    @Test
    void update(){
        TabDept tabDept=new TabDept(5,"小乔妹妹","在此11",0);
        int i= testMapper.updateById(tabDept);
        System.out.println(i);
    }
    //逻辑删除
    @Test
    void deleted(){
        int i= testMapper.deleteById(6);
        System.out.println(i);
    }
    //查所有
    @Test
    void selectAll(){
        List<TabDept> tabDept=testMapper.selectList(null);
        System.out.println(tabDept);
    }
    //条件查询
    @Test
    void conditionSelect(){
        // Wrapper: 条件的包装类。-QueryWrapper  UpdateWrapper
        // LambdaQueryWrapper LambdaUpdateWrapper
        QueryWrapper<TabDept> wrapper=new QueryWrapper<>();
        //条件
        wrapper.between("id",2,5);
        wrapper.or();
        wrapper.like("dept_location","在");
        wrapper.orderByDesc("id");
        //
        List<TabDept> tabDepts=testMapper.selectList(wrapper);
        System.out.println(tabDepts);
    }
    //分页查询, 而且必须配置分页插件
    @Test
    void pageSelect(){
        /**
         * Page:当前页码  每页显示的条数
         */
        Page<TabDept> page=new Page<>(2,2);
        //分页查询
        Page<TabDept> selectPage=testMapper.selectPage(page,null);
        //
        System.out.println("当前页码"+selectPage.getPages());
        //
        System.out.println("总条数"+selectPage.getTotal());
        //
        System.out.println("查询结果"+selectPage.getRecords());
    }

————————————————
版权声明:本文为CSDN博主「爱上java的小白」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41851195/article/details/106372875

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值