Three processing methods when the field names in the database and the attribute names in the entity class cannot correspond one-to-one

When the column name of the query result does not correspond to the attribute name of the java object, the following methods need to be used for processing:

        The first way: use the keyword "as" in the query to alias the column

        The second way: use resultMap result mapping

        The third way: Turn on the automatic mapping of hump naming (configuration settings)

Entity class Car:

package com.bjpowernode.domain;

public class Car {

    private Integer id;

    private String carNum;

    private String brand;

    private Double guidePrice;

    private String produceTime;

    private String carType;

//The construction method is abbreviated

//setter and getter 略

//toString slightly

The t_car table in the database:

We can see that the attribute names in the entity class are not exactly the same as the field names in the database table, and need to be processed. The three processing methods are as follows:

The first method: use the keyword "as" to alias the column in the query statement , and the alias must be the attribute name of the entity class

<select id="selectById" resultType="com.powernode.mybatis.pojo.Car">

        select

            id,

            car_num as carNum,

            brand,

            guide_price as guidePrice,

            produce_time as produceTime,

            car_type as carType

        from

            t_car

        where

            id = #{id}

    </select>

The second method: use resultMap result mapping, as follows:

<!--

 resultMap:

       id: is the unique identifier of this result mapping,

       type: The class to which the result set is mapped (that is, the corresponding entity class). Aliases can be used.

-->

<resultMap id="carResultMap" type="car">

         <!-- id is the unique identifier of the object, in order to improve the performance of mybatis. It is recommended to write on it. -->

         <id property="id" column="id"/>

         <result property="carNum" column="car_num"/>

         <!--When the attribute name is consistent with the database column name, it can be omitted. But write it down. -->

         <!--javaType is used to specify the attribute type. jdbcType is used to specify the column type. Generally can be omitted. -->

         <result property="brand" column="brand" javaType="string" jdbcType="VARCHAR"/>

         <result property="guidePrice" column="guide_price"/>

         <result property="produceTime" column="produce_time"/>

         <result property="carType" column="car_type"/>

</resultMap>

<!--The value of the resultMap attribute must be consistent with the value of the id attribute in the resultMap tag. -->

<select id="selectAllByResultMap" resultMap="carResultMap">

        select * from t_car

</select>

The third way: Turn on the automatic mapping of hump naming (configuration settings)

The prerequisites for using this method are:

        Attribute names follow Java naming conventions, and database table column names follow SQL naming conventions.

Java naming convention:

        The first letter is capitalized, and the first letter of each subsequent word is capitalized, following the hump naming convention.

SQL naming convention:

        All lowercase, words are separated by underscores.

For example, the following correspondences:

        Attribute names in entity classes Column names in database tables

                carNum car_num

                carType                                         car_type

                produceTime                                 produce_time

To enable this function, you need to configure the following in the mybatis-config.xml file:

<!--After the properties tag -->

<settings>

       <setting name="mapUnderscoreToCamelCase" value="true"/>

</settings>

Guess you like

Origin blog.csdn.net/heliuerya/article/details/131299340