Mybatis笔记


 错误原因:在StudentMapper.xml中没有增加命名空间,致使xml无法找到映射接口

基于xml的Mybatis配置流程图

 

当数据库的字段名和javabean的属性名不一致时,会产生NullPoint异常

方案一:用as语句改变sql语句查询后的结果的字段名,使其和javabean的属性名相同
例:SELECT student_id as studentId,student_name as studentName FROM tb_student WHERE STUDENT_ID=#{studentId}

方案二:通过resultMap标签设置pojo与数据库表字段一一对应
例:
<resultMap type="cn.gzsxt.pojo.Student" id="studentMap">
<id property="studentId" column="student_id"/>
<result property="studentName" column="student_name"/>
<result property="studentPwd" column="student_pwd"/>
</resultMap>
<select id="findById" resultMap="studentMap">
SELECT * FROM tb_student WHERE STUDENT_ID=#{studentId}
</select>

方案三:通过配置全局驼峰原则,在mybatis-config.xml配置一个
<settings>
<setting></setting>
</settings>

原文地址:https://www.cnblogs.com/lch-Hao/p/10754298.html