首页 > 代码库 > ibatis对枚举类的支持
ibatis对枚举类的支持
pojo类
package com.sg.bean; public class User { /** * 用户ID */ private String userID; /** * 用户名 */ private String userName; /** * 用户状态 * @see com.sg.bean.EnumStatus */ private EnumStatus status; public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public EnumStatus getStatus() { return status; } public void setStatus(EnumStatus status) { this.status = status; } }
枚举类
package com.sg.bean; public enum EnumStatus { NORMAL("1", "正常"), DELETE("2", "删除"), LOGICDEL("3", "注销"); private EnumStatus(String code, String description) { this.code = code; this.description = description; } private String code; private String description; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
重点来了——ibatis类型转换接口实现
先看一下源码中的注释
com.ultrapower.common.ibatis.sqlmap.client.extensions.TypeHandlerCallback A simple interface for implementing custom type handlers. Using this interface, you can implement a type handler that will perform customized processing before parameters are set on a PreparedStatement and after values are retrieved from a ResultSet. Using a custom type handler you can extend the framework to handle types that are not supported, or handle supported types in a different way. For example, you might use a custom type handler to implement proprietary BLOB support (e.g. Oracle), or you might use it to handle booleans using "Y" and "N" instead of the more typical 0/1. EXAMPLE Here‘s a simple example of a boolean handler that uses "Yes" and "No". public class YesNoBoolTypeHandlerCallback implements TypeHandlerCallback { <p/> private static final String YES = "Yes"; private static final String NO = "No"; <p/> public Object getResult(ResultGetter getter) throws SQLException { String s = getter.getString(); if (YES.equalsIgnoreCase(s)) { return new Boolean (true); } else if (NO.equalsIgnoreCase(s)) { return new Boolean (false); } else { throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected."); } } <p/> public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { boolean b = ((Boolean)parameter).booleanValue(); if (b) { setter.setString(YES); } else { setter.setString(NO); } } <p/> public Object valueOf(String s) { if (YES.equalsIgnoreCase(s)) { return new Boolean (true); } else if (NO.equalsIgnoreCase(s)) { return new Boolean (false); } else { throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected."); } } <p/> }
针对User对象中的EnumStatus,他的转换类如下
package com.sg.util; import java.sql.SQLException; import java.sql.Types; import com.ibatis.sqlmap.client.extensions.ParameterSetter; import com.ibatis.sqlmap.client.extensions.ResultGetter; import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback; import com.sg.bean.EnumStatus; public class EnumStatusHandler implements TypeHandlerCallback { public Object getResult(ResultGetter getter) throws SQLException { EnumStatus result = null; if(!getter.wasNull() && getter.getObject()!= null) { for(EnumStatus status : EnumStatus.values()) { if(status.getCode().equals(getter.getObject())) { result = status; break; } } } return result; } public void setParameter(ParameterSetter setter, Object obj) throws SQLException { if(obj == null) { setter.setInt(Types.INTEGER); }else { EnumStatus status = (EnumStatus)obj; setter.setString(status.getCode()); } } public Object valueOf(String s) { return s; } }
ibatis配置文件声明
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false" /> <typeAlias alias="com.sg.bean.EnumStatus" type="com.sg.util.EnumStatusHandler"/> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="http://www.mamicode.com/com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/sg" /> <property name="JDBC.Username" value="http://www.mamicode.com/root" /> <property name="JDBC.Password" value="http://www.mamicode.com/root" /> <property name="Pool.MaximumIdleConnections" value="http://www.mamicode.com/5" /> <property name="Pool.MaximumCheckoutTime" value="http://www.mamicode.com/120000" /> <property name="Pool.TimeToWait" value="http://www.mamicode.com/500" /> <property name="Pool.PingQuery" value="http://www.mamicode.com/select 1 from sample" /> <property name="Pool.PingEnabled" value="http://www.mamicode.com/false" /> <property name="Pool.PingConnectionsOlderThan" value="http://www.mamicode.com/1" /> <property name="Pool.PingConnectionsNotUsedFor" value="http://www.mamicode.com/1" /> </dataSource> </transactionManager> <sqlMap resource="User.xml" /> </sqlMapConfig>
经过上述的配置,User对象中的EnumStatus枚举类就可以顺利的进行数据库操作了,以上方案有以下几个好处:
- 没有破坏pojo的纯净,pojo对象的扩展和可维护性更高;
- 枚举类加强了User对象对属性类型的把控,开发这不用担心其他人在status属性中放错值了;
- pojo的扩展性增强,比如现在前台展示需要展示名称,开发者只需对枚举进行简单的读取就可以将数据库中的0、1转为自然语言;
- 脱离实体的限制,比如前台需要展示status的可选值,枚举类便可以首当其冲
No tags for this post.
除非注明,本站文章均为原创或编译,转载请注明: 文章来自
KENGINE | Kankanews.com
ibatis对枚举类的支持
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。