springboot整合mybaitsplus和达梦数据库分页/fastjson/时间转换

java 文章 2022-07-24 14:20 0 全屏看文

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

准备数据

搭建springboot

pom依赖

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


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!--达梦数据库依赖-->
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>Dm8JdbcDriver18</artifactId>
            <version>8.1.1.49</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </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
@MapperScan("com.baomidou.cloud.service.*.mapper*")
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);
    }

}


-EOF-