zoukankan      html  css  js  c++  java
  • 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());
    }
  • 相关阅读:
    centos7.6 使用yum安装mysql5.7
    解决hadoop本地库问题
    docker-compose 启动警告
    docker 安装zabbix5.0 界面乱码问题解决
    docker 部署zabbix问题
    zookeeper 超时问题
    hbase regionserver异常宕机
    (转载)hadoop 滚动升级
    hadoop Requested data length 86483783 is longer than maximum configured RPC length
    zkfc 异常退出问题,报错Received stat error from Zookeeper. code:CONNECTIONLOSS
  • 原文地址:https://www.cnblogs.com/64Byte/p/13261878.html
Copyright © 2011-2022 走看看