Mybatis-- entity class attribute names and database field names are different solutions

When using Mybatis to persist database, sometimes encountered inconsistencies property name database tables and fields in the entity class java, in this case Mybatis is unable to complete the field of automatic mapping. And usually, the entity class and the database should not be changed to. So to solve this problem without changing the database and the entity class, the following is to solve the problem in three ways:

java entity classes:

public  class the User 
{ 
    Private Long ID;
     Private String the userName;
     Private String passWord,;
     / ** 
     * ... 
     * GET, SET method is no longer listed 
     * ... 
     * * / 
}

 

1. Since it is because of inconsistencies caused Mybatis name can not be completed automatically mapped, then you can find ways to be both consistent name, individual name for the query from the database fields can, examples are as follows:

<the SELECT the above mentioned id = "selectUserById" resultType = "the User"> 
        the SELECT 
        the above mentioned id, 
        user_name AS userName, <-! do not care about the case, Mybatis will first be converted to uppercase and then match -> 
        user_password AS the userPassword, 
        from the User 
        the WHERE the above mentioned id = {ID} #
 </ SELECT>

 


2. Another way is to resolve inconsistencies name specified in advance mapping relationships, so Mybatis can automatically complete the mapping. Providing Mybatis resultMap can specify the mapping relationship, for example:

  type attribute: Specifies the fully qualified class name of the entity class
  id attribute: given a unique identifier, a reference to the query select the label used.
<resultMap type="User" id="UserResultMap">
      label id: specifies the primary key field
      Tags result: specifies the non-primary key field
      column attribute: used to specify database column names
      property attributes: name attribute specifies the entity class
        <id column="id" property="id"/>
        <result column="user_name" property="userName"/>
        <result column="user_password" property="userPassword"/>
</resultMap>
    
    
<select id="selectUserById" resultMap="UserResultMap">
        select 
        id,
        user_name,
        user_password,
        from user
        where id = #{id}
</select>

 


Note:
1. resultMap used, the configuration select statement, must replace the original resultMap resultType.

The 2.resultMap column to be consistent with the query to the field name, property must be consistent with the properties of the entity class.

3. Under normal circumstances, the entity class java property is generally used in camelCase named fields and tables in the database are underlined to distinguish between letters. In this case, Mybatis provides a global property mapUnderscoreToCamelCase to solve the problems of both the name inconsistent.

<Settings> 
    <-! Other configurations ... -> 
    <Setting name = "mapUnderscoreToCamelCase" value = "to true" /> 
    <-! Other configurations ... -> 
</ Settings>

 

Note: Because the property is a global property, so you need to configure Mybatis configuration file, rather than Mapper.xml mapping file.

Guess you like

Origin www.cnblogs.com/yscec/p/12041747.html