[MyBatis日记]问题汇总
Posted SunnyYoona
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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>
转义字符表:
转义符 | 符号 |
---|---|
< | <(小于号) |
> | >(大于号) |
& | &(和) |
' | ‘(单引号) |
" | "(双引号) |
解决方案二:
因为这个是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日记]问题汇总的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot2之使用MyBatis Plus遇到的问题汇总
Mybatis报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题解决办法汇总(示例代
Mybatis报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题解决办法汇总(示例代