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工程,引入对应的包和依赖。- ---真实开发中的
- version乐观锁
- deleted逻辑删除
- gmt_create\get_modief
复制代码 导入依赖:- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.2</version>
- </dependency>
复制代码 主要安装的mysql8:
配置时区配置:- 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之后:
- package com.ithema.reggie.mapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.ithema.reggie.entity.Employee;
- import org.apache.ibatis.annotations.Mapper;
- @Mapper
- public interface EmployeeMapper extends BaseMapper<Employee> {
- }
复制代码 扫描mapper文件夹:- @MapperScan("com.ithema.reggie.mapper")
复制代码 例子:
1.查询全部用户:- package com.ithema.reggie;
- import com.ithema.reggie.entity.Employee;
- import com.ithema.reggie.mapper.EmployeeMapper;
- import com.ithema.reggie.service.EmployeeService;
- import net.minidev.json.JSONUtil;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import java.util.List;
- @SpringBootTest
- class ReggieApplicationTests {
- @Autowired
- private EmployeeMapper mapper;
- @Test
- void contextLoads() {
- //参数是一个条件构造器
- //查询全部用户
- List<Employee> employees = mapper.selectList(null);
- employees.forEach(System.out::print);
- }
- }
复制代码 配置日志
我们所有的sql是不可见的,我们希望它是可见的,开发时需要,上线关闭即可。
1.配置日志:- mybatis-plus:
- configuration:
- #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
- map-underscore-to-camel-case: true
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
复制代码 配置完毕日志之后,后面的学习就能自动的生成sql语句,你会喜欢上mybatis plus
CRUD扩展
insert
- @Test
- public void insert(){
- User us = new User();
- us.setName("郝泾钊");
- us.setAge("8");
- mapper.insert(user)
- }
复制代码 发现没有插入id会自动插入id
数据库的id为全局的唯一的默认值
主键的生成策略:- 对应的数据库中的主键(uuid、自增id、雪花算法、redis、zookeper!)
复制代码更新方法
- @Test
- public void update(){
- User us = new User();
- //通过条件自动拼接动态sql
- us.setId("6L")
- us.setName("郝泾钊");
- us.setAge("8");
- //注意:updateById但是参数是对象
- mapper.updateById(user)
- }
复制代码 所有的sql都是自动帮你配置的。
查询操作
- @Test
- public void selectById(){
- User us = userMapper.selectById(1L);
-
- }
复制代码 //测试批量查询- @Test
- public void selectByBatchId(){
- List<User> us = userMapper.selectBatchIds(Arrays.asList(1,2,3));
- us.forEach(System.out::println);
-
- }
复制代码 //测试条件区的查询用map操作- @Test
- public void selectById(){
- HashMap<String,Object> map = new Hash<>();
- //自定义查询的条件
- map.put("name","郝泾钊");
- List<User> us = userMapper.selectByMap(map);
- us.forEach(System.out::println);
- }
复制代码分页查询
1.原始的limit分页
2.page
拦截器:
1.配置拦截器组件- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
- mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
- return mybatisPlusInterceptor;
- }
复制代码 2.内置了一个page对象- //测试分页查询 第一页,5个数据
- public void testPage(){
- //参数一:当前页
- //参数二:页面的大小
- //使用了分页之后,分页变得简单
- Page<User> page = new Page <>(1,5);
- userMapper.selectPage(page,null);
- page.getRecords().forEach(System.out::println);
- }
复制代码删除操作
- //测试分页查询 第一页,5个数据
- public void testDelete(){
- User us = userMapper.deleteById(1L);
- }
复制代码- //测试分页查询 第一页,5个数据
- public void testDelete(){
- List<User> us = userMapper.deleteBatchIds(Arrays.asList(1,2,3));
- }
复制代码- @Test
- public void selectById(){
- HashMap<String,Object> map = new Hash<>();
- //自定义查询的条件
- map.put("name","郝泾钊");
- List<User> us = userMapper.deleteByMap(map);
- us.forEach(System.out::println);
- }
复制代码 逻辑删除
逻辑删除:在数据库中没有移除 :通过 deleted =0 =>delete=1
物理删除:在数据库中移除了
管理员可以参看被删除的记录,防止数据的丢失,类似于回收站
1.在数据库中增加一个deleted字段
2.实体类添加一个deleted属性3.配置bean组机- mybatis-plus:
- global-config:
- db-config:
- logic-delete-field: flag # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不配置步骤2)
- logic-delete-value: 1 # 逻辑已删除值(默认为 1)
- logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
复制代码- @Bean
- public ISqlInjector sqlInjector(){
- return new logicSqlInjector();
- }
复制代码 走的是逻辑删除,但是是更新操作
查询的时候会自动过滤逻辑删除的项目--管理员看的很清楚
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |