mybatis-puls条件查询、分页功能

mybatis-puls条件查询、分页功能

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

一、代码生成

在test中写,读者需要自行看里面需要填写的内容,里面都是适合我自己内容。

@Test
    void genCode(){
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("D:\\大二上\\SpringBoot\\springboot01_08\\src\\main\\java");
        gc.setAuthor("wp");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setIdType(IdType.AUTO); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(false);//开启Swagger2模式
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/123?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("zjw241014");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("springboot01_08"); //模块名
        pc.setParent("com.iwei");
        pc.setController("controller");
        pc.setEntity("model");
        pc.setService("service");
        pc.setMapper("dao");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("myuser","student");
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        mpg.setStrategy(strategy);

        // 6、执行
        mpg.execute();
    }

二、代码生成后项目结构在这里插入图片描述 三、新建数据库中一个表

学生的数据库是我之前自己创建的,大家练习条件查询和分页功能可以就使用该数据库中的mysuer表。

CREATE TABLE myuser
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);INSERT INTO myuser(id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

四、yaml配置

虽然mybatis-puls帮我们生成了很多代码,但是还是要在yaml里面连接数据库和一些基础配置

server:
  port: 80
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root(用户名)
    password: 密码
  thymeleaf:
    cache: false
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml

五、test启动类中条件查询测试

 @Test
    void eq(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.eq("name","张俊伟");
        studentService.list(wrapper);
    }
    @Test
    void gt(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.gt("age",18);
        studentService.list(wrapper);
    }
    @Test
    void lt(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.lt("age",20);
        studentService.list(wrapper);
    }
    @Test
    void like(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.like("name","张%");
        studentService.list(wrapper);
    }
    @Test
    void and(){
        QueryWrapper<Student> wrapper=new QueryWrapper<>();
        wrapper.lt("id",3).gt("age",18);
        studentService.list(wrapper);
    }

    @Test
    void or(){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.lt("age",18).or().gt("id",8);
        studentService.list(wrapper);
    }

六、分页功能

我们需要配置分页插件
在config包下

@Configuration
public class pageConfig {

    public class MybatisPlusConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
            pageInterceptor.setOverflow(false);
            pageInterceptor.setMaxLimit(500L);
            pageInterceptor.setDbType(DbType.MYSQL);
            interceptor.addInnerInterceptor(pageInterceptor);
            return interceptor;
        }
    }
}

测试代码

 @Test
    void page(){
        IPage<Student> studentPage= new Page<>(2,4);
        studentService.page(studentPage,null);
        System.out.println(studentPage.getRecords());
        System.out.println(studentPage.getPages());
        System.out.println(studentPage.getTotal());
        System.out.println(studentPage.getCurrent());
    }

七、测试结果示例
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程初学者01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值