目录
在开发用,如果使用枚举而又没有在配置中指定类型,mybatis默认使用EnumTypeHandler。EnumTypeHandler和EnumOrdinalTypeHandler区别主要在于数据库存取类型不一样。
1.声明一个性别枚举
public enum SexEnum {
MALE(1, "男"),
FEMALE(0, "女");
private int id;
private String name;
//get,set方法省略...
2.创建User试例
public class User {
private Long id;
private String userName;
private String password;
private SexEnum sex;
private String mobile;
private String tel;
private String email;
private String note;
/** setter and getter **/
3.EnumOrdinalTypeHandler
配置xml文件使用EnumOrdinalHandler测试
<?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="mybatis02.mapper.UserMapper">
<resultMap id="userMapper" type="user">
<result property="id" column="id" />
<result property="userName" column="user_name" />
<result property="password" column="password" />
<result property="sex" column="sex"
typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
<result property="mobile" column="mobile" />
<result property="tel" column="tel" />
<result property="email" column="email" />
<result property="note" column="note" />
</resultMap>
<select id="getUser" resultMap="userMapper" parameterType="long">
select id, user_name, password, sex, mobile, tel, email, note from t_user
where id = #{id}
</select>
</mapper>
建表语句和数据
create table t_user
(
id int(12) not null auto_increment,
user_name varchar(60) not null,
password varchar(60) not null,
sex varchar(1) not null,
mobile varchar(20) not null,
tel varchar(20),
email varchar(60),
note varchar(512),
primary key (id)
);
insert into t_user(user_name, `password`, sex, mobile, tel, email, note)
values('user_name_1', 'pwd1', 1, '13888888888', '010-88888888', 'y666@163.com', 'note_1');
测试案列
private static void testTypeHandler() {
Logger log = Logger.getLogger(Chapter4Main.class);
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUser(1L);
log.debug("enum"+user.getSex());
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
输出结果
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'sex' from result set. Cause: java.lang.NumberFormatException: For input string: "MALE"
### The error may exist in mybatis02/mapper/UserMapper.xml
### The error may involve mybatis02.mapper.UserMapper.getUser
### The error occurred while handling results
### SQL: select id, user_name, password, sex, mobile, tel, email, note from t_user where id = ?
### Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'sex' from result set. Cause: java.lang.NumberFormatException: For input string: "MALE"
原因:在使用EnumOrdinalTypeHandler时,mybatis会要求数据库返回一个整数,把这个整数作为其下标,在根据下标找到Enum对应的类型
按照现有配置,如果查询第二条数据,则不会报错,有兴趣的可以自己拿下代码去试试
4.EnumTypeHandler
EnumTypeHandle是根据数据库返回的名称和枚举进行对应,例如MALE,会进行Enum.valueOf(“MALE”)
SexEnum sexEnum=SexEnum.valueOf("MALE");
之后将xml文件中的typeHandler换成EnumTypeHandler
之后测试就没问题了
两者区别:
EnumTypeHandler:要求数据库返回一个String类型的名称,根据名称去匹配枚举
EnumOrdianlTypeHandler:要求数据返回一个整数作为下标,根据下标匹配枚举