乐趣区

关于mybatis:MyBatis查询Error-instantiating-class-types-or-values

在学习 mybatis 时的关联关系时遇到了这个问题。

Error instantiating class com.springmvc_mybatis.pojo.Items with invalid types () or values ().

实例化具备有效类型()或值()的类 com.springmvc_mybatis.pojo.items 时出错

解决办法:

实例化失败,阐明 pojo 类的构造函数有问题,本次谬误地点在于我的实体类上,没有提供一个 mybatis 的 resultMap 后果集构造函数,我将此构造函数加上就不报错了。

因为 mapper.xml 中有一个 resultMap 须要用到这个构造函数:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.star.mapper.NewsMapper">
 <resultMap id="BaseResultMap" type="com.star.model.News">
 <constructor>
 <idArg column="news_id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
 <arg column="title" jdbcType="VARCHAR" javaType="java.lang.String"/>
 </constructor>
 </resultMap>
 <!-- 新闻对应多个类型标签:一对多关系 -->
 <resultMap id="newsManyToMany" type="com.star.model.News" extends="BaseResultMap">
<!--        <id column="news_id" property="newsId"/>-->
<!--        <result column="title" property="title"/>-->
 <collection property="categories" column="newsId" ofType="com.star.model.Category"
 select="com.star.mapper.CategoryMapper.queryNewsByCategoryId">
 </collection>
 </resultMap>
 <sql id="Base_Column_List">
 news_id, title
    </sql>
 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
 select
        <include refid="Base_Column_List"/>
 from t_news
        where news_id = #{newsId,jdbcType=INTEGER}
 </select>
 <select id="queryCategoryByNewId" resultMap="newsManyToMany">
 select * from t_news nws
 left join t_news_category nct on nws.news_id = nct.nid
        left join t_category ctg on ctg.category_id = nct.cid
        where nws.news_id = #{newsId}
 </select>
 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
 delete from t_news
    where news_id = #{newsId,jdbcType=INTEGER}
 </delete>
 <insert id="insert" parameterType="com.star.model.News">
 insert into t_news (news_id, title)
 values (#{newsId,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR})
 </insert>
 <insert id="insertSelective" parameterType="com.star.model.News">
 insert into t_news
        <trim prefix="(" suffix=")" suffixOverrides=",">
 <if test="newsId != null">
 news_id,
            </if>
 <if test="title != null">
 title,
            </if>
 </trim>
 <trim prefix="values (" suffix=")" suffixOverrides=",">
 <if test="newsId != null">
 #{newsId,jdbcType=INTEGER},
            </if>
 <if test="title != null">
 #{title,jdbcType=VARCHAR},
            </if>
 </trim>
 </insert>
 <update id="updateByPrimaryKeySelective" parameterType="com.star.model.News">
 update t_news
        <set>
 <if test="title != null">
 title = #{title,jdbcType=VARCHAR},
            </if>
 </set>
 where news_id = #{newsId,jdbcType=INTEGER}
 </update>
 <update id="updateByPrimaryKey" parameterType="com.star.model.News">
 update t_news
    set title = #{title,jdbcType=VARCHAR}
 where news_id = #{newsId,jdbcType=INTEGER}
 </update>
</mapper>
退出移动版