Mybatis Plus (特性、快速入门、日志、CRUD)

打印 上一主题 下一主题

主题 519|帖子 519|积分 1557

Mybatis plus

可以节省很多的工作,所有的CRUD
JPA  yk-mapper Mybatis plus
偷懒的
简介:
MyBatis-Plus(opens new window)(简称 MP)是一个 MyBatis(opens new window) 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
愿景
我们的愿景是成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
特性


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

使用第三方组件
1.导入第三方依赖
2.研究依赖的配置
3.代码如何编写
4.提升扩展技术的能力
具体流程:
1.建表-建立对应的springboot工程,引入对应的包和依赖。
  1. ---真实开发中的
  2. version乐观锁  
  3. deleted逻辑删除
  4. gmt_create\get_modief
复制代码
导入依赖:
  1. <dependency>
  2.             <groupId>com.baomidou</groupId>
  3.             <artifactId>mybatis-plus-boot-starter</artifactId>
  4.             <version>3.4.2</version>
  5.         </dependency>
复制代码
主要安装的mysql8
配置时区配置:
  1. url=jdbc:mysql://localhost:3306/mybatis?useSSL=false$useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2BB
复制代码

  • 传统的:2.实现pojo-dao(连接mybatis,配置papper.xml文件)-service-controller
  • 使用了mybatis之后:

    • pojo
    • mapper接口
    • 使用

  1. package com.ithema.reggie.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.ithema.reggie.entity.Employee;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface EmployeeMapper extends BaseMapper<Employee> {
  7. }
复制代码
扫描mapper文件夹:
  1. @MapperScan("com.ithema.reggie.mapper")
复制代码
例子:
1.查询全部用户:
  1. package com.ithema.reggie;
  2. import com.ithema.reggie.entity.Employee;
  3. import com.ithema.reggie.mapper.EmployeeMapper;
  4. import com.ithema.reggie.service.EmployeeService;
  5. import net.minidev.json.JSONUtil;
  6. import org.junit.jupiter.api.Test;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import java.util.List;
  10. @SpringBootTest
  11. class ReggieApplicationTests {
  12.     @Autowired
  13.     private EmployeeMapper mapper;
  14.     @Test
  15.     void contextLoads() {
  16.         //参数是一个条件构造器
  17.         //查询全部用户
  18.         List<Employee> employees = mapper.selectList(null);
  19.         employees.forEach(System.out::print);
  20.     }
  21. }
复制代码
配置日志

我们所有的sql是不可见的,我们希望它是可见的,开发时需要,上线关闭即可。
1.配置日志:
  1. mybatis-plus:
  2.   configuration:
  3.     #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
  4.     map-underscore-to-camel-case: true
  5.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码
配置完毕日志之后,后面的学习就能自动的生成sql语句,你会喜欢上mybatis plus
CRUD扩展

insert

  1. @Test
  2. public void insert(){
  3.     User us = new User();
  4.     us.setName("郝泾钊");
  5.     us.setAge("8");
  6.     mapper.insert(user)
  7. }
复制代码
发现没有插入id会自动插入id
数据库的id为全局的唯一的默认值
主键的生成策略:
  1. 对应的数据库中的主键(uuid、自增id、雪花算法、redis、zookeper!)
复制代码
更新方法

  1. @Test
  2. public void update(){
  3.     User us = new User();
  4.     //通过条件自动拼接动态sql
  5.     us.setId("6L")
  6.     us.setName("郝泾钊");
  7.     us.setAge("8");
  8.     //注意:updateById但是参数是对象
  9.     mapper.updateById(user)
  10. }
复制代码
所有的sql都是自动帮你配置的。
查询操作

  1. @Test
  2. public void selectById(){
  3. User us = userMapper.selectById(1L);
  4.    
  5. }
复制代码
//测试批量查询
  1. @Test
  2. public void selectByBatchId(){
  3. List<User> us = userMapper.selectBatchIds(Arrays.asList(1,2,3));
  4. us.forEach(System.out::println);
  5.    
  6. }
复制代码
//测试条件区的查询用map操作
  1. @Test
  2. public void selectById(){
  3. HashMap<String,Object> map = new Hash<>();
  4. //自定义查询的条件
  5. map.put("name","郝泾钊");
  6.     List<User> us = userMapper.selectByMap(map);
  7.     us.forEach(System.out::println);
  8. }
复制代码
分页查询

1.原始的limit分页
2.page
拦截器:
1.配置拦截器组件
  1. @Bean
  2. public MybatisPlusInterceptor mybatisPlusInterceptor(){
  3.     MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
  4.     mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
  5.     return mybatisPlusInterceptor;
  6. }
复制代码
2.内置了一个page对象
  1. //测试分页查询  第一页,5个数据
  2. public void testPage(){
  3. //参数一:当前页
  4. //参数二:页面的大小
  5. //使用了分页之后,分页变得简单
  6. Page<User> page = new Page <>(1,5);
  7.     userMapper.selectPage(page,null);
  8.     page.getRecords().forEach(System.out::println);
  9. }
复制代码
删除操作

  1. //测试分页查询  第一页,5个数据
  2. public void testDelete(){
  3. User us = userMapper.deleteById(1L);
  4. }
复制代码
  1. //测试分页查询  第一页,5个数据
  2. public void testDelete(){
  3. List<User> us = userMapper.deleteBatchIds(Arrays.asList(1,2,3));
  4. }
复制代码
  1. @Test
  2. public void selectById(){
  3. HashMap<String,Object> map = new Hash<>();
  4. //自定义查询的条件
  5. map.put("name","郝泾钊");
  6.     List<User> us = userMapper.deleteByMap(map);
  7.     us.forEach(System.out::println);
  8. }
复制代码
逻辑删除
逻辑删除:在数据库中没有移除  :通过 deleted =0 =>delete=1

物理删除:在数据库中移除了

管理员可以参看被删除的记录,防止数据的丢失,类似于回收站
1.在数据库中增加一个deleted字段
2.实体类添加一个deleted属性
  1. @Tablelogid//逻辑删除注解
复制代码
3.配置bean组机
  1. mybatis-plus:
  2.   global-config:
  3.     db-config:
  4.       logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
  5.       logic-delete-value: 1 # 逻辑已删除值(默认为 1)
  6.       logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
复制代码
  1. @Bean
  2. public ISqlInjector sqlInjector(){
  3.     return new logicSqlInjector();
  4. }
复制代码
走的是逻辑删除,但是是更新操作
查询的时候会自动过滤逻辑删除的项目--管理员看的很清楚

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

前进之路

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表