您的位置:首页 > 其它

mybatis resultMap和转义符 知识点

2017-12-29 10:03 197 查看
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的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:
原符号<<=>>=&'"
替换符号<<=>>=&'"
2、使用

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

SELECT * FROM  user WHERE  age  <= 30 AND age >= 18


<update id="updateSearchControlFeedback">

update SEARCH_CONTROL_FEEDBACK set IS_ARRETED = #{isArreted}

,FEEDBACK_TIME=#{feedbackTime}

,EVENT_STATUS = #{newEventStatus}

where EVENT_STATUS=#{oldEventStatus}

<![CDATA[ AND RECEIVE_TIME <= #{receiveTime} ]]>

</update>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: