The mismatch between entity class attribute name and field name in database in MyBatis development

Posted by rayner75 on Fri, 24 Dec 2021 14:43:10 +0100

In the development, we often encounter that the entity class attributes are named by hump, and the underscores are used in the database_ As a result, the attribute name in the entity class does not correspond to the field name in the database:

In java code, we usually use hump naming

public class Staff {
    private Integer id;
    private String name;
    private String gender;
    private String salary;
    private Date joinDate;
    private Integer depId;
    ......
    }

There are two cases: development based on xml configuration file and annotation
1: xml based configuration file
1. Alias sql so that the queried fields are the same as the attribute names in the entity class:

    <select id="showAll" resultType="Staff">
        select id,name,gender,salary,join_date as joinDate,dep_id as depId from emp;
    </select>

The operation results are normal, but this method is extremely inconvenient. You can use sql tags for optimization:

    <sql id="all">
        id,name,gender,salary,join_date as joinDate,dep_id as depId
    </sql>
    <select id="showAll" resultType="Staff">
        select <include refid="all"></include> from emp;
    </select>

Although the sql tag can be introduced each time to improve the reusability, it is still very inconvenient
2. Use resultMap:
We can define a resultMap tag in the mapper file. Just like the tag name, it is like a map, which corresponds the field name in the database to the attribute name in the entity class one by one, and specifies the returned data type, such as the resultType of the function upgrade

    <resultMap id="resultStaff" type="Staff">
        <result column="NAME" property="name"/>
        <result column="join_date" property="joinDate"/>
        <result column="dep_id" property="depId"/>
    </resultMap>

In subsequent codes, as long as this type is involved, it can be called through the id of resultMap:

    <select id="showAll" resultMap="resultStaff">
        select * from emp;
    </select>

2: MyBatis development based on annotation:
Using annotation development can make our development more convenient and suitable for some simple sql statements
Solving the problem that attribute names do not correspond to field names in the database is also the same as developing based on xml:
1. Alias sql statements

    @Select("select id,name,gender,salary,join_date as joinDate,dep_id as depId from emp")
    List<Staff> showAll();

2. Use @ ResultMap:
Based on annotation development, what if there is no resultMap tag in xml? MyBatis provides us with the annotation @ Results,@Result,@ResultMap to replace < resultMap >, < result >, < select id = "showall" resultMap = "Id" >
The specific implementation method is also well understood:

    @Select("select * from emp")
    @Results({
            @Result(column="join_date",property="joinDate"),
            @Result(column="dep_id",property="depId")
    })
    List<Staff> showAll();

But this only solves the problem of the current method. How to reuse the later code? Like the < resultmap > tag, @ Results also has an id attribute. The following code can reuse the code through @ ResultMap("id"):

    @Select("select * from emp")
    @Results(id = "reStaff",value={
            @Result(column="join_date",property="joinDate"),
            @Result(column="dep_id",property="depId")
    })
    List<Staff> showAll();

    @Select("select * from emp where id=#{id}")
    @ResultMap("reStaff")  //Just use @ ResultMap to import directly here~
    Staff selectById(int id);

Topics: Java Mybatis IDEA