Mybatis查询语句条件为枚举类型报错
通常我们对于数据库中一些枚举字段使用tinyInt类型,而java对象对应的字段很多时候会为了方便定义成short或者int。但这样显然不美观方便,让后面维护的人抠破脑袋找你的常量定义在哪儿,要是没有注释简直让人崩溃。时间久后,没有人知道这里面的值。只能一行行读源码。
优雅的程序员当然想到了优雅的枚举,而mybatis“强大”的枚举类型处理器EnumOrdinalTypeHandler相信都不陌生。
然而配置枚举处理器花了九牛二虎之力改好原来的mapper运行测试用例全在报错。而插入、部分查询却没报错。这时进程进行到一半让人崩溃想要放弃。
通常这个错误是
"failed to invoke constructor for handler class org.apache.ibatis.type.EnumOrdinalTypeHandler”
原因是因为该死的查询条件使用枚举对象作为条件,无论你用selectExample还是其他的select,当条件where enum = #{enum}时就会报错。不要怀疑自己是不是EnumOrdinalTypeHandler没配对,如果没配对那一定会是所有的查询接口都会报错。
stackoverflow上只有一条相关问题。为什么这么少?这不是很常见的错误吗?jpa或hibernate就能很优雅的使用枚举啊。原因嘛,老外们很少用半自动的mybatis框架。只有国内奉为圭臬,原因嘛当然是听说人家阿里就用mybatis,所以一定是好的。也不看自己的业务到底是否真正触及到要提升sql性能的地步。
话说回来,目前给出来的答案似乎是mybatis的bug,但对于mybatis这种半自动框架这不一定是bug。
解决办法很简单粗暴,把where enum = #{enum}条件换成where enum in (***)万事大吉。但熟悉的同学已经发现了。这样的性能显然不如=。用short和int的同学肯定又开心了。看吧我就说数据库什么类型就用什么类型,枚举就是垃圾。说这话的同学显然还不习惯封装、规范这一套,更喜欢随心所欲的感觉。
今天的教训就到这。
Mybatis处理枚举类型
1、枚举
- package com.ahut.core.enums;
- import java.util.HashMap;
- import java.util.Map;
- /**
- *
- * @ClassName: SexEnum
- * @Description: 性别枚举
- * @author cheng
- * @date 2017年11月20日 下午8:32:27
- */
- public enum SexEnum {
- MAN("1", "男"), WOMAN("2", "女");
- private String key;
- private String value;
- private static Map<String, SexEnum> sexEnumMap = new HashMap<>();
- static {
- for (SexEnum sexEnum : SexEnum.values()) {
- sexEnumMap.put(sexEnum.getKey(), sexEnum);
- }
- }
- /**
- * 私有化构造函数
- *
- * @param key
- * @param value
- */
- private SexEnum(String key, String value) {
- this.key = key;
- this.value = value;
- }
- /**
- *
- * @Title: getSexEnumByKey
- * @Description: 依据key获取枚举
- * @param key
- * @return
- */
- public static SexEnum getSexEnumByKey(String key) {
- return sexEnumMap.get(key);
- }
- public String getKey() {
- return key;
- }
- public void setKey(String key) {
- this.key = key;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- }
2、包含枚举的实体类
- package com.ahut.entity;
- import java.io.Serializable;
- import java.util.Date;
- import com.ahut.core.enums.SexEnum;
- /**
- *
- * @ClassName: Demo
- * @Description:
- * @author cheng
- * @date 2017年11月21日 下午8:32:59
- */
- public class Demo implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 4122974131420281791L;
- private Date birthDay;
- private String userName;
- private int age;
- private String id;
- private SexEnum sex;
- public Demo() {
- super();
- // TODO Auto-generated constructor stub
- }
- @Override
- public String toString() {
- return "Demo [id=" + id + ", userName=" + userName + ", age=" + age + ", birthDay=" + birthDay + ", sex=" + sex
- + "]";
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public Date getBirthDay() {
- return birthDay;
- }
- public void setBirthDay(Date birthDay) {
- this.birthDay = birthDay;
- }
- public SexEnum getSex() {
- return sex;
- }
- public void setSex(SexEnum sex) {
- this.sex = sex;
- }
- }
3、书写枚举处理器
- package com.ahut.handler;
- import java.sql.CallableStatement;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.apache.ibatis.type.BaseTypeHandler;
- import org.apache.ibatis.type.JdbcType;
- import com.ahut.core.enums.SexEnum;
- /**
- *
- * @ClassName: EnumHandler
- * @Description:
- * @author cheng
- * @date 2017年11月20日 下午8:41:12
- */
- public class SexEnumHandler extends BaseTypeHandler<SexEnum> {
- /**
- * 用于定义设置参数时,该如何把Java类型的参数转换为对应的数据库类型
- */
- @Override
- public void setNonNullParameter(PreparedStatement ps, int i, SexEnum parameter, JdbcType jdbcType)
- throws SQLException {
- // baseTypeHandler已经帮我们做了parameter的null判断
- // 第二个参数 : 存入到数据库中的值
- ps.setString(i, parameter.getKey());
- }
- /**
- * 用于定义通过字段名称获取字段数据时,如何把数据库类型转换为对应的Java类型
- */
- @Override
- public SexEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
- System.out.println("columnName执行我");
- // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型
- String key = rs.getString(columnName);
- if (rs.wasNull()) {
- return null;
- } else {
- // 根据数据库中的key值,定位SexEnum子类
- return SexEnum.getSexEnumByKey(key);
- }
- }
- /**
- * 用于定义通过字段索引获取字段数据时,如何把数据库类型转换为对应的Java类型
- */
- @Override
- public SexEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
- System.out.println("columnIndex执行我");
- // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型
- String key = rs.getString(columnIndex);
- if (rs.wasNull()) {
- return null;
- } else {
- // 根据数据库中的key值,定位SexEnum子类
- return SexEnum.getSexEnumByKey(key);
- }
- }
- /**
- * 用定义调用存储过程后,如何把数据库类型转换为对应的Java类型
- */
- @Override
- public SexEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
- // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型
- String key = cs.getString(columnIndex);
- if (cs.wasNull()) {
- return null;
- } else {
- // 根据数据库中的key值,定位SexEnum子类
- return SexEnum.getSexEnumByKey(key);
- }
- }
- }
4、配置枚举处理器
mybatis配置
- <?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>
- <settings>
- <!-- 打印sql语句 -->
- <setting name="logImpl" value="STDOUT_LOGGING" />
- </settings>
- <typeHandlers>
- <typeHandler handler="com.ahut.handler.SexEnumHandler"
- javaType="com.ahut.core.enums.SexEnum" jdbcType="CHAR" />
- </typeHandlers>
- </configuration>
5、dao层
- package com.ahut.mapper;
- import java.util.List;
- import java.util.Map;
- import com.ahut.entity.Demo;
- /**
- *
- * @ClassName: DemoMapper
- * @Description:
- * @author cheng
- * @date 2017年11月16日 下午9:10:38
- */
- public interface DemoMapper {
- /**
- *
- * @Title: saveDemo
- * @Description: 保存
- * @param map
- * @throws Exception
- */
- void saveDemo(Map<String, Object> map) throws Exception;
- /**
- *
- * @Title: selectDemoList
- * @Description: 查询
- * @return
- * @throws Exception
- */
- List<Map<String, Object>> selectDemoList() throws Exception;
- /**
- *
- * @Title: selectDemoList1
- * @Description: 查询
- * @return
- * @throws Exception
- */
- List<Demo> selectDemoList1() throws Exception;
- }
6、mapper文件
- <?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="com.ahut.mapper.DemoMapper">
- <!-- 保存 -->
- <insert id="saveDemo" parameterType="map">
- INSERT INTO DEMO
- VALUES(replace(UUID(),'-',''),#{USER_NAME},#{AGE},#{BIRTH_DAY},#{SEX})
- </insert>
- <!-- 查询 -->
- <select id="selectDemoList" resultType="map">
- SELECT
- ID,
- USER_NAME,
- AGE,
- BIRTH_DAY,
- SEX
- FROM DEMO
- </select>
- <!-- 查询 -->
- <select id="selectDemoList1" resultType="com.ahut.entity.Demo">
- SELECT
- ID,
- USER_NAME USERNAME,
- AGE,
- BIRTH_DAY BIRTHDAY,
- SEX
- FROM DEMO
- </select>
- </mapper>
7、测试
- package com.ahut.service;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import com.ahut.core.enums.SexEnum;
- import com.ahut.entity.Demo;
- /**
- *
- * @ClassName: DemoServiceTest
- * @Description:
- * @author cheng
- * @date 2017年11月16日 下午9:28:56
- */
- @SpringBootTest
- @RunWith(SpringRunner.class)
- public class DemoServiceTest {
- @Autowired
- private DemoService demoService;
- /**
- *
- * @Title: testSelectDemoList1
- * @Description:
- * @throws Exception
- */
- @Test
- public void testSelectDemoList1() throws Exception {
- List<Demo> demoList = demoService.selectDemoList1();
- for (Demo demo : demoList) {
- System.out.println(demo);
- }
- }
- /**
- *
- * @Title: testSelectDemoList
- * @Description:
- * @throws Exception
- */
- @Test
- public void testSelectDemoList() throws Exception {
- List<Map<String, Object>> demoList = demoService.selectDemoList();
- for (Map<String, Object> map : demoList) {
- for (String key : map.keySet()) {
- if (key.equals("BIRTH_DAY")) {
- Date birthDay = (Date) map.get(key);
- System.out.println(key + ":" + birthDay);
- } else if (key.equals("AGE")) {
- int age = (int) map.get(key);
- System.out.println(key + ":" + age);
- } else if (key.equals("SEX")) {
- SexEnum sex = (SexEnum) map.get(key);
- System.out.println(key + ":" + sex);
- } else {
- String value = (String) map.get(key);
- System.out.println(key + ":" + value);
- }
- }
- }
- }
- /**
- *
- * @Title: testSaveDemo
- * @Description:
- * @throws Exception
- */
- @Test
- public void testSaveDemo() throws Exception {
- Map<String, Object> map = new HashMap<>();
- map.put("USER_NAME", "rick11");
- map.put("AGE", 22);
- map.put("BIRTH_DAY", new Date());
- map.put("SEX", SexEnum.WOMAN);
- demoService.saveDemo(map);
- }
- }
执行testSaveDemo方法:
SexEnum.WOMAN被转换成了2存入到数据库中

执行testSelectDemoList1方法:
数据库中的1、2成功被转换成了枚举
当resultType为包含枚举的实体类时,mybatis调用了枚举处理器


执行testSelectDemoList方法:
报错
由下图可知,resultType为map时,并没有调用枚举处理器


以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。