3

I need some clarification on the following as I just took over an old j2Ee project and am studying the codes, plus I had no experience in ibatis. Sorry for the noob question but I searched for 2 days with no answers.

Example: I have a pojo class :

public class Document
{
          private int _id;
          private string _title;
          ..........
}

DB Table :

CREATE TABLE [Documents] (
       [Document_ID] [int] NOT NULL ,
       [Document_Title] [varchar] (32) NULL ,
)

Mapping :

<resultMap id="document" class="Document">
     <result property="id" column="Document_ID"/>
     <result property="title" column="Document_Title"/>
</resultMap>

Question :

I noticed that in the declaration of the id and title variables(in java),there was a underscore in front(I have instances where the underscore was behind, eg.title) but in the resultmap, the underscore is not there. I'm curiuos how it's Mapping is done since the 2 doesn't match exactly.

Thanks for your guidance.

DanialChan
  • 100
  • 7

1 Answers1

4

MyBatis is a fork from iBATIS.

In simple cases MyBatis can auto-map the results for you and in others you will need to build a result map. But you can also mix both strategies. Let's have a deeper look at how auto-mapping works.

When auto-mapping results MyBatis will get the column name and look for a property with the same name ignoring case. That means that if a column named ID and property named id are found, MyBatis will set the id property with the ID column value.

Usually database columns are named using uppercase letters & underscores between words java properties often follow the camel-case naming convention. To enable the auto-mapping between them set the setting mapUnderscoreToCamelCase to true.

Auto-mapping works even when there is an specific result map. When this happens, for each result map, all columns that are present in the ResultSet that have not a manual mapping will be auto-mapped, then manual mappings will be processed.

In the following sample id & userName columns will be auto-mapped and hashed_password column will be mapped.

<select id="selectUsers" resultType="User">
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password
  from some_table
  where id = #{id}
</select>

<resultMap id="userResultMap" type="User">
  <result property="password" column="hashed_password"/>
</resultMap>

There are three auto-mapping levels:

NONE - disables auto-mapping. Only manually mapped properties will be set.
PARTIAL - will auto-map results except those that have nested result mappings defined inside (joins).
FULL - auto-maps everything.

FYIP refer How Auto-Mapping Works

Community
  • 1
  • 1
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68