在MyBatis中,可以使用resultMap來進行結果映射和值轉換。MyBatis提供了以下幾種方式來進行值轉換:
示例:
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="email" column="email" typeHandler="com.example.EmailTypeHandler" />
</resultMap>
示例:
<resultMap id="userResultMap" type="User" typeHandlers="com.example.UserTypeHandler">
<id property="id" column="user_id" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="email" column="email" />
</resultMap>
示例:
@Results(id = "userResultMap", value = {
@Result(property = "id", column = "user_id", id = true),
@Result(property = "username", column = "username"),
@Result(property = "password", column = "password"),
@Result(property = "email", column = "email", typeHandler = EmailTypeHandler.class, javaType = Email.class,
options = { @Options(javaType = String.class, name = "value", typeHandler = EmailTypeHandler.class) })
})
@Select("SELECT * FROM users")
User getUser();
這些是MyBatis中進行值轉換的幾種常見方法,可以根據具體的需求選擇適合的方式進行值轉換。