标签:
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;
}
}
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/>
}
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;
}
}
<?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="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/sg" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="root" /> <property name="Pool.MaximumIdleConnections" value="5" /> <property name="Pool.MaximumCheckoutTime" value="120000" /> <property name="Pool.TimeToWait" value="500" /> <property name="Pool.PingQuery" value="select 1 from sample" /> <property name="Pool.PingEnabled" value="false" /> <property name="Pool.PingConnectionsOlderThan" value="1" /> <property name="Pool.PingConnectionsNotUsedFor" value="1" /> </dataSource> </transactionManager> <sqlMap resource="User.xml" /> </sqlMapConfig>
经过上述的配置,User对象中的EnumStatus枚举类就可以顺利的进行数据库操作了,以上方案有以下几个好处:
标签:
原文地址:http://my.oschina.net/u/1398304/blog/374744