mybatis resultMap和转义符 知识点
2017-12-29 10:03
197 查看
1.mapper时,resultMap与resultType不能写错.Map为自定义属性,没写的属性就不映射,可以把一对一的级连关系值也可以映射上
在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>等标签不会被解析
举例如下:
<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>
<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的程序中经常需要我们来处理。其实很简单,我们只需作如下替换即可避免上述的错误:
原符号 | < | <= | > | >= | & | ' | " |
替换符号 | < | <= | > | >= | & | ' | " |
<![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>
相关文章推荐
- Mybatis基础---常用SQL映射标签、多参数设置、resultMap(自定义映射)
- Mybatis中<select id="getImages" resultType="java.util.Map">查出的结果顺序
- MyBatis错误:Result Maps collection already contains value for novel.storage.mapper.NovelMapper.BaseResultMap
- MyBatis返回类型resultType和resultMap
- Mybatis中的resultType和resultMap
- mybatis-关联查询(resultType&resultMap)
- Mybatis中的resultType和resultMap
- mybatis学习之多表关联查询和resultmap的常用标签和属性 和分页查询
- 【Mybatis框架】输出映射-resultType与resultMap
- mybatis中的resultMap的映射关系详解
- MyBatis笔记:使用resultMap映射sql返回的结果
- mybatis的resultType和resultMap基础用法
- 博为峰JavaEE技术文章 ——MyBatis Mapper ResultMap(4)
- MyBatis(八) resultMap 之 (三) 延迟加载 (懒加载)
- MyBatis:关联查询——一对多 多表连接、单独查询(详解resultMap)
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- mybatis中映射文件的resultType和resultMap
- Mybatis中的resultType和resultMap
- JavaEE_Mybatis_SpringMVC_Mybatis_ResultMap 与 ResultType应用场合选择