- 通过在 SQL 的查询语句中定义字段名的别名,然后把字段名的别名与实体类的属性名一致。
<!--方式一:通过为数据库字段名设置别名的方式来解决--> <select id="findAll" resultType="com.itbaizhan.pojo.Teacher"> select tid as id,tname as teacherName from teacher; </select>
2. 通过 <resultMap> 标签来映射字段名与实体类属性名的一一对应的关系。
<!--方式二:通过resultMap映射字段名与实体类属性名的一一对应关系来解决--> <!--id:自定义映射名(自己定义) type:自定义映射的对象类型--> <resultMap id="teacherMapper" type="com.itbaizhan.pojo.Teacher"> <!--id:定义主键列 property:POJO属性名 column:数据库列名--> <id property="id" column="tid"></id> <!--result:定义普通列 property:POJO属性名 column:数据库列名--> <result property="teacherName" column="tname"></result> </resultMap> <select id="findAll" resultMap="teacherMapper"> select * from teacher; </select>