您的位置:首页 > 其它

[MyBatis日记]问题汇总

2016-02-21 23:25 309 查看

问题一:mybatis映射文件insert不执行,而直接用sql则可以插入成功


解决方案:

studentMapper.insertStudent(student);在执行晚SQL语句之后,记得session.commit();


问题二:中文显示乱码问题


解决方案:

配置数据库地址时加上编码格式characterEncoding:<propertyname="url"value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"/>


问题三:XML文件中大于号小于号问题


解决方案一:用了转义字符把>和<替换掉

[code]<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.sjf.mapper.CourseMapper">
<resultMapid="CourseResultMap"type="com.sjf.bean.Course">
<idproperty="ID"column="ID"/>
<resultproperty="name"column="name"/>
<resultproperty="desc"column="description"/>
<resultproperty="startDate"column="startDate"/>
<resultproperty="endDate"column="endDate"/>
</resultMap>
<selectid="getCourseByCondation"parameterType="hashmap"resultMap="CourseResultMap">
SELECT*FROMCourse
WHEREteacherID=#{teacherID}
<iftest="courseName!=null">
ANDNAME=#{courseName}
</if>
<iftest="startDate!=null">
ANDstartDate>=#{startDate}
</if>
<iftest="endDate!=null">
ANDendDate<=#{endDate}
</if>
</select>
</mapper>


转义字符表:

转义符符号
<<(小于号)
>>(大于号)
&&(和)
''(单引号)
""(双引号)
解决方案二:

因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用<![CDATA[]]>符号进行说明,将此类符号不进行解析。

[code]<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.sjf.mapper.CourseMapper">
<resultMapid="CourseResultMap"type="com.sjf.bean.Course">
<idproperty="ID"column="ID"/>
<resultproperty="name"column="name"/>
<resultproperty="desc"column="description"/>
<resultproperty="startDate"column="startDate"/>
<resultproperty="endDate"column="endDate"/>
</resultMap>
<selectid="getCourseByCondation"parameterType="hashmap"resultMap="CourseResultMap">
SELECT*FROMCourse
WHEREteacherID=#{teacherID}
<iftest="courseName!=null">
ANDNAME=#{courseName}
</if>
<iftest="startDate!=null">
<![CDATA[ANDstartDate>=#{startDate}]]>
</if>
<iftest="endDate!=null">
<![CDATA[ANDendDate<=#{endDate}]]>
</if>
</select>
</mapper>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: