uuid custom typehandler mybatis(uuid custom typehandler mybatis)

我想管理一个带有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永远不会被调用(我有点记录,但从不打印任何东西)。 有什么不对? 谢谢。

I want to manage a table with a VARCHAR primary key, that in the mapped java object should be a UUID.

i have my 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>

and the user.xml is like that:

<?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>

anyway, i got this exception:

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'

it seems that my typehandler never gets called (i have it logging a bit, but never prints anything). Is there something wrong? Thanks.

最满意答案

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

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

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

Seems kinda odd answering my own question, but i got some help on the mybatis-users list, so i'd like to share some hints here:

Mybatis was trying to "get" a non-existing field: Luckily, one of mybatis developers helped me a while ago, suggesting that the easiest way to get it to work was to add a @Param annotation in the UserMapper to do this thing:

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>

I was stuck in watching the typehandler configuration, but, if I understood it right, this has not much to do with typehandlers, since they are used to "transport and translate" data between the sql table and the java objects (through setter and getters), while my issue was in the handling of methods' parameters. Hope this helps someone.

uuid custom typehandler mybatis(uuid custom typehandler mybatis)

我想管理一个带有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永远不会被调用(我有点记录,但从不打印任何东西)。 有什么不对? 谢谢。

I want to manage a table with a VARCHAR primary key, that in the mapped java object should be a UUID.

i have my 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>

and the user.xml is like that:

<?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>

anyway, i got this exception:

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'

it seems that my typehandler never gets called (i have it logging a bit, but never prints anything). Is there something wrong? Thanks.

最满意答案

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

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

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

Seems kinda odd answering my own question, but i got some help on the mybatis-users list, so i'd like to share some hints here:

Mybatis was trying to "get" a non-existing field: Luckily, one of mybatis developers helped me a while ago, suggesting that the easiest way to get it to work was to add a @Param annotation in the UserMapper to do this thing:

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>

I was stuck in watching the typehandler configuration, but, if I understood it right, this has not much to do with typehandlers, since they are used to "transport and translate" data between the sql table and the java objects (through setter and getters), while my issue was in the handling of methods' parameters. Hope this helps someone.