uuid custom typehandler mybatis

时间:2011-02-18 12:51:51

标签: mybatis typehandler

我想用一个VARCHAR主键来管理一个表 映射的java对象应该是UUID。

我有sql-map-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
       <properties resource="database.properties"/>
   <typeHandlers>
       <typeHandler
handler="[...].persistence.typehandlers.UuidTypeHandler"
                    javaType="java.util.UUID"
                    jdbcType="VARCHAR"/>
   </typeHandlers>
       <environments default="development">
               <environment id="development">
                       <transactionManager type="JDBC" />
                       <dataSource type="POOLED">
                               <property name="driver" value="${driver}" />
                               <property name="url" value="${url}" />
                               <property name="username" value="${username}" />
                               <property name="password" value="${password}" />
                       </dataSource>
               </environment>
       </environments>
       <mappers>
               <mapper resource="user.xml" />
   </mappers>
</configuration>

user.xml就是这样:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="[...].persistence.mappers.UserMapper">

   <select id="selectUserByUUID" parameterType="java.util.UUID"
resultMap="userResultMap">
       SELECT * FROM user WHERE uuid = #{uuid, jdbcType=VARCHAR,
typeHandler=[...].persistence.typehandlers.UuidTypeHandler}
   </select>
   <resultMap id="userResultMap" type="[...].model.User">
       <id property="uuid" column="uuid" jdbcType="OTHER"
typeHandler="[...].persistence.typehandlers.UuidTypeHandler"/>
       ...
   </resultMap>
</mapper>

无论如何,我得到了这个例外:

org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named 'uuid' in 'class java.util.UUID'
### The error may involve
[...].persistence.mappers.UserMapper.selectUserByUUID-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named 'uuid' in 'class java.util.UUID'

似乎我的typehandler永远不会被调用(我记录了它 有点,但从不打印任何东西)。 有什么不对?感谢。

3 个答案:

答案 0 :(得分:2)

你的问题就在你的例外....

There is no getter for property named 'uuid' in 'class java.util.UUID'

使用String的参数类型,并传入唯一ID作为参数。您不需要类型处理程序。

答案 1 :(得分:1)

似乎有点奇怪回答我自己的问题,但我在mybatis-users列表上得到了一些帮助,所以我想在这里分享一些提示:

Mybatis试图“获取”一个不存在的字段: 幸运的是,其中一位mybatis开发人员在一段时间前帮助了我,建议最简单的方法就是在UserMapper中添加@Param注释来做这件事:

public User selectUserByUUID(@Param("uuid") UUID uuid);

<select id="selectUserByUUID" parameterType="uuid" resultMap="userResultMap"> 
SELECT * FROM user WHERE uuid = #{uuid, typeHandler=com.collective.persistence.typehandlers.UuidTypeHandler, javaType=uuid, jdbcType=VARCHAR}
</select> 

我一直在看着打字机配置,但是, 如果我理解正确,这与打字机没什么关系, 因为它们用于在sql之间“传输和转换”数据 表和java对象(通过setter和getters),而我的 问题在于处理方法的参数。希望这有助于某人。

答案 2 :(得分:1)

此外,可以通过在mapper文件中添加具有相应队列的新方法来修复此类错误 Smth喜欢这样:

@Select("SELECT * FROM Users u WHERE u.userUUID = '${uuid}'")
User selectByUserUuid(@Param("uuid") UUID uuid);