Mybatis Plus:Mybatis框架的Plus版

目录

Mybatis-Plus的介绍:

新手入门

通用Service的CURD

内置的分页插件

条件构造器 Wrapper

全局ID生成策略

逻辑删除

数据安全保护

 乐观锁

附图(资料)

相关注解

 条件构造器的相关方法


Mybatis-Plus的介绍:

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

Mybatis-Plus官网:MyBatis-Plus (baomidou.com)icon-default.png?t=M3C8https://baomidou.com/

特点:

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 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 操作智能分析阻断,也可自定义拦截规则,预防误操作

新手入门

步骤:

导入mybatis-plus-boot-starter依赖

       <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

编写pojo的类名要与对应的数据表名称相同,如果不相同可以使用注解@TableName

编写dao层,接口继承BaseMapper<T> T-pojo类

@Repository
public interface UserMapper extends BaseMapper<user> {
}

进行测试:获取mapper接口对象,直接调用CRUD方法

注意:

  • 这些方法执行SQL的方式均是预编译;
  • 进行添加后可以立即返回id值。
  • pojo类中:设置主键自增的注解是@TableId(value = "id",type = IdType.AUTO)
   @Test
    void contextLoads() {
//        增加 id自增  0是初始值 不会添加进去
        user test001 = new user(0, "test001", "123");
        userMapper.insert(test001);
//         mybatis-plus 的特性:可以立即返回id值
        System.out.println(test001.getId());
//        删除
        userMapper.deleteById(14);
//        修改
        user tony = new user(10, "tony", "123456");
        userMapper.updateById(tony);
//        查询
        System.out.println(userMapper.selectByMap(null));
    }

通用Service的CURD

步骤:

建立接口继承IService<T>接口

public interface userService extends IService<user> {
}

实现类实现刚刚创建的接口、继承ServiceImpl<S,T>,S:dao层mapper类名,T:pojo类名

@Service
public class userServiceImpl extends ServiceImpl<UserMapper, user>implements userService {
}

获取实现类对象调用CRUD方法进行测试

 @Test
    void test_2(){
//        增加
        user test002 = new user(0, "test003", "123");
        userService.save(test002);
        System.out.println(test002.getId());
//        删除
        userService.removeById(11);
//        保存/修改
        user test003 = new user(17, "test005", "123");
        userService.saveOrUpdate(test003);
        System.out.println(test003.getId());
//        查询
        System.out.println(userService.getById(17));
    }

内置的分页插件

在springboot的配置类中添加bean对象

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

对单表进行分页查询:底层会自动加上limit关键字

 @Test
    void test_3(){
//        参数: 页码、每页的条数
        Page<user> userPage = new Page<>(1, 3);
        Page<user> page = userService.page(userPage);
        System.out.println(page.getRecords());
    }

条件构造器 Wrapper

具体的实现类有QueryWrapper、UpdateWrapper (查询、更新)

 @Test
    void test_4(){
//        查询 id>17的,只查询username这个属性
        QueryWrapper<user> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper
                .select("username")
                .gt("id",17);
        System.out.println(userService.list(userQueryWrapper));
        System.out.println("=================================");
//        查询id>17 || id<10
        QueryWrapper<user> userQueryWrapper1 = new QueryWrapper<>();
        userQueryWrapper1
                .gt("id",17)
                .or()
                .lt("id",10);
        System.out.println(userService.list(userQueryWrapper1));
        System.out.println("=====================================");
//        查询id>10 && id<17 &&不需要and方法
        QueryWrapper<user> userQueryWrapper2 = new QueryWrapper<>();
        userQueryWrapper2
                .gt("id",10)
                .lt("id",17);
        System.out.println(userService.list(userQueryWrapper2));
    }

使用lambda方法配置条件构造器:优势所有的名称没有写死!

 @Test
    void test_5(){
        UpdateWrapper<user> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .set(user::getUsername,"test006")
                .eq(user::getId,17);
        userService.update(wrapper);
    }

全局ID生成策略

不用每个pojo的主键都添加注解 在配置文件设置即可

mybatis-plus.global-config.db-config.id-type=auto

逻辑删除

逻辑删除:不同于物理删除,物理删除是直接将数据删除掉。逻辑删除是在数据表中设定标志位,通过判断标志位的数据,来确定是否已经删除。

逻辑删除:实质进行的是修改。执行查询的时候,也不会显示已经被“逻辑删除”的数据。

步骤:

在数据表中添加标志位,类似于字段enabled,整型(0,1进行判断)

局部配置:在pojo类上,添加注解@TableLogic:正常-0 删除-1

  @TableLogic(value ="0" ,delval = "1")
    private Integer enabled;

全局配置:不用每个pojo的主键都添加注解 在配置文件设置即可

mybatis-plus.global-config.db-config.logic-delete-field=enabled
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

数据安全保护

方法:对url、username、password进行加密

步骤:

得到密钥

AES.generateRandomKey()

使用密钥进行加密

  System.out.println(AES.encrypt("url", "1648c388ed87847b"));
        System.out.println(AES.encrypt("username", "1648c388ed87847b"));
        System.out.println(AES.encrypt("password", "1648c388ed87847b"));

将加密后的结果,复制粘贴到配置文件中。

将项目进行打包,添加打包插件

<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
        </plugins>

部署项目:java ‐jar xxxx.jar ‐‐mpw.key=你的16位随机秘钥

 乐观锁

 步骤:

数据表添加版本字段version

 在pojo类的属性上添加注解@Version @TableField()-自动填充策略

 @Version
    @TableField(fill = FieldFill.INSERT)//自动填充策略:插入、更新是自动填充
    private Integer version;

在springboot的配置类中添加OptimisticLockerInnerInterceptor

 @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//乐观锁插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

进行测试:

 @Test
    void test_9(){
//        线程1
        user user1 = userService.getById(7);
//        线程2
        user user2=userService.getById(7);
//        线程1 做出修改
        user1.setUsername("thread1");
       if (userService.updateById(user1)){
           System.out.println("线程1 更改成功");
       }
//       线程2 做出修改
        if (!userService.updateById(user2)){
            System.out.println("线程2 更改失败");
        }
    }

附图(资料)

相关注解

 

 条件构造器的相关方法

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值