Mybatis如何使用Guava的不可变集合?

问题描述 投票:0回答:1

我尝试制作返回类型 ImmutableList 并从表中选择一堆 ID,但当然 mybatis 不知道该怎么做并抛出异常:

org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.google.common.collect.ImmutableList with invalid types () or values (). Cause: java.lang.InstantiationException
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:88)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:53)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45)
    at org.apache.ibatis.binding.MapperMethod.convertToDeclaredCollection(MapperMethod.java:173)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:154)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
    at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:145)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:86)
    at jdk.proxy5/jdk.proxy5.$Proxy76.getAllIds(Unknown Source)
    at org.acanthite.services.UtilityService.getAllIds(UtilityService.java:98)
    at org.acanthite.services.UtilityService_ClientProxy.getAllIds(Unknown Source)
    at org.acanthite.resources.UtilityResource.ids(UtilityResource.java:77)
    at org.acanthite.resources.UtilityResource$quarkusrestinvoker$ids_8602df045ce9b7e168a75788bbd936486ed83b98.invoke(Unknown Source)
    at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
    at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:141)
    at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:145)
    at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:576)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
    at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
    at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.InstantiationException
    at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:66)
    ... 22 more

这是我的映射器的 XML:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.acanthite.repository.UtilityRepository">
    <select id="getAllIds" resultType="integer">
        select id from utility;
    </select>
</mapper>

映射器界面:

@Mapper
public interface UtilityRepository {
  ImmutableList<Integer> getAllIds();
}

以及我如何使用它:

@ApplicationScoped
@AllArgsConstructor
public class UtilityService {
  private final UtilityRepository repository;
  
  public ImmutableList<Integer> getAllIds() {
    return repository.getAllIds();
  }
}

是否有库或变通方法教 mybatis 如何实例化 Guava 的不可变集合(或基本上任何其他非 java 集合)?

java quarkus guava mybatis
1个回答
0
投票

尝试创建一个

ArrayList
,然后使用
ImmutableList#copyOf(Collection<? extends E>)
将其转换为
ImmutableList
.

© www.soinside.com 2019 - 2024. All rights reserved.