mybatis中实现枚举自动转换方法详解

所属分类: 软件编程 / java 阅读数: 23
收藏 0 赞 0 分享

前言

最近在工作中遇到一个问题,在设计数据库的时候,我们有时候会把表里的某个字段的值设置为数字或者为英文来表示他的一些特殊含义。就拿设置成数字来说,假如1对应是学生,2对应是教师,在Java里面定义成这样的枚举,但是一般使用mybatis查出来的话,我们想要让它自动装换成我们想要的枚举,不需要再手动根据数值去判断设置成我们想要的枚举。要是实现这样的效果,那么我们就要用到mybatis的BaseTypeHandler了。

BaseTypeHandler介绍

让我们来看看要继承BaseTypeHandler这个抽象类,需要覆写哪些方法:

public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; 
 
public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException; 
 
public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException; 
 
public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException; 

实现了这些抽象类,当得到结果集的时候,程序就会回调这些方法,例如根据名称获取当前行的某一列的值,那么就会直接回调getNullableResult(ResultSet rs, String columnName)这个方法,根据名称得到当行的当前列的值,然后我们在这里去调用枚举,匹配枚举中的每一个值,相等的话直接返回该枚举,达到自动转换成我们想要的枚举的效果。其他的重载方法类似,只不过是有些根据列索引,有些根据列名称做枚举自动转换而已。

好了,介绍就到这里,让我们来看看具体实现。。

自动转换实现例子

创建数据库表


创建枚举

package net.itaem.less; 
 
import java.util.HashMap; 
import java.util.Map; 
 
/** 
 * @author: Fighter168 
 */ 
public enum PersonType{ 
 STUDENT("1","学生"), 
 TEACHER("2","教师"); 
 
 private String value; 
 private String displayName; 
 
 static Map<String,PersonType> enumMap=new HashMap<String, PersonType>(); 
 static{ 
 for(PersonType type:PersonType.values()){ 
  enumMap.put(type.getValue(), type); 
 } 
 } 
 
 private PersonType(String value,String displayName) { 
  this.value=value; 
  this.displayName=displayName; 
 } 
 
 public String getValue() { 
 return value; 
 } 
 public void setValue(String value) { 
 this.value = value; 
 } 
 public String getDisplayName() { 
 return displayName; 
 } 
 public void setDisplayName(String displayName) { 
 this.displayName = displayName; 
 } 
 
 public static PersonType getEnum(String value) { 
 return enumMap.get(value); 
 } 
} 

创建Po实体类

/** 
 * @author: Fighter168 
 */ 
public class Person { 
 private String id; 
 private String name; 
 //枚举 
 private PersonType personType; 
 //set get 方法。。 
} 

创建Dao接口

创建一个简单的测试dao,这里简单的提供一个测试的查询方法。

/** 
 * @author: Fighter168 
 */ 
public interface PersonDao { 
 
 public List<Person> query(); 
 
} 

创建枚举转换处理器

package net.itaem.handler; 
 
import java.sql.CallableStatement; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
 
import net.itaem.less.PersonType; 
 
import org.apache.ibatis.type.BaseTypeHandler; 
import org.apache.ibatis.type.JdbcType; 
 
/** 
 * @author: Fighter168 
 */ 
public class PersonTypeHandler extends BaseTypeHandler<PersonType>{ 
 
 private Class<PersonType> type; 
 
 private PersonType[] enums; 
 
 /** 
 * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现 
 * @param type 配置文件中设置的转换类 
 */ 
 public PersonTypeHandler(Class<PersonType> type) { 
 if (type == null) 
  throw new IllegalArgumentException("Type argument cannot be null"); 
 this.type = type; 
 this.enums = type.getEnumConstants(); 
 if (this.enums == null) 
  throw new IllegalArgumentException(type.getSimpleName() 
   + " does not represent an enum type."); 
 } 
 
