数据库列一般是以单词命名,单词间以下划线分隔,而java属性一般采用驼峰命名法,所以在mapper文件中需要对它们进行映射。目前我们采用的方式是手动进行映射,比如:
<resultMap type="com.chunbo.gift.model.GiftCondition" id="giftConditionResult"> <result property="conditionId" column="condition_id" /> <result property="activityId" column="activity_id" /> <result property="code" column="code" /> <result property="lower" column="lower" /> <result property="upper" column="upper" /> <result property="startDate" column="start_date" /> <result property="endDate" column="end_date" /> </resultMap>
然后把SQL的resultMap指定为刚才定义的类型:
<select id="getGiftConditionList" parameterType="int" resultMap="giftConditionResult">
但这种方式相对繁琐。Mybatis中可以通过增加mapUnderscoreToCamelCase配置项来简化,在mybatis-config.xml中增加如下的设置:
<settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings>
然后把上面的SQL修改为下面的形式即可:
<select id="getGiftConditionList" parameterType="int" resultType="com.chunbo.gift.model.GiftCondition">
这样就大大简化了配置工作!
附:mybatis官网对于属性mapUnderscoreToCamelCase的说明:
Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn.