springboot:mybatis-plus操作、热部署

740 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情

Mybatis-plus 操作

MyBatis 出现 BindingException 错误

  1. 检查 mapper.xml 配置文件是否放在 src 下的 resources 资源包下

    image-20220422214936811

  2. 检查 mapper 接口和 mapper.xml 配置文件名字是否相同

    image-20220422214756116

  3. 检查 mapper.xml 配置文件里的命名空间是否指向正确的位置

    命名空间 = mapper 包路径 + mapper 接口名

    image-20220422215200423

  4. 检查 mapper.xml 配置文件中的 sql 语句是否指向对应得 mapper 接口得方法

    image-20220422215530052

  5. 检查 mybatis-plus 配置

    # mybatis-plus
    mybatis-plus:
      # 指向你的 mapper.xml 配置文件地址
      mapper-locations: classpath:/com/hyz/mapper/*.xml
      # 指向你的 mapper 包
      type-aliases-package: com.hyz.mapper
    
  6. 检查是否在 pom.xml 里添加资源

    <build>
      g  <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!--修改mp依赖错误-->
        <resources>
            <resource>
                <directory>src/main/</directory>
                <!-- 此配置不可缺,否则mybatis的Mapper.xml将会丢失 -->
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--指定资源的位置-->
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

MyBatis-plus 出现 BindingException 错误

  1. 检查测试类的 mapper 对象是否装载正确

    image-20220422230211778

  2. 检查你的 mapper 接口继承得 baseMapper 的泛型是否指向实体类。

    image-20220422230252091

  3. 检查实体类和属性是否对应数据库的表名和字段名

    image-20220422230505953

  4. 如果代码没有问题,那就是环境的问题了,检查你的 mybatis-plus 依赖是否注入正确

    <!--导入依赖-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.0</version>
    </dependency>
    

Mybatis-plus 分页查询

  1. 创建 IPage 对象

    IPage page = new Page(current, size);
    
    • current:当前查看页数
    • size:当前页面展示的记录量
  2. 创建 Mybatis-plus 拦截器

    分页操作是在 Mybaits-plus 的常规操作上增强得到,内部是动态的拼写 SQL 语句,因此需要增强对应的功能,使用 Mybatis-plus 拦截器实现。

    package com.hyz.config;
    
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author workplace
     * @date 2022/4/23 14:07
     */
    @Configuration
    public class MPConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            // 1. 创建拦截器容器
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            // 2. 在容器中放入指定功能的拦截器
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            // 3. 返回拦截器容器
            return interceptor;
        }
    }
    
  3. 通过 Mybatis-plus 调用方法

        @Test
        public void test5() {
            IPage page = new Page(1,5);
            booksMapper.selectPage(page, null);
            System.out.println("最大页码值:"+page.getPages());
            System.out.println("当前页码值:"+page.getCurrent());
            System.out.println("数据总量:"+page.getTotal());
            System.out.println("每页数据总量:"+page.getSize());
            System.out.println("数据:"+page.getRecords());
        }
    

Mybatis-plus 模糊查询

@Test
public void test6() {
    // 1. 创建条件查询器
    QueryWrapper<Books> qw = new QueryWrapper<>();
    // 2. 调用 like 方法,选择字段名和模糊查询的条件
    qw.like( column, val );
    // 3. 使用模糊查询
    mapper接口.selectList(qw);
}
  • 请一定要确定 like 方法里的 column 是正确对应数据库字段名的。写错会报异常

热部署

热部署仅在开发环境下有效!!!热部署仅在开发环境下有效!!!热部署仅在开发环境下有效!!!

什么是热部署

  • 项目修改之后不需要重新启动,可以直接生效

如何启动热部署

  1. 导入开发者依赖

    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    
  2. 激活热部署

    image-20220427213604499

    快捷键:Ctrl + F9


热部署详情

在我们这个整个项目中由大致分成了两类资源,一类是【自定义开发代码,包含类、页面、配置文件等,加载位置 restart 类加载器】,另一类是【jar 包,加载位置 base 类加载器】。

热部署仅仅加载了当前程序员自定义开发的资源,不加载 jar 资源。

  • 重启(Restart):自定义开发代码,包含类、页面、配置文件等,加载位置 restart 类加载器。
  • 重载(ReLoad):jar 包,加载位置 base 类加载器。

一般的运行是包含了重启和重载两个操作,热部署仅仅包含了重启的操作。


idea 启动自动热部署

  1. 引入开发者依赖

  2. 通过 idea 自动热部署

    image-20220427215000394

  3. 1 编译器设置勾选上

    image-20220427221028830

  4. 2 如果是旧版本则需要修改一下注册表

    快捷键:alt+ctrl+shift+/,选择第一个注册表

    image-20220427215547813

    在里面找对应的注册勾选上

    image-20220427215424493

以后就可以设置成热部署了。


修改热部署范围

系统默认不重启范围

image-20220427221526498

自定义热部署不触发范围:

# devtools 热部署不触发范围
devtools:
  restart:
    additional-exclude:

禁用热部署

devtools:
  restart:
    enabled: false

如果我们仅仅只是在配置文件中使用了依赖配置选择关闭热部署,那么别人可能就可以在其他配置文件中将热部署开启。我们如果在遇到这种配置冲突的时候,可以通过设置高优先级属性禁用热部署。

通过在引导类里加入方法,就是设置高优先级属性禁用热部署。

@MapperScan("com.hyz.mapper")

@SpringBootApplication
public class SSMPApplication {
    public static void main(String[] args) {
        System.setProperty("spring.dectool.restart.enabled", "false");
        SpringApplication.run(SSMPApplication.class, args);
    }
}

image-20220427223639899