Four ways to map between entity class attributes and data columns in Mybatis

Mybatis is not as automated as Hibernate. It uses @Columnannotations or directly uses the attribute name of the entity class as the data column name, but needs to specify 
the mapping relationship between the entity class attribute and the column name in the data table, which makes people used to Hibernate. People are not used to it, but fortunately, through exploration, we found three ways to establish a mapping relationship, among which there are always relatively 
simple ones.

First define an entity class as follows:

public class User implements Serializable {
    private Integer userId;
    private String userName;
    ......
}

1. resultMapThis way of mapping files through XML is the most common, similar to the following:

<mapper namespace="data.UserMapper">
    <resultMap type="data.User" id="userResultMap">
        <!-- 用id属性来映射主键字段 -->
        <id property="id" column="user_id"/>
        <!-- 用result属性来映射非主键字段 -->
        <result property="userName" column="user_name"/>
    </resultMap>
</mapper>

The mapping relationship is established through the idlabels and tags inside, and the entity class attributes and the column names of the data table are specified resultby propertyand respectively.column

2. By annotation @Resultsand@Result

These two annotations correspond to tags in the XML file:

  • @ResultscorrespondresultMap
  • @Resultcorrespondresult

These two annotations are applied at the method level , that is, on the mapper method, as follows:

@Select("select * from t_user where user_name = #{userName}")
@Results(
        @Result(property = "userId", column = "user_id"),
        @Result(property = "userName", column = "user_name")
)
User getUserByName(@Param("userName") String userName);

shortcoming:

  • Since annotations are for methods, each method in Mapper that operates on the database must have the same annotations to complete the establishment of the mapping relationship, resulting in repeated configurations;
  • If you want to avoid the problem of configuration duplication, you can configure this in the XML configuration file resultMap, and then @Resultrefer to this through the id attribute resultMap
    but this feels very troublesome (due to the use of two configuration methods), it is better to use the XML-based resultMapconfiguration method directly ;

3. Complete the mapping through attribute configuration

The most unfamiliar to users is to complete the mapping by configuring attributes. Mybatis provides us with a mapping method. If the naming of attributes follows the camel case nomenclature and the data column names follow the underscore naming, 
then this method can be used, similar to the following :

  • userName对应user_name;
  • userId corresponds to user_id;

配置代码如下:

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
Configuration configuration = new Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);

4. 通过使用在SQL语句中定义别名完成映射

这种方式最直接,直接在SQL语句中建立别名来完成映射,如下:

@Select("select user_name as userName, user_id as userId from t_user where user_name = #{userName}")
User getUserByName(@Param("userName") String userName);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326263877&siteId=291194637