mybatis resultMap和转义符 知识点

1.mapper时,resultMap与resultType不能写错.Map为自定义属性,没写的属性就不映射,可以把一对一的级连关系值也可以映射上

<mapper namespace="com.qbd.mapper.StudentMappers">
    <select id="findbyid" parameterType="Integer" resultMap="StudentResult">
        select *from student where id=#{id}
    </select>
    
    <select id="findbygradeid" parameterType="Integer" resultMap="StudentResult">
        select *from student where gid=#{gid}
    </select>

     <select id="findbygradeid2" parameterType="Integer" resultType="com.qbd.mapper.AddressMappers.findbyid">
        select *from student where gid=#{gid}
    </select>
    
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addid" select="com.qbd.mapper.AddressMappers.findbyid">    
        </association>
        <association property="grade" column="gid" select="com.qbd.mapper.GradeMappers.findbyid">    
        </association>
    </resultMap>
</mapper>
2.mybatis转义符

在mapper文件中直接写<会出错,sql预编译时出错,项目都运行不起来。这时就要用转义符

1、在xml的sql语句中,不能直接用大于号、小于号要用转义字符

如果用小于号会报错误如下:

org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

转义字符

这样的问题在MyBatis中或者自定义的xml处理sql的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:

原符号  <   <=   >   >=    &     '      "
替换符号&lt;&lt;=&gt;&gt;=&amp;&apos;&quot;
2、使用

<![CDATA[ ]]>标记的sql语句中的<where> <if>等标签不会被解析

举例如下:

SELECT * FROM  user WHERE  age  &lt;= 30 AND age &gt;= 18
  1.  <update id="updateSearchControlFeedback">  
  2.     update SEARCH_CONTROL_FEEDBACK set IS_ARRETED = #{isArreted}   
  3.     ,FEEDBACK_TIME=#{feedbackTime}  
  4.     ,EVENT_STATUS = #{newEventStatus}  
  5.     where EVENT_STATUS=#{oldEventStatus}  
  6.     <![CDATA[ AND RECEIVE_TIME <= #{receiveTime} ]]>  
  7. </update>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值