发布于 2017-09-03 04:08:27 | 196 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


这篇文章主要介绍了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 
  FROM ARTICLE        
 </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 
  FROM ARTICLE    
 </select>
<resultmap id="BaseResultMap" type="com.pet.bean.Article">
 <id column="ID" jdbctype="INTEGER" property="id">
 <result column="AUTHOR_NAME" jdbctype="VARCHAR" property="authorName">
 <result column="TITLE" jdbctype="VARCHAR" property="title">
 <result column="CONTENT" jdbctype="VARCHAR" property="content">
 <result column="MAKE_TIME" jdbctype="VARCHAR" property="makeTime">
 <result column="UPDATE_TIME" jdbctype="VARCHAR" property="updateTime">
 <result column="KIND" jdbctype="VARCHAR" property="kind">
 <result column="ABOUT" jdbctype="VARCHAR" property="about">
</result></result></result></result></result></result></result></id></resultmap>

日志显示:

以上所述是小编给大家介绍的解决mybatis执行SQL语句部分参数返回NULL问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务