Spring
Kotlin & Spring Boot-메서드를 "원숭이 패치"하고있는 MyBatis 매퍼가 있으며 메서드에 자동 연결 빈이 필요합니다.
기록만이살길
2021. 3. 15. 09:03
Kotlin & Spring Boot-메서드를 "원숭이 패치"하고있는 MyBatis 매퍼가 있으며 메서드에 자동 연결 빈이 필요합니다.
1. 질문(문제점):
Kotlin에 MyBatis 매퍼 인 인터페이스가 있습니다.
@Mapper
interface EquipmentSupportMapper {
@SelectProvider(type = SqlProviderAdapter::class, method = "select")
fun findMany(selectStatement: SelectStatementProvider?): List<Equipment>
}
그런 다음 메서드 구현을 추가합니다.
fun EquipmentSupportMapper.searchEquipment(
textSearchValue: String? = null, facilities: Array<Int>? = null,
manufacturers: Array<Int>? = null, equipmentExample: Equipment? = null
):
List<Equipment> {
val builder = SqlBuilder.select(equipmentColumnList)
.from(EquipmentSupport.EquipmentTable)
.where()
...
return someList;
이제 내 방법에서 라는 스프링 빈이 searchEquipment필요합니다 .@AutowireMetaMapper
다음을 추가 할 수 없습니다.
@Autowired lateinit var metaMapper: MetaMapper인터페이스에서 오류가 발생하기 때문입니다. 어떻게 할 수 있습니까?
2. 해결방안:
내가 이것을 할 수 있다는 것이 밝혀졌습니다.
@Mapper
@Qualifier("IESM")
interface InterfaceEquipmentSupportMapper {
@SelectProvider(type = SqlProviderAdapter::class, method = "select")
fun findMany(selectStatement: SelectStatementProvider?): List<Equipment>
}
@Service
@Qualifier("EquipmentSupportMapper")
class EquipmentSupportMapper(
@Autowired @Qualifier("IFSM") ifsm: InterfaceEquipmentSupportMapper,
@Autowired metaMapper: MetaMapper
): InterfaceEquipmentSupportMapper by ifsm
트릭은 Spring이 구현을 생성하는 인터페이스에 한정자를 생성 한 다음 autowired Spring 생성 빈에 위임하여 동일한 인터페이스를 구현하는 클래스에이를 주입하는 것입니다.
그런 다음 내 앱에서 Mapper호출 된 IESM 을 사용하는 대신 한정자 EquipmentSupportMapper를 사용하여 자동 연결하고 올바른 것을 얻습니다.
다음은 테스트의 예입니다.
@SpringBootTest
class EquipmentSupportMapperTest {
@Autowired
@Qualifier("EquipmentSupportMapper")
lateinit var mapper: EquipmentSupportMapper
65677219