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());
    }
  • 相关阅读:
    PHP生成xml 无法识别或是无法读取或是浏览器不识别等问题
    关于PHP 采集类
    Centos7 下安装Docke
    Git使用之设置SSH Key
    yii2.0中Rbac 怎么添加超加管理员
    Undefined index: HTTP_RAW_POST_DATA的解决办法
    window下phpstudy的nginx配置虚拟主机
    yii2.0中添加二维数组,多条数据。
    预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445端口
    github与git之间怎么建立连接
  • 原文地址:https://www.cnblogs.com/64Byte/p/13261878.html
Copyright © 2011-2022 走看看