 @Override 
 public PersonType getNullableResult(ResultSet rs, String columnName) throws SQLException { 
 // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型 
 String i = rs.getString(columnName); 
 if (rs.wasNull()) { 
  return null; 
 } else { 
  // 根据数据库中的value值,定位PersonType子类 
  return PersonType.getEnum(i); 
 } 
 } 
 
 @Override 
 public PersonType getNullableResult(ResultSet rs, int columnIndex) throws SQLException { 
 // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型 
  String i = rs.getString(columnIndex); 
 if (rs.wasNull()) { 
  return null; 
 } else { 
  // 根据数据库中的value值,定位PersonType子类 
  return PersonType.getEnum(i); 
 } 
 } 
 
 @Override 
 public PersonType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { 
  // 根据数据库存储类型决定获取类型,本例子中数据库中存放String类型 
 String i = cs.getString(columnIndex); 
 if (cs.wasNull()) { 
  return null; 
 } else { 
  // 根据数据库中的value值,定位PersonType子类 
  return PersonType.getEnum(i); 
 } 
 } 
 
 @Override 
 public void setNonNullParameter(PreparedStatement ps, int i, PersonType parameter, JdbcType jdbcType) 
  throws SQLException { 
 // baseTypeHandler已经帮我们做了parameter的null判断 
 ps.setString(i, parameter.getValue()); 
 
 } 
 
} 

创建Mapper映射文件

PersonDao对应的PersonMapper映射文件

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd" > 
<mapper namespace="net.itaem.dao.PersonDao" > 
 
 <resultMap id="resultMap" type="net.itaem.po.Person" > 
 <result column="id" property="id" jdbcType="CHAR" /> 
 <result column="name" property="name" jdbcType="CHAR" /> 
 <result column="type" property="personType" jdbcType="CHAR" /> 
 </resultMap> 
 
 <select id="query" resultMap="resultMap"> 
 select * from person 
 </select> 
 
</mapper> 

其实handler还可以写在PersonMapper.xml这里,写成下面这样:

<result column="type" property="personType" typeHandler="net.itaem.handler.PersonTypeHandler"/> 

创建Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
  <property name="url" value="jdbc:mysql://localhost:3306/test"/> 
  <property name="username" value="root"/> 
  <property name="password" value="123abc"/> 
  <!-- 连接池启动时候的初始连接数 --> 
  <property name="initialSize" value="10"/> 
  <!-- 最小空闲值 --> 
  <property name="minIdle" value="5"/> 
  <!-- 最大空闲值 --> 
  <property name="maxIdle" value="20"/> 
  <property name="maxWait" value="2000"/> 
  <!-- 连接池最大值 --> 
  <property name="maxActive" value="50"/> 
  <property name="logAbandoned" value="true"/> 
  <property name="removeAbandoned" value="true"/> 
  <property name="removeAbandonedTimeout" value="180"/> 
 </bean> 
 
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
 <property name="configLocation" value="classpath:/resource/cfg.xml"/> 
 <property name="dataSource" ref="dataSource"/> 
 </bean> 
 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
  <property name="basePackage" value="net.itaem.dao"/> 
 </bean> 
</beans> 

创建mybatis的配置文件

下面是为mybatis创建配置文件cfg.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> 
 <typeHandlers> 
 <typeHandler handler="net.itaem.handler.PersonTypeHandler" 
  javaType="net.itaem.less.PersonType" jdbcType="CHAR"/> 
 </typeHandlers> 
 <!-- mapping 文件路径配置 --> 
 <mappers> 
 <mapper resource="resource/PersonMapper.xml" /> 
 </mappers> 
</configuration> 

创建测试用例

/** 
 * @author: Fighter168 
 */ 
public class SpringTest { 
 
 public static void main(String[] args) { 
 ApplicationContext context=new ClassPathXmlApplicationContext("resource/ApplicationContext.xml"); 
 PersonDao personDao=(PersonDao) context.getBean("personDao"); 
 List<Person> list=personDao.query(); 
 for(Person p:list){ 
  System.out.println(p.toString()); 
 } 
 } 
} 

测试结果展示

结果是成功自动转换成了我们想要的枚举

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多