抛弃Yu-Db了,集成一下Spring-data-Jpa

1、引入Spring-data-jpahibernatehikaricp

val springVersion = "2.6.2"
val hibernateVersion = "5.6.7.Final"
val hikariCPVersion = "4.0.3"

implementation("org.springframework.data:spring-data-jpa:$springVersion")
implementation("org.hibernate:hibernate-core:$hibernateVersion")
implementation("com.zaxxer:HikariCP:$hikariCPVersion")

2、配置hibernate信息

新建一个hibernate.propertiesresources文件夹下

hibernate.connection.drive_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:./db/yuq;AUTO_SERVER=TRUE
hibernate.connection.username=yuq
hibernate.connection.password=yuq
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
hibernate.show_sql=false
hibernate.hbm2ddl.auto=update

3、配置Spring-data-Jpa

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
open class JpaConfig{

    @Bean
    open fun dataSource(): HikariDataSource {
        return HikariDataSource()
    }

    @Bean
    open fun entityManagerFactory(): LocalContainerEntityManagerFactoryBean {
        val vendorAdapter  = HibernateJpaVendorAdapter()
        val factory = LocalContainerEntityManagerFactoryBean()
        // entity、Repository等所在的包名
        factory.setPackagesToScan("me.kuku.yuq.entity")
        factory.jpaVendorAdapter = vendorAdapter
        val properties = Properties()
        properties.load(Thread.currentThread().contextClassLoader.getResourceAsStream("hibernate.properties"))
        factory.setJpaProperties(properties)
        factory.dataSource = dataSource()
        return factory
    }

    @Bean
    open fun transactionManager(emf: EntityManagerFactory): JpaTransactionManager {
        val transactionManager = JpaTransactionManager()
        transactionManager.entityManagerFactory = emf
        return transactionManager
    }

    @Bean
    open fun transactionTemplate(transactionManager: JpaTransactionManager): TransactionTemplate {
        return TransactionTemplate(transactionManager)
    }

}

因为YuQ自带依赖注入,所以在YuQ依赖注入之前把Repository添加到YuQ的容器中,在依赖注入之前运行,需要配置一个模块

YuQ.properties文件中添加

#类路径
yu.modules=me.kuku.yuq.JpaModule

这个类继承于Moudle,并实现onLoad方法

class JpaModule: Module {

    @Inject
    private lateinit var context: YuContext

    override fun onLoad() {
        val applicationContext = AnnotationConfigApplicationContext(JpaConfig::class.java)
        context.putBean(applicationContext)
        transactionManager = applicationContext.getBean(JpaTransactionManager::class.java)
        val transactionTemplate = applicationContext.getBean(TransactionTemplate::class.java)
        context.putBean(transactionTemplate)
        val classes = MyUtils.getClasses("me.kuku.yuq.entity")
        for ((_, v) in classes) {
            v.interfaces.takeIf { it.contains(JpaRepository::class.java) }
                ?.let {
                    val repository = applicationContext.getBean(v)
                    val name = v.name
                    val classContextMap = context::class.java.declaredFields.first { it.name == "classContextMap" }
                        .also { it.isAccessible = true }.get(context) as MutableMap<String, ClassContext>
                    val classContext =
                        ClassContext(name, v, false, null, repository, mutableMapOf("" to repository), null, null)
                    classContextMap[name] = classContext
                }
        }
    }
}

4、启动,需要把Springrepository加入到YuQ的类加载器黑名单中最新版已不需要

fun main(args: Array<String>) {
    YuQArtQQStarter.start(args)
}

5、使用事务

注入TransactionTemplate,调用其execute方法即可

最后修改:2022 年 04 月 07 日
如果觉得我的文章对你有用,请随意赞赏