码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • MybatisPlus实践积累


    MybatisPlus实践积累

    文章目录

    • MybatisPlus实践积累
      • 1.MybatisPlus基础用法(SpringBoot项目中)
        • 新建Mapper软件包
      • 2.在Mapper中添加自定义方法(单表)
      • 3.关于MybatisPlus的批量插入
        • 写一个工具类
        • 写一个配置类
        • 到Mappe中添加这个方法
      • 4.关于MybatisPlus中的分页
        • 在MybatisPlus的配置类中添加Bean

    1.MybatisPlus基础用法(SpringBoot项目中)

    新建Mapper软件包

    1. 创建Mapper接口,同时实现BaseMapper类,泛型为对应实体类

    2. 添加@Mapper注解

    3. 在主启动类上添加@MapperScan注解并添加扫描路径。例:@MapperScan("com.airweb.mapper")

    4. //Mapper接口
      @Mapper
      public interface AdminMapper extends BaseMapper<Admin> {
      }
      
      //主启动类
      @MapperScan("com.airweb.mapper")
      @MapperScan("com.airweb.listen")
      @SpringBootApplication
      public class AirWebApplication {
          public static void main(String[] args) {
              SpringApplication.run(AirWebApplication.class, args);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    5. 到这一步,就可以使用MP中的一些基本的增删改查方法了

    2.在Mapper中添加自定义方法(单表)

    只需要在Mapper接口中添加方法,并使用@Select、@Insert、@Delete、@Update等注解写入SQL语句

    
    /**根据Id查找管理员信息中的指定字段BuildingId
    */
    @Select("select building_id from admin where admin_id=${id}")
    int selectBuildingById(@Param("id")Long adminId);
    
    //其他的增删改也一样,只是注解不同
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3.关于MybatisPlus的批量插入

    MybatisPlus其实也有提供一个批量插入的方法,但是那个方式本质上也是多次的调用insert(),所以不用,我们用下面这个insertBatchSomeColumn来实现真正的批量插入

    关于 insertBatchSomeColumn(List list) 这个方法,大家可以直接复制下面的代码就可以了

    写一个工具类

    public class InsertBatchSqlInjector extends DefaultSqlInjector {
        @Override
        public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
            List<AbstractMethod> methodList = super.getMethodList(mapperClass);
            methodList.add(new InsertBatchSomeColumn());
            return methodList;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    写一个配置类

    @Component
    public class MybatisPlusConfig {
    
        @Bean
        public InsertBatchSqlInjector easySqlInjector () {
            return new InsertBatchSqlInjector();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    到Mappe中添加这个方法

    @Mapper
    public interface ClassroomMapper extends BaseMapper<Classroom> {
    
        /**
         * 批量插入
         * @param classrooms
         */
        void insertBatchSomeColumn(@Param("list") List<Classroom> classrooms);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.关于MybatisPlus中的分页

    在MybatisPlus的配置类中添加Bean

    @Configuration
    public class MybatisPlusConfig {
        /**
         * 批量插入所需要的Bean
         * @return
         */
        @Bean
        public InsertBatchSqlInjector easySqlInjector () {
            return new InsertBatchSqlInjector();
        }
        
        /**
         * 分页插件所需要的Bean
         */
        //分页插件
        @Bean
        public MybatisPlusInterceptor paginationInnerInterceptor(){
            MybatisPlusInterceptor paginationInnerInterceptor = new MybatisPlusInterceptor();
            paginationInnerInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            return paginationInnerInterceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    然后就可以开始使用了,下面是具体操作代码:

    //具体操作
    //1、创建IPage对象---pageNum为第几页,pageSize为每页几条信息
    IPage<Admin> page = new Page<>(pageNum,pageSize);
    //调用Mapper接口中的方法
    List<Admin> allAdmin = adminMapper.getAllAdmin(page);
    
    
    //adminMapper接口中的getAllAdmin(page)方法
    @Select("select * from admin")
    List<Admin> getAllAdmin(IPage<Admin> page);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    随机更新
    更加详细的信息可以查看MybatisPlus的官网:MybatisPlus,这个还是挺好看懂的!冲!

  • 相关阅读:
    跨浏览器测试需要进行的测试与评估
    Android学习笔记 62. 为TextView设置样式
    短视频素材哪里有?8个视频素材免费下载素材库无水印
    高防服务器如何抵御大规模攻击
    3D视觉系统实现自动化上下料操作
    【智能优化算法-战争策略算法】基于战争策略算法求解单目标优化问题附matlab代码
    Java - NPE(NullPointerException);Optional
    基于Java实现的GRE(美国研究生入学考试)学习系统
    什么是服务器集群?海外服务器集群的优势?
    腾讯云16核服务器配置有哪些?CPU型号处理器主频性能
  • 原文地址:https://blog.csdn.net/weixin_63649357/article/details/132740648
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号