resultMap和resultType,parameterMap和parameterType的区别

resultMap和resultType

       两者都表示查询结果集与java对象之间的一种关系,处理查询结果集,映射到java对象

       resultMap表示将查询结果集中的列一一映射到bean对象的各个属性。映射的查询结果集中的列标签可以根据需要灵活变化,并且还可以通过typeHandler设置查询结果值的类型转换,例如boolean与0和1之间的类型转换。

例:

<resultMap type="hdu.terence.bean.Message" id="MessageResult"> 

    <!--存放Dao值--><!--type是和数据库对应的bean类名Message-->
    <id column="id" jdbcType="INTEGER" property="id"/><!--主键标签-->
    <result column="COMMAND" jdbcType="VARCHAR" property="command"/>
    <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>
</resultMap>

<select id="queryMessageList" parameterType="hdu.terence.bean.Message" 
    resultMap="MessageResult">
        SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1      
    <if test="command!=null and!&quot;&quot;.equals(command.trim())">
    	and COMMAND=#{command}
    </if>
    <if test="description!=null and!&quot;&quot;.equals(description.trim())">
    	and DESCRIPTION like '%' #{description} '%'
    </if> 
  </select>

       resultType表示的是bean中的对象类,此时可以省略掉resultMap标签的映射,但是必须保证查询结果集中的属性和bean对象中的属性是一一对应的,此时大小写不敏感,但是有限制。

       以下是resultType的写法,将其设置成对应的java类上即可,不需要上述resultMap的映射关系。

<select id="queryMessageList" parameterType="hdu.terence.bean.Message"   resultType=" hdu.terence.bean.Message ">
    
    SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1      

    <if test="command!=null and!&quot;&quot;.equals(command.trim())">

        and COMMAND=#{command}

    </if>

    <if test="description!=null and!&quot;&quot;.equals(description.trim())">

        and DESCRIPTION like '%' #{description} '%'

    </if> 

  </select>

parameterMap(已废弃)和parameterType

       parameterMapresultMap类似,表示将查询结果集中列值的类型一一映射到java对象属性的类型上(手动),不建议使用。

       一般使用paramaterType直接将查询结果列值类型自动对应到java对象属性类型上(自动),不再配置映射关系一一对应,例如上述代码中查询结果类型自动对应到hdu.terence.bean.Message的Bean对象属性类型。

参考原文地址:https://blog.csdn.net/csdn_terence/article/details/60779889

猜你喜欢

转载自blog.csdn.net/xhf852963/article/details/81735797