抛弃Yu-Db
了,集成一下Spring-data-Jpa
1、引入Spring-data-jpa
和hibernate
和hikaricp
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.properties
在resources
文件夹下
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、启动,需要把最新版已不需要Spring
和repository
加入到YuQ
的类加载器黑名单中
fun main(args: Array<String>) {
YuQArtQQStarter.start(args)
}
5、使用事务
注入TransactionTemplate
,调用其execute
方法即可