springboot 整合mybaitsplus和达梦数据库分页

springboot 整合mybaitsplus和达梦数据库分页

准备数据

springboot 整合mybaitsplus和达梦数据库分页_第1张图片

搭建springboot

pom依赖

    <parent>
        <artifactId>spring-boot-starter-parentartifactId>
        <groupId>org.springframework.bootgroupId>
        <version>2.6.1version>
    parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


        
        <dependency>
            <groupId>com.damenggroupId>
            <artifactId>Dm8JdbcDriver18artifactId>
            <version>8.1.1.49version>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.83version>
        dependency>

    dependencies>

配置文件

spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    url: jdbc:dm://127.0.0.1:5236/SYSDBA?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true
    username: SYSDBA
    password: SYSDBA


mybatis-plus:
  #  global-config:
  #    db-config:
  #      # 表名前缀
  #      table-prefix: tb_
  #      # id生成策略 数据库自增
  #      id-type: auto
  configuration:
    #开启日志  这样可以再控制台查看sql的语句
    log-impl:  org.apache.ibatis.logging.stdout.StdOutImpl

启动类

package com.thq;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.thq.mapper")
public class DmPageApplicaiton {
    public static void main(String[] args) {
        SpringApplication.run(DmPageApplicaiton.class);
    }
}

domin

package com.thq.domin;


import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@TableName("SYSDBA.STUDENT")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private Integer id;
    private String name;
    private Integer age;

    //@DateTimeFormat(pattern = "yyyy/MM/dd")   /* 插入数据时候 时间转换*/
    //@JSONField(format="yyyy/MM/dd")   /** 使用fastjson 用他转换字符串 就悔把这个时间格式转换 */
    private Date stime;

}

mapper


public interface StudentMapper extends BaseMapper<Student> {


}

mybatis plus 分页插件配置类

package com.thq.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//Spring boot方式
@Configuration
public class MybatisPlusConfig {

     旧版
    //@Bean
    //public PaginationInterceptor paginationInterceptor() {
    //    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    //    // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
    //    // paginationInterceptor.setOverflow(false);
    //    // 设置最大单页限制数量,默认 500 条,-1 不受限制
    //    // paginationInterceptor.setLimit(500);
    //    // 开启 count 的 join 优化,只针对部分 left join
    //    paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
    //    return paginationInterceptor;
    //}
  
    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
  
}

测试类

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thq.DmPageApplicaiton;
import com.thq.domin.Student;
import com.thq.mapper.StudentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest(classes = DmPageApplicaiton.class)
public class NiTest {

    @Autowired
    StudentMapper studentMapper;

    @Test
    public void t1(){
        Student student = studentMapper.selectById(2);
        System.out.println(student);
    }

    @Test
    public void t2() {
        Page<Student> page = new Page<>(3, 2);
        studentMapper.selectPage(page, null);
        List<Student> records = page.getRecords();
        System.out.println(records);
    }

}


按照时间查询并排序

 /**
     * 按照时间查询 ,并排序
     */
    @Test
    public void t3() {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(Student::getStime, "2022-06-23", "2022-06-26")
                .orderByAsc(Student::getStime);


        Page<Student> page = new Page<>(1, 10);
        studentMapper.selectPage(page, queryWrapper);

        List<Student> records = page.getRecords();
        System.out.println(records);

    }

你可能感兴趣的:(mybatis,java,spring,boot,数据库)