• SpringBoot 整合MybatisPlus


    1、加载依赖: MybatisPlus的依赖将mybatis、mybatis-spring的依赖去除

    <!-- mybatisPlus的相关依赖,   不需要加载 mybatis-spring-boot-starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>

    2、修改Mapper接口

    @Mapper
    public interface EmployeeMapper extends BaseMapper<Employee>{
        //根据id查询
        public Employee  queryById(int empno) throws Exception;
        //查询指定页数据
        public List<Employee>  queryByPage(@Param("start")int start, @Param("pageSize") int pageSize) throws Exception;
        // 添加员工
        public void add(Employee emp) throws Exception;
    }

    3、修改springBoot对mybatis的配置

    #Mybatisplus的配置
    mybatis-plus:
      type-aliases-package: com.zl.app.entity //给类定义别名
      mapper-locations: classpath*:mapper/*Mapper.xml //mapper扫描器

    4、编写一个MybatisPlus的配置类, 配置一个分页插件

    @Configuration
    //@EnableTransactionManagement
    public class MybatisPlusConfig {
        
        //配置一个分页插件
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    }

    5、 如果数据库的表名与类名不一样,需要使用@TableName注解

    @Data
    @TableName("emp") //如果数据库表名与类名不一致
    public class Employee {
        @TableId //如果数据库id与属性不一样
        private Integer empno;
        @TableFieId //如果数据库其他字段与属性不一样
        private String ename;
        private String job;
        private Integer mgr;
        private Date hiredate;
        private Double comm;
        private Double sal;
        private Integer deptno;
    }

    测试分页:

    @Override
    public Page<Employee> queryByPage2(int pageIndex, int pageSize) throws Exception {
        //创建Page对象
        Page<Employee> page = new Page<>(pageIndex,pageSize);
        empMapper.selectPage(page, null);
        return page;
    }
    @Test
    public void test7()  throws Exception{
        Page<Employee> page = empService.queryByPage2(1, 3);
        System.out.println(page.getRecords());
        System.out.println("总页数:"+page.getPages());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("当前页:"+page.getCurrent());
        System.out.println("页容量:"+page.getSize());
    }
  • 相关阅读:
    封装和参数调用(格式修改)
    今天休息
    2018.1.9内部类
    2018.1.8转型
    环境变量
    环境变量
    计算机的高级语言
    常用的设计模式
    常用的设计模式
    【python3】中 elif 的使用
  • 原文地址:https://www.cnblogs.com/64Byte/p/13261878.html
Copyright © 2020-2023  润新知