Mybatis field matching problem

Mybatis field matching

In development, sometimes we encounter the inconsistency between the database field and the attribute name in the entity class. Although this situation can be solved by aliasing, it will undoubtedly reduce the efficiency of development.
Therefore, the configuration of resultMap is provided in MyBatis. The specific code is as follows

<!-- 当数据库列名与java实体类中的字段名不一致时,使用该配置   -->
    <resultMap id="goodsMap" type="com.imis.pojo.Goods">
<!--   配置主键     -->
        <id property="goods_id" column="id"></id>
<!--    其他字段    -->
        <result property="goods_name" column="name"></result>
        <result property="goods_price" column="price"></result>
    </resultMap>

The property is the attribute name in the entity class, and the column is the field name of the table in the database. This correspondence can no longer pay attention to whether the attribute name matches the field name in subsequent queries.

In subsequent use, such as query, you do not need to specify the entity class in the resultSetType, you only need to correspond to the resultMap with the above id, that is, goodsMap, to use this configuration.

    <select id="queryAll" resultMap="goodsMap" >
        select * from goods
    </select>

Guess you like

Origin blog.csdn.net/weixin_45925906/article/details/112661469