I will explain the mechanism (mapping) that associates the table column name with the field name used in the code in MyBatis.
There are four types of mapping methods. In the field, you will choose one of them.
The standard setting of MyBatis is "Ignore the difference between lowercase and uppercase letters and associate them".
The following examples are automatically associated and operate.
Example: (table column name) A_COLUMN → (field name) a_column or a_Column
When writing an SQL statement, describe the correspondence in the AS clause.
SQL statement
SELECT
A_COLUMN AS "aColumn",
B_COLUMN AS "bColumn",
FROM
MY_TABLE
WHERE
A_COLUMN = "test"
Field name
Public Class myTableDto {
String aColumn;
String bColumn;
}
In the config file (eg mybatis-config.xml)
mybatis-config.xml
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
If you specify, the column name with underscore will automatically correspond to camel case (capitalize the beginning of the second word).
Example: (table column name) ADD_COLUMN → (field name) @Column
In the Mapper file called from the config file, use the resultMap tag to describe the correspondence.
testMapper.xml
<mapper namespace="org.apache.ibatis.example.Mapper">
<resultMap type="org.apache.ibatis.example.dto.myTableDto" id="testmap">
<result column="A_COLUMN" property="aColumn"/>
<result column="B_COLUMN" property="bColumn"/>
</resultMap>
<select id="getAll" resultMap="testmap">
SELECT * FROM MY_TABLE
</select>
</mapper>
Personally, I might like to write in the AS clause, which makes it easy to see if there are any omissions when looking at the SQL statement.
Recommended Posts