mybatis 动态sql返回一个List封装类报错求解决方法

报错信息:

Exception in thread "main" org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.sun.tools.javac.util.List with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.sun.tools.javac.util.List.<init>()
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:83)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:38)
    at org.apache.ibatis.binding.MapperMethod.convertToDeclaredCollection(MapperMethod.java:138)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:131)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
    at com.sun.proxy.$Proxy0.findUserById(Unknown Source)
    at com.mybatis.SQL.App.main(App.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.NoSuchMethodException: com.sun.tools.javac.util.List.<init>()
    at java.lang.Class.getConstructor0(Class.java:3069)
    at java.lang.Class.getDeclaredConstructor(Class.java:2165)
    at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:57)
    ... 13 more

代码如下:
映射文件配置:

<mapper namespace="com.mybatis.SQL.mapping.Impl.UserOrderMapper">
    <select id="findUserById" parameterType="map" resultMap="userResult">
        SELECT t.id,t.username,t.sex,t.address
        FROM t_user t
        <if test="id != null" >
            WHERE t.id=#{id}
        </if>
        <if test="address != null">
            WHERE t.address LIKE #{address}
        </if>
        <if test="address != null &amp; id != null" >
          AND t.address LIKE #{address}
        </if>
    </select>
    <resultMap id="userResult" type="com.mybatis.SQL.domain.User">
        <id property="id" column="id" />
        <result property="username" column="username" />
        <result property="Sex" column="sex" />
        <result property="address" column="address" />
        <!--<collection property="orderses" resultMap="ordersResult" />-->
    </resultMap>
</mapper>
接口类:
public interface UserOrderMapper {

    public List<User> findUserById(Map<String,Object> keyMap);
}

domain类:

public class User {
    private int id;//主键
    private String username;
    private Date birthday;
    private String Sex;
    private String address;
    
    省略getter 和 setter方法
    }

测试类:

public class App {

    public static void main(String[] args) {
        SqlSession session = MyBatisUtil.getSqlSession();
        Map<String,Object> map = new HashMap<String, Object>();
//        map.put("id",12);
        map.put("address","%广%");
        UserOrderMapper mapper = session.getMapper(UserOrderMapper.class);
        List<User> list = mapper.findUserById(map);
        System.out.println(list);

    }
}

请问我代码哪里出了问题,为何List<User>不能创建?求各位大神帮忙看看分析一下。本人刚刚学习mybatis,在google和百度找不到解决方法所以才需要帮忙的。

阅读 11.1k
2 个回答

List类import错了吧。

你本意是importjava.util.List吧,但你的Mapper类import的是com.sun.tools.javac.util.List

UserOrderMapper里面import的List错了,应该是java.util.List

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