[MyBatis日记]问题汇总

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

studentMapper.insertStudent(student);在执行晚SQL语句之后,记得session.commit();
问题二:中文显示乱码问题
解决方案:

配置数据库地址时加上编码格式characterEncoding: <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"/> 
问题三:XML文件中大于号小于号问题
解决方案一: 用了转义字符把>和<替换掉
    
    
    
    
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sjf.mapper.CourseMapper">
 
<resultMap id = "CourseResultMap" type="com.sjf.bean.Course" >
<id property="ID" column="ID"/>
<result property="name" column="name"/>
<result property="desc" column="description"/>
<result property = "startDate" column = "startDate"/>
<result property = "endDate" column = "endDate"/>
</resultMap>
<select id="getCourseByCondation" parameterType="hashmap" resultMap="CourseResultMap">
SELECT * FROM Course
WHERE teacherID = #{teacherID}
<if test = "courseName != null" >
AND NAME = #{courseName}
</if>
<if test = "startDate != null">
AND startDate >= #{startDate}
</if>
<if test = "endDate != null">
AND endDate <= #{endDate}
</if>
</select>
</mapper>

转义字符表:
转义符 符号
&lt; <(小于号)
&gt; >(大于号)
&amp; &(和)
&apos; '(单引号)
&quot; "(双引号)

解决方案二:
因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用<![CDATA[ ]]>符号进行说明,将此类符号不进行解析。
    
    
    
    
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sjf.mapper.CourseMapper">
 
<resultMap id = "CourseResultMap" type="com.sjf.bean.Course" >
<id property="ID" column="ID"/>
<result property="name" column="name"/>
<result property="desc" column="description"/>
<result property = "startDate" column = "startDate"/>
<result property = "endDate" column = "endDate"/>
</resultMap>
<select id="getCourseByCondation" parameterType="hashmap" resultMap="CourseResultMap">
SELECT * FROM Course
WHERE teacherID = #{teacherID}
<if test = "courseName != null" >
AND NAME = #{courseName}
</if>
<if test = "startDate != null">
<![CDATA[AND startDate >= #{startDate} ]]>
</if>
<if test = "endDate != null">
<![CDATA[AND endDate <= #{endDate} ]]>
</if>
</select>
</mapper>










你可能感兴趣的:(MyBatis日记)