在MyBatis中,如何將數據庫中的字符串類型映射為枚舉類型?
網上看了很多教程。說了很多,但是都沒說到重點!
很簡單,xml文件中,··· 使用resultType,而不是使用resultMap就可以了。
resultType="org.jeecg.modules.hc.entity.HcMainCategories">
源碼:
Entity
/*** 名稱*/@Excel(name = "名稱", width = 15)@ApiModelProperty(value = "名稱")private java.lang.String name;/*** 排序*/@Excel(name = "排序", width = 15)@ApiModelProperty(value = "排序")private java.lang.Integer sort;/*** 狀態*/@Excel(name = "狀態", width = 15)@ApiModelProperty(value = "狀態")private HcUpFlagEnum state;
Xml
<resultMap id="BaseResultMap" type="org.jeecg.modules.hc.entity.HcMainCategories"><id property="id" column="ID" jdbcType="VARCHAR"/><result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/><result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/><result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/><result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/><result property="icon" column="ICON" jdbcType="VARCHAR"/><result property="name" column="NAME" jdbcType="VARCHAR"/><result property="sort" column="SORT" jdbcType="BIGINT"/><result property="state" column="STATE" jdbcType="VARCHAR"/></resultMap><sql id="Base_Column_List">ID,CREATE_BY,CREATE_TIME,UPDATE_BY,UPDATE_TIME,ICON,NAME,SORT,STATE</sql><select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from hc_main_categorieswhere ID = #{id,jdbcType=VARCHAR}</select><!--根據地址ID查詢主營品類--><select id="selectByAddressId" resultType="org.jeecg.modules.hc.entity.HcMainCategories">select distinctd.ID,d.CREATE_BY,d.CREATE_TIME,d.UPDATE_BY,d.UPDATE_TIME,d.ICON,d.NAME,d.SORT,d.STATEfromhc_site_address_details aleft join hc_site_relation b on b.SITE_ID = a.SITE_IDleft join hc_main_relation c on c.MERCHANT_ID = b.MERCHANT_IDleft join hc_main_categories d on d.ID = c.MAIN_CATEGORIES_IDwherea.ID = #{vo.id} and d.STATE = 'yes'</select>
Column 'ID' in field list is ambiguous;
SELECT ID, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME, ICON, NAME, SORT, STATE FROM hc_site_address_details a LEFT JOIN hc_site_relation b ON b.SITE_ID = a.SITE_ID LEFT JOIN hc_main_relation c ON c.MERCHANT_ID = b.MERCHANT_ID LEFT JOIN hc_main_categories d ON d.ID = c.MAIN_CATEGORIES_ID WHERE a.ID = ?
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'ID' in field list is ambiguous
; Column 'ID' in field list is ambiguous;
nested exception is java.sql.SQLIntegrityConstraintViolationException: Column 'ID' in field list is ambiguous] with root cause
java.sql.SQLIntegrityConstraintViolationException: Column 'ID' in field list is ambiguous
----
錯誤原因:
··· Base_Column_List中的字段沒有加上別名d.
<sql id="Base_Column_List">ID,CREATE_BY,CREATE_TIME,UPDATE_BY,UPDATE_TIME,ICON,NAME,SORT,STATE
</sql><select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from hc_main_categorieswhere ID = #{id,jdbcType=VARCHAR}
</select>
<select id="selectByAddressId" resultType="org.jeecg.modules.hc.entity.HcMainCategories">select distinctd.ID,d.CREATE_BY,d.CREATE_TIME,d.UPDATE_BY,d.UPDATE_TIME,d.ICON,d.NAME,d.SORT,d.STATEfromhc_site_address_details aleft join hc_site_relation b on b.SITE_ID = a.SITE_IDleft join hc_main_relation c on c.MERCHANT_ID = b.MERCHANT_IDleft join hc_main_categories d on d.ID = c.MAIN_CATEGORIES_IDwherea.ID = #{vo.id} and d.STATE = 'yes'
</select>
?