All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.postgres.TestDB.kt Maven / Gradle / Ivy

package test.postgres

import cn.hutool.core.util.RandomUtil
import cn.hutool.db.DbUtil
import jchanghong.kotlin.beanFillNullValues
import jchanghong.kotlin.log
import org.mybatis.spring.SqlSessionTemplate
import org.springframework.boot.CommandLineRunner
import org.springframework.stereotype.Service
import test.dao.dao.AutoMapperTestTable
import test.dao.dao.DOTestTable
import java.util.*

//数据库43秒
//@Service
class TestDB(val autoMapperTestTable: AutoMapperTestTable,
val sqlSessionTemplate: SqlSessionTemplate):CommandLineRunner{
    fun insert(): Unit {
        val table = DOTestTable().beanFillNullValues().apply { this.id = null
        this.aUuid=null}
        val list = (1..5000).map { testRow() }
        autoMapperTestTable.delete(null)
        println("开始测试数据库")
        val scanner = Scanner(System.`in`)
        while (scanner.hasNext()) {
            sendMessage(scanner.nextInt(), table)
        }
        sendMessage(1000,table)
    }
    private fun sendMessage(i: Int, table: DOTestTable) {
        val millis = System.currentTimeMillis()
        (0..i).toList().parallelStream().forEach{
            autoMapperTestTable.insert(table)
        }
        log.info("数量$i 时间 ${System.currentTimeMillis() - millis}")
    }
    fun testRow(): DOTestTable {
      return  DOTestTable().apply {
            beanFillNullValues()
            aUuid = null
            id=null
            aText=RandomUtil.randomString(100)
            aVarchar=RandomUtil.randomString(200)
        }
    }

    override fun run(vararg args: String?) {
        DbUtil.use().execute("""
            create  table if not exists test_table
            (
                id        bigserial not null
                        primary key,
                a_int     bigint,
                a_decimal numeric,
                a_varchar varchar,
                a_text    text,
                a_byte    bytea,
                a_time    timestamp,
                a_date    date,
                a_boolean boolean,
                a_uuid    uuid,
                a_json    jsonb
            )
        """.trimIndent())
        insert()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy