Using MyBatis 3.2.8, I'm trying to map an enum type (Status) to the jdbc VARCHAR type (to can use only the enum in my entity bean). So I defined the TypeHandler UserStatusHandler
import com.sample.User.Status;
import org.apache.ibatis.type.EnumTypeHandler;
public class UserStatusHandler extends EnumTypeHandler<Status>
{
public UserStatusHandler(Class<Status> type)
{
super(type);
}
}
I correctly declared the handler in the xml config file and in the UserDao.xml (mapping the attribute Status to the VARCHAR in the resultMap ...) sample :
In the XML config file:
<typeHandlers>
<typeHandler handler="com.sample.dao.UserStatusHandler" javaType="com.sample.User.Status"/>
</typeHandlers>
In the DAO mapper XML file:
<resultMap id="UserResultMap" type="User">
<id property="id" column="ID" javaType="long"/>
<result property="status" column="STATUS" typeHandler="com.sample.dao.UserStatusHandler" javaType="com.sample.User.Status"/>
xxxxx
</resultMap>
But the problem incoming from MyBatis was that MyBatis cannot found my java enum class because it is defined inside another interface
public interface User
{
public enum Status
{
A, B, C
}
...
}
When I define this enum class in a separate file, it works with no problem, but I don't like to change my design (because of a limit ? ), I search to understand why MyBatis cannot found the class in this case ? Is it a way to fix this ?
MyBatis cannot build the SqlSession. While executing a simple test to find a User I get the following exception
Cause: org.apache.ibatis.builder.BuilderException:
Error parsing SQL Mapper Configuration. Cause:
org.apache.ibatis.builder.BuilderException: Error resolving class.
Cause: org.apache.ibatis.type.TypeException: Could not resolve type
alias 'com.sample.User.Status'.
Cause: java.lang.ClassNotFoundException: Cannot find class:
com.sample.User.Status
typeHandlers. Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type.
The resultMap element is the most important and powerful element in MyBatis. It's what allows you to do away with 90% of the code that JDBC requires to retrieve data from ResultSet s, and in some cases allows you to do things that JDBC does not even support.
It is the same thing, a persistence framework! But until June 2010, iBatis was under Apache license and since then, the framework founders decided to move it to Google Code and they renamed it to MyBatis. The framework is still the same though, it just has a different name now.
Finally I solved it by writing, in the XML configuration file, the name of the Inner enum like its associated compiled file name in the jar i.e. by adding a dollar $
between the enveloped and the inner class.
com.sample.User$Status
It appears a bug or a limit in MyBatis ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With