application.yml中配置:
datasources:
dev:
dialect: MYSQL
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: xxx
oranew:
dialect: ORACLE
driverClassName: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:15213/abc
参考官网,只注入了1个数据源: Access a database with MyBatis https://guides.micronaut.io/latest/micronaut-data-access-mybatis-gradle-kotlin.html
如何注入2个数据源, 下面MybatisFactory这种写法会报错:
2023-01-16 10:35:09,187 ERROR i.m.h.s.RouteExecutor 444 - Unexpected error occurred: Failed to inject value for field [dataSource] of class: com.soft.db.MybatisFactory
Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [javax.sql.DataSource, javax.sql.DataSource]
package com.soft.db
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.*
import io.micronaut.inject.qualifiers.Qualifiers
import jakarta.inject.Inject
import jakarta.inject.Named
import jakarta.inject.Singleton
import org.apache.ibatis.mapping.Environment
import org.apache.ibatis.session.Configuration
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactoryBuilder
import org.apache.ibatis.transaction.TransactionFactory
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
import org.slf4j.LoggerFactory
import javax.sql.DataSource
@Factory
class MybatisFactory {
val log = LoggerFactory.getLogger(this.javaClass)
@Inject
@Named("dev")
lateinit var dataSource: DataSource// "default" will be injected
@Inject
@Named("oranew")
lateinit var oranewDataSource: DataSource
@Singleton
fun devSqlSessionFactory(): SqlSessionFactory {
log.info("---------------sqlSessionFactory_dev default")
val transactionFactory: TransactionFactory = JdbcTransactionFactory()
val environment = Environment("dev", transactionFactory, dataSource)
val configuration = Configuration(environment)
configuration.addMappers("com.soft.mapper")
return SqlSessionFactoryBuilder().build(configuration)
}
@Singleton
fun oranewSqlSessionFactory(): SqlSessionFactory {
val transactionFactory: TransactionFactory = JdbcTransactionFactory()
val environment = Environment("oranew", transactionFactory, oranewDataSource)
val configuration = Configuration(environment)
configuration.addMappers("com.soft.mapper")
return SqlSessionFactoryBuilder().build(configuration)
}
}
package com.soft.mapper
import com.soft.Student
import com.soft.db.ConnPool
import com.soft.db.PoolName
import jakarta.inject.Inject
import jakarta.inject.Singleton
import org.apache.ibatis.session.SqlSession
import org.apache.ibatis.session.SqlSessionFactory
import org.slf4j.LoggerFactory
@Singleton
open class StudentMapperImpl : StudentMapper {
companion object {
val log = LoggerFactory.getLogger(StudentMapperImpl::class.java)
}
@Inject
lateinit var devSqlSessionFactory: SqlSessionFactory
private fun getStuMapper(sqlSession: SqlSession?): StudentMapper? {
return sqlSession?.getMapper(StudentMapper::class.java)
}
override fun findById(id: Int): Student? {
//Use try-with-resources to automatically close the SQL session.
devSqlSessionFactory?.openSession().use { sqlSession ->
return getStuMapper(sqlSession)?.findById(id)
}
}
override fun findAll(): List<Student>? {
log.info("environment.id---{}", devSqlSessionFactory?.configuration?.environment?.id)
devSqlSessionFactory?.openSession().use { sqlSession ->
return getStuMapper(sqlSession)?.findAll()
}
}
}