mysql - mybatis 动态sql返回一个List封装类报错求解决方法
伊谢尔伦
伊谢尔伦 2017-04-17 13:58:18
0
2
1408

报错信息:

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和百度找不到解决方法所以才需要帮忙的。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
迷茫

List The class import is wrong.

Your original intention was to import java.util.List, but your Mapper class imported com.sun.tools.javac.util.List.

PHPzhong

The imported List in UserOrderMapper is wrong, it should be java.util.List

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!