IBatis中使用enum(IBatis sqlMal Enum)

很多朋友可能会说大可以使用Mybatis,但是目前项目决定,所以还得使用IBaits进行开发。过程中涉及到一个业务类型,原本使用数据库表进行存储,但是就为几个值而多了一个连表查询时很不值得的,所以果断
采用Enum来作为一个属性进行存储,数据库表存储int型的code值。展示时可以展示为对应的标题。接下来直接说一下步骤吧。

1、引入jar包,我是用的是maven

[java]<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.0</version>
</dependency>
[/java]

2、接下来看一下我的Enum对象:

[java]public enum BusinessTypeEnum {

BOTH(0, "两者都是"),
SUPPLIER(1, "供应商"),
PURCHASER(2, "采购商"),
MERCHANTS(3, "招商");

private int code = 0;

private String name = "";

private BusinessTypeEnum(int code, String name) {
this.code = code;
this.name = name;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}[/java]

3、自定义BusinessTypeEnum的处理类:

[java]import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

import java.sql.SQLException;
import java.sql.Types;

/**
* Created by jian on 14-8-7.
*/
public class BusinessTypeEnumHandler implements TypeHandlerCallback {
/**
* Performs processing on a value before it is used to set
* the parameter of a PreparedStatement.
*
* @param setter The interface for setting the value on the PreparedStatement.
* @param parameter The value to be set.
* @throws java.sql.SQLException If any error occurs.
*/
public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
if(parameter==null){
setter.setInt(Types.INTEGER);
}else
{
BusinessTypeEnum businessTypeEnum=(BusinessTypeEnum)parameter;
setter.setInt(businessTypeEnum.getCode());
}
}

/**
* Performs processing on a value before after it has been retrieved
* from a ResultSet.
*
* @param getter The interface for getting the value from the ResultSet.
* @return The processed value.
* @throws java.sql.SQLException If any error occurs.
*/
public Object getResult(ResultGetter getter) throws SQLException {
BusinessTypeEnum businessTypeEnum=null;
if(!getter.wasNull() && getter.getObject()!=null){
for (BusinessTypeEnum item: BusinessTypeEnum.values())
{
if(item.getCode()==Integer.parseInt(getter.getObject().toString()))
{
businessTypeEnum=item;
}
}
}
return businessTypeEnum;
}

/**
* Casts the string representation of a value into a type recognized by
* this type handler. This method is used to translate nullValue values
* into types that can be appropriately compared. If your custom type handler
* cannot support nullValues, or if there is no reasonable string representation
* for this type (e.g. File type), you can simply return the String representation
* as it was passed in. It is not recommended to return null, unless null was passed
* in.
*
* @param s A string representation of a valid value for this type.
* @return One of the following:
* <ol>
* <li>the casted repersentation of the String value,</li>
* <li>the string as is,</li>
* <li>null, only if null was passed in.</li>
* </ol>
*/
public Object valueOf(String s) {
return s;
}
}[/java]

4、配置处理类:
这里需要说明一下,配置方式分两种,一种是全局设置,另外一种则是单独设置,相比较来说全局设置比较方便一些:

[java]<sqlMapConfig>
<typeHandler javaType="com.yneit.www.common.BusinessTypeEnum" callback="com.yneit.www.common.BusinessTypeEnumHandler"/>
</sqlMapConfig>[/java]

关于单独设置:
首先需要在<sqlMap>中设置parameterMap,在设置parameter时指定typeHandler对应处理类,接下来需要设置resultMap,对应的属性也需要添加typeHandler

PS:MyBatis在处理Enum时较IBatis要方便,可以使用泛型。本人也刚刚开始接触,如果有不正确的地方欢迎指正交流。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据