解决:mybatis执行SQL语句部分参数返回NULL
今天在写代码的时候发现一个问题:mybatis执行sql语句的时候返回bean的部分属性为null,在数据库中执行该sql语句能够正常返回,把相关代码反反复复翻了个遍,甚至都重启eclipse了,依旧没解决问题,后来网上搜了一下,还真有类似的问题
闲话少说,直接说问题,该sql语句是自己写的,resultType直接用了该bean全名称,最终导致部分属性显示为null,
原来的写法:
<select id="selectByArticle" parameterType="com.pet.bean.Article" resultMap="com.pet.bean.Article">
SELECT <include refid="Base_Column_List"/>
FROM ARTICLE
<where>
<include refid="com.pet.mapper.ArticleMapper.queryCondition" />
</where>
</select>
<sql id="queryCondition">
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(id)">AND ID = #{id,jdbcType=Integer}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(authorName)">AND AUTHOR_NAME = #{authorName,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(title)">AND TITLE = #{title,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(content)">AND CONTENT = #{content,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(makeTime)">AND MAKE_TIME = #{makeTime,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(updateTime)">AND UPDATE_TIME = #{updateTime,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(kind)">AND KIND = #{kind,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(about)">AND ABOUT = #{about,jdbcType=VARCHAR}</if>
<if test="@org.apache.commons.lang.StringUtils@isNotBlank(status)">AND STATUS = #{status,jdbcType=VARCHAR}</if>
</sql>
部分代码:
日志显示:
修改后的写法:resultType改成了resultMap了
<select id="selectByArticle" parameterType="com.pet.bean.Article" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM ARTICLE
<where>
<include refid="com.pet.mapper.ArticleMapper.queryCondition" />
</where>
</select>
<resultMap id="BaseResultMap" type="com.pet.bean.Article" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="AUTHOR_NAME" property="authorName" jdbcType="VARCHAR" />
<result column="TITLE" property="title" jdbcType="VARCHAR" />
<result column="CONTENT" property="content" jdbcType="VARCHAR" />
<result column="MAKE_TIME" property="makeTime" jdbcType="VARCHAR" />
<result column="UPDATE_TIME" property="updateTime" jdbcType="VARCHAR" />
<result column="KIND" property="kind" jdbcType="VARCHAR" />
<result column="ABOUT" property="about" jdbcType="VARCHAR" />
</resultMap>
日志显示:
还没有评论,来说两句吧...