问题
数据库表字段名称类似这样
然而根据SQL逆向生成的实体类,变量名只可能是这样:
注:此处使用工具http://java.bejson.com/generator/生成的实体类。
实际运行时会发现,实体类与数据库表字段绑定失败、匹配不到字段,原因在于mybatis/mybatis plus匹配变量时按照ext_attr4
去找,而数据库中则是ext_attr_4
。
解决
由于我遇到的情况,无法修改数据库字段,只能从mybatis/mybatis plus这一层着手。使用自定义查询,在XML层中自己手工指定resultMap类型:
<resultMap id="selectMemberByIdResultMap" type="com.br.employee.bean.model.oa.OaOrgMember">
…………………………
<result property="extAttr1" column="EXT_ATTR_1"/>
<result property="extAttr2" column="EXT_ATTR_2"/>
<result property="extAttr3" column="EXT_ATTR_3"/>
<result property="extAttr4" column="EXT_ATTR_4"/>
…………………………
</resultMap>
<select id="selectMemberById" parameterType="java.lang.String" resultMap="selectMemberByIdResultMap">
SELECT
id,
`EXT_ATTR_1`,
`EXT_ATTR_2`,
`EXT_ATTR_3`,
`EXT_ATTR_4`,
…………………………………………
FROM XXXXX m
where 1=1
<if test="id!= null and id!= ''">
and m.id = #{id}
</if>
</select>