Samuel Learning

  • 联系
  •  
  • 管理

文章分类(203)

  • 01 ASP/HTML(6)
  • 02 Script/CSS(16)
  • 03 C/C++
  • 04 XML(4)
  • 05 微软编程(26)
  • 06 J2EE(60)
  • 09 Linux(14)
  • 10 Database(27)
  • 11 报表打印
  • 12 打包安装(1)
  • 13 模式重构(2)
  • 14 系统架构
  • 15 敏捷开发(11)
  • 16 案例分析(30)
  • 17 Workflow(1)
  • 18 配置管理(1)
  • 19 项目管理
  • 20 英语(4)

新闻分类(52)

  • CXF学习
  • Hibernate学习(1)
  • iBatis学习(16)
  • Struts1学习(1)
  • Struts2学习(34)

.NET资源

  • adxmenu
  • C# Open Source
  • DNN Directory
  • M2Land
  • Windows Form FAQ
  • 中国DNN
  • 中国DNN联盟

Ajax

  • DoJo
  • GWT
  • JQuery
  • jquery autocomplete
  • jquery flexgrid
  • JQuery Form
  • jquery masked input
  • JQuery UI
  • jquery validation
  • Jquery 图表
  • jquery报表
  • jquery插件集合
  • Qooxdoo
  • Tibco GI
  • YUI
  • YUI-EXT
  • ZeroKode

Java开源

  • ABLE
  • Agile Tao
  • Ajax4JSF
  • Alfresco
  • AppFuse
  • Compiere
  • Equinox
  • Findbugs
  • Geronimo
  • Grails
  • Harmony
  • Hibernate论坛
  • JAG
  • Java开源大全
  • Java视线论坛
  • jBPM工作流
  • JSFTutorials
  • Nereide ERP
  • Ofbiz ERP
  • Opentaps ERP
  • operamasks
  • Petstore
  • Prototype.js
  • RIFE
  • Runa
  • SpringSide
  • Struts Menu
  • Sun Glassfish
  • Trails
  • YUI4JSF
  • 满江红

Mobile

  • Sencha

WEB资源

  • DHTML中心
  • DHTML参考手册
  • DHTML文档
  • EclipsePlugin
  • Firebug
  • GRO Clinux
  • jMaki
  • JSTL文档
  • LoadIcon
  • Openlaszlo
  • Struts Menu 展示
  • Web Test Tools
  • WebCtrs
  • Webdeveloper
  • 中国RIA开发者论坛

Workflow

  • E-Workflow
  • JBPM
  • OpenWFE
  • OSWorkflow
  • WFMC
  • Workflow Research

其他连接

  • confach
  • CPP
  • ejay
  • Giovanni
  • 丹佛
  • 交大e-learning
  • 交大研究生院
  • 可恶的猫
  • 天天@blog
  • 我的相册
  • 阿飞

大牛人

  • 32篇JBPM
  • David.Turing
  • HongSoft@业务集成
  • Joel
  • Koen Aers
  • Martinfowler
  • Raible Matt
  • Raible Wiki
  • Scott W.Ambler
  • Tom Baeyens
  • Uncle Bob
  • 一个世界在等待
  • 子在川上曰
  • 小布老师
  • 小明
  • 差沙
  • 徐昊
  • 江南白衣
  • 汪博士
  • 汪小金
  • 银狐999

开源软件

  • 2Bizbox ERP
  • CompiereCRM&ERP
  • EGW
  • Vtiger CRM
  • webERP

敏捷

  • Canoo
  • Cruisecontrol
  • DBUnit
  • EL4Ant
  • Extreme Programming
  • Fit
  • Fitnesse
  • JFrog
  • Liquibase
  • Maven
  • MockObjects
  • Selenium
  • Squish
  • xpairtise
  • XPlanner
  • XProgramming
  • 敏捷联盟

数据库

  • Oracle 中国
  • Oracle-ERP
  • Oracle在线社区

未归类

  • Aquarius Orm Studio
  • mambo建站系统
  • Oracle产品下载
  • 远程同步管理工具Capivara

经典框架

  • Apache Shale
  • formdef-struts
  • FreeMarker 主页
  • JBoss Seam
  • JSF 中心
  • JSF 入门应用
  • JSF中国
  • MyFaces官方
  • Spring 社区
  • Spring专业网站
  • Spring中文论坛
  • Spring参考手册
  • Spring官方网站
  • strecks-struts
  • Struts1
  • Struts2
  • Struts-layout
  • StrutsWiKi
  • Tapestry WIKI
  • Tapestry 官方
  • Tapestry4开发指南
  • Tapestry中文文档
  • Webwork2文档
  • Wicket

