主要原因:表的字段名找不到实体类的对应的属性
解决方案:
1、开启mybatis的字段命名驼峰转换,在 application.yml添加
mybatis: configuration: map-underscore-to-camel-case: true
即可。
2、采用resultMap,将查出的数据的列名称对应的实体属性单独定义,形式为<resultMap>...</resultMap>。例如:
<resultMap id="ResultMap1" type="model.student">
<id column="student_id" property="id" jdbcType="VARCHAR" />
<result column="studnet_name" property="name" jdbcType="VARCHAR" />
<result column="student_sex" property="sex" jdbcType="VARCHAR" />
</resultMap>
然后在写SELECT语句时,就可以引用了,
<select id="queryStudnetInfo" resultMap="ResultMap1">
select * from department
where
student_id=#{id,jdbcType=VARCHAR}
</select>
3、写SQL语句时给列名添加别名,别名对应实体的属性
对应的实体类如下:
public class Member {
private Integer memberId;
private String memberName;
private String department;
// 省略getter和setter
}
对应的Mapper.xml如下:
<select id="selectMemberByMemberName" resultType="Member">
SELECT member_id as memberId,member_name as memberName
FROM member
WHERE member_name = #{memberName}
</select>