tower888 2023-01-16 11:19 采纳率: 0%
浏览 38

micronaut mybatis多数据源注入问题

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()
        }
    }
}

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 修改了问题 1月16日
  • 创建了问题 1月16日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