网络教程

  • Laliluna
  • RoseIndia
  • Sang Shin
  • Visualbuilder

著名站点

  • Buildix
  • Dev2Dev
  • IBM dev中国
  • InfoQ
  • ITPub
  • Java Eye
  • Java Research
  • JavaRead
  • JavaWorldTW
  • Matrix
  • PHP100
  • PHPX
  • SpringSideWiKi
  • TheServerSide
  • TWPHP
  • 中国工作流论坛

项目管理

  • 管理人网

最新评论

View Post

How do I use a Custom Type Handler with complex property or Type Safe Enumeration

Here is an example of how to set up a custom type handler with a complex property. This example uses the enum features of Java 1.5 but could be used with a Type Safe Enumeration in anything below 1.5.

Lets start by defining the database table

SHAPE {
name    varchar2(25),
color   number(100)
}

As you see the color is stored as a number, but in the class Shape is is a Color object. There are two question that I ran into. First, how do you cleanly map the number stored in the database with the instance of a Color? Second, how can iBatis and custom type handlers help?

To answer the first let me show you the code for the Shape class.

/*
* Shape.java
*
* Created on September 23, 2005
*/
package domain;
/**
*
* @author nmaves
*/
public class Shape {
public enum Color {
BLUE(1, "0000FF"), RED(2, "FF0000"), GREEN(3, "00FF00");
private int id;
private String rgb;
private Type(int id, String name) {
this(id, name, "no mime type");
}
private Type(int id, String rgb) {
this.id = id;
this. rgb = rgb;
}
public int getId() {
return this.id;
}
public String getRGB() {
return this.name;
}
public static Type getInstance(int i) {
for(Color color : Color.values()) {
if(color.id == i) {
return type;
}
}
throw new IllegalArgumentException("No such Color");
}
}
private String name;
private Color color;
/** Creates a new instance of Frequency */
private Shape(String name, Color color) {
this.color = color;
this.name = name;
}
public String toString() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public void setColor(Color color) {
this.color = color;
}
public String getName() {
return this.name;
}
public Color getColor() {
return this.color;
}
}

As you see the getInstance(int i) method allows for the mapping of the number stored in the database to an instance of this class. I am sure there is a better way of pulling these initial value from a properties file but this is only an example.

For the second part you will need a way to allow iBatis to make the conversion. This is where the custom type handler comes into play.

Below is my ColorTypeHandler.java

package dao.ibatis;
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;
import Shape.Color;
public class ColorTypeHandler implements TypeHandlerCallback {
public Object getResult(ResultGetter getter) throws SQLException {
int value = getter.getInt();
if (getter.wasNull()) {
return null;
}
Color color = Color.getInstance(value);
return color;
}
public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
if (parameter == null) {
setter.setNull(Types.INTEGER);
} else {
Color color = (Color) parameter;
setter.setInt(color.getId());
}
}
public Object valueOf(String s) {
return s;
}
}

You are almost there! All you need left to do is tell iBatis to map instances of Frequency object to this handler.

I added the following line into my SqlMapConfig file.

<typeHandler javaType="domain.Shape$Color" callback="dao.ibatis.ColorTypeHandler"/>
Useful Information

Notice the $ in the class name. This is how Java 1.5 denotes enums. If this were a type safe enum you would just use standard notation.

That is it. No need to change any of your other sqlmap entries. Here is an example of a select and insert that use this handler.

<resultMap class="Shape" id="ShapeResult">
<result column="name" property="name" />
<result column="color" property="color" />
</resultMap>
<select id="getShapeByName" parameterClass="string" resultMap="ShapeResult">
SELECT
*
FROM
SHAPE
WHERE
name = #value#
</select>
<insert id="insertReport" parameterClass="Report">
INSERT INTO
SHAPE (
name,
color
)
values (
#name#,
#color#,
)
</insert>

Notice that there is nothing special that need to be done for the color columns.

posted on 2008-07-22 23:18 MingIsMe 阅读(111) 评论(0)  编辑  收藏 所属分类: iBatis学习

 
Powered by:
BlogJava
Copyright © MingIsMe