• Mybatis-Plus3.x的使用


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


    一、引入

    创建步骤:

    1.创建Spring Boot工程

    2.添加依赖

    引入 Spring Boot Starter 父工程:

    1. <parent>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-parentartifactId>
    4. <version>2.1.7.RELEASEversion>
    5. <relativePath/>
    6. parent>

    引入相关其他依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.mybatis.spring.bootgroupId>
    7. <artifactId>mybatis-spring-boot-starterartifactId>
    8. <version>2.1.3version>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-testartifactId>
    13. dependency>
    14. <dependency>
    15. <groupId>org.projectlombokgroupId>
    16. <artifactId>lombokartifactId>
    17. <optional>trueoptional>
    18. dependency>

    mybatis-plus相关依赖

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>mybatis-plus-boot-starterartifactId>
    4. <version>3.4.0version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-jdbcartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>mysqlgroupId>
    12. <artifactId>mysql-connector-javaartifactId>
    13. dependency>

    配置application.yml:

    添加mysql相关配置

    1. mybatis-plus:
    2. type-aliases-package: com.hz.entity #类型别名所在的包
    3. #控制台打印sql语句
    4. configuration:
    5. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    6. map-underscore-to-camel-case: false #驼峰映射
    7. global-config:
    8. db-config:
    9. logic-delete-field: delFlag #全局逻辑删除字段值 3.3.0开始支持,详情看下面。
    10. logic-delete-value: 1 #逻辑已删除值(默认为 1)
    11. logic-not-delete-value: 0 #逻辑未删除值(默认为 0)
    12. #数据库链接
    13. spring:
    14. datasource:
    15. url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    16. username: root
    17. driver-class-name: com.mysql.cj.jdbc.Driver
    18. password: 123456
    19. #静态资源
    20. resources:
    21. static-locations: classpath:/templates,classpath:/static/

    在 Spring Boot 启动类中添加 @MapperScan 注解

    编码:

    mapper类中继承Basemapper

    测试: 

    注意:

    1.数据库字段若为驼峰命名,则需要开启

    mybatis-plus:configuration:map-underscoreto-camel-case: false #驼峰映射

    2. UserMapper 中的 selectList() 方法的参数为 MP 内置的条件封装器 Wrapper ,所以 不填写就是无任何条件

    3.若需要自定义DAO接口,则需要在yml中读取mapper文件,

    mybatis-plus:mapperlocations: classpath:mappers/*.xml

    二、通用crud接口

            CRUD是指在做计算处理时的增加(Create)、检索(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。CRUD主要被用在描述软件系统中数据库或者持久层的基本操作功能。 

    service层

            如 UserService 用户业务接口有自定义的方法,可创建 UserService 并继承 IService ,接口实现类 UserServiceImpl 可继承 ServiceImpl, T> ,实现 UserService 接口并 实现接口定义方法。具体代码如下:

    继承 IService 接口


    四、分页插件:

    1.分页插件

    配置插件

    1. //Spring boot方式
    2. @EnableTransactionManagement
    3. @Configuration
    4. public class MybatisPlusConfig {
    5. /**
    6. * 分页插件定义
    7. * @return
    8. */
    9. @Bean
    10. public PaginationInterceptor paginationInterceptor() {
    11. PaginationInterceptor paginationInterceptor = new
    12. PaginationInterceptor();
    13. // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
    14. // paginationInterceptor.setOverflow(false);
    15. // 设置最大单页限制数量,默认 500 条,-1 不受限制
    16. // paginationInterceptor.setLimit(500);
    17. return paginationInterceptor;
    18. }
    19. }

    使用分页查询

    1. @Test
    2. public void testPageList() {
    3. Page page = new Page(1, 2);
    4. userMapper.selectPage(page, null);
    5. // 输出page对象分页查询信息
    6. System.out.println("总条数:" + page.getTotal());
    7. System.out.println("每页显示条数:" + page.getSize());
    8. System.out.println("总页数:" + page.getPages());
    9. System.out.println("当前页:" + page.getCurrent());
    10. System.out.println("是否有上一页:" + page.hasPrevious());
    11. System.out.println("是否有下一页:" + page.hasNext());
    12. System.out.println("查询结果:" + page.getRecords());
    13. }

    五、逻辑删除

    1. application.yml 加入配置(如果你的默认值和mp默认的一样,该配置可无):

    1. #以下为mybatis-plus配置
    2. mybatis-plus:
    3. global-config:
    4. db-config:
    5. logic-delete-field: flag #全局逻辑删除字段值 3.3.0开始支持,详情看下面。
    6. logic-delete-value: 1 # 逻辑已删除值(默认为 1)
    7. logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

    3. 在数据库中加入逻辑删除的字段,并且对应实体类中,该映射字段需要加入注解 @TableLogic

    1. @Data
    2. @TableName(value = "user") // 对应数据库表名
    3. public class User {
    4. ......省略
    5. /**
    6. * 逻辑删除字段
    7. */
    8. @TableField(value="delete_flag")
    9. @TableLogic
    10. private Integer deleteFlag;
    11. }

  • 相关阅读:
    spark支持深度学习批量推理
    Word控件Spire.Doc 【文本】教程(2) ;在 C#、VB.NET 中从 Word 文档中提取文本
    使用mod_rewrite时常用的服务器变量: RewriteRule规则表达式的说明:
    1.mysql安装及基础
    互联网Java工程师面试题·Spring篇·第二弹
    什么是内存泄漏?如何避免?
    从零玩转之JPOM自动化部署本地构建 + SSH 发布 java 项目
    Linux shell脚本编程
    【论文阅读】基于隐蔽带宽的汽车控制网络鲁棒认证(一)
    8. Java面向对象编程(三)
  • 原文地址:https://blog.csdn.net/qq_44114187/article/details/133773217