栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-plus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-plus

添加依赖

 


    com.baomidou
    mybatis-plus-boot-starter
    3.4.2

另:全部依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        
        2.1.3.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
        
            mysql
            mysql-connector-java
            runtime
        

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.3.0
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.2
        
        
            org.junit.jupiter
            junit-jupiter
            RELEASE
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


调整配置

去掉mybatis配置

添加mybatis plus配置

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  type-aliases-package: com.qcby.train.entity
  mapper-locations: classpath:mapper/*.xml

  # 全局配置id自增  =>
  global-config:
    db-config:
      id-type: auto

Mybatis-plus继承了分页 

设置表名

默认表名就是实体类的类名

在实体类的类名上加上@TableName注解进行表示

主键生成策略

默认MP插入数据时,在没有设置主键生成策略的情况下的策略是基于雪花算法的自增id

如果需要别的策略,在实体类中代表主键的字段上加上@TableId注解,使用其type属性指定主键生成策略

@TableId(type=IdType.AUTO) 自动增长

或在全局设置中设置

id-type:AUTO

驼峰映射

MP默认开启字段名列名的驼峰映射

userName(实体类中)——》user_name(数据库中)


自定义映射

在实体类的属性上面加@TableField("address")


如果需要关闭可以使用如下配置进行关闭

mybatis-plus:
  configuration:
	#是否开启自动驼峰命名规则(camel case)映射,即从经典数据库列名 A_COLUMN(下划线命名) 到经典 Java 属性名 aColumn(驼峰命名) 的类似映射
    map-underscore-to-camel-case: false

日志

如果需要打印MP操作对应的SQL语句等,可以配置日志输出。

配置方式如下:

mybatis-plus:
  configuration:
    # 日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

条件构造器Wrapper

在其子类`AbstractWrapper`中提供了很多用于构造Where条件的方法。

QueryWrapper针对select方法,查询哪些列

UpdateWrapper针对set语法的set方法,设置来对哪些列进行更新

         

常用AbstractWrapper方法

 

分页

 

Mapper层

 

Service层 

 

Controller层

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号