在MyBatis中,如何将数据库中的字符串类型映射为枚举类型?

在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_categories

where ID = #{id,jdbcType=VARCHAR}

</select>

<!--根据地址ID查询主营品类-->

<select id="selectByAddressId" resultType="org.jeecg.modules.hc.entity.HcMainCategories">

select distinct

d.ID,d.CREATE_BY,d.CREATE_TIME,

d.UPDATE_BY,d.UPDATE_TIME,d.ICON,

d.NAME,d.SORT,d.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 = #{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_categories
    where  ID = #{id,jdbcType=VARCHAR}
</select>
<select id="selectByAddressId" resultType="org.jeecg.modules.hc.entity.HcMainCategories">
    select distinct
        d.ID,d.CREATE_BY,d.CREATE_TIME,
        d.UPDATE_BY,d.UPDATE_TIME,d.ICON,
        d.NAME,d.SORT,d.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 = #{vo.id} and d.STATE = 'yes'
</select>

 

相关推荐

  1. MyBatis:类型字符串比较

    2024-05-12 06:36:17       46 阅读
  2. MyBatis(17)MyBatis 如何处理类型

    2024-05-12 06:36:17       24 阅读
  3. C其他类型转换字符类型

    2024-05-12 06:36:17       34 阅读
  4. 【Django】类型数据

    2024-05-12 06:36:17       36 阅读
  5. mybatis映射postgres数据库geometry类型

    2024-05-12 06:36:17       43 阅读
  6. 字符串转换Python数据类型

    2024-05-12 06:36:17       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 06:36:17       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 06:36:17       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 06:36:17       82 阅读
  4. Python语言-面向对象

    2024-05-12 06:36:17       91 阅读

热门阅读

  1. TensorFlow 的基本概念和使用场景。

    2024-05-12 06:36:17       28 阅读
  2. 【图像畸变校正】

    2024-05-12 06:36:17       35 阅读
  3. ES 7.0.0 升级 7.1.0,离线升级

    2024-05-12 06:36:17       27 阅读
  4. 等保测评安全物理环境测评讲解

    2024-05-12 06:36:17       29 阅读
  5. SpringBoot集成Minio

    2024-05-12 06:36:17       34 阅读
  6. 使用torch.nn.ModuleList构建神经网络

    2024-05-12 06:36:17       38 阅读