ibatis-TypeHandlerCallback使用
来源:互联网 发布:智能家庭解决方案知乎 编辑:程序博客网 时间:2024/06/05 19:28
场景
数据入库前需要加密,替换特殊字符,枚举类型转化;数据读取时需要对数值字段null保护等。。
应用实例
- 1.字段加密入库,解密读取
public class EncodeTypeHandler implements TypeHandlerCallback { @Override public void setParameter(ParameterSetter parameterSetter, Object o) throws SQLException { String id = encode((String) o); parameterSetter.setString(id); } @Override public Object getResult(ResultGetter resultGetter) throws SQLException { return decode(resultGetter.getString()); } @Override public Object valueOf(String s) { return s; }}
- 2.字段特殊字符处理(替换’\0’)
public class SpecialCharTypeHandler implements TypeHandlerCallback { @Override public void setParameter(ParameterSetter parameterSetter, Object o) throws SQLException { if (o == null) { parameterSetter.setString(null); return; } String id = (String)o; parameterSetter.setString(id.replaceAll("\0", "")); } @Override public Object getResult(ResultGetter resultGetter) throws SQLException { return resultGetter.getString(); } @Override public Object valueOf(String s) { return s; }}
- 3.字段保护(防止数据库里的值是null)
public class IntegerTypeHandler implements TypeHandlerCallback { @Override public Object getResult(ResultGetter getter) throws SQLException { if (getter.wasNull()) { return 0; } return getter.getInt(); } @Override public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { setter.setInt((Integer)parameter); } @Override public Object valueOf(String s) { return s; }}
- 4.枚举类型转化
public class OrderStatusTypeHandler implements TypeHandlerCallback { @Override public void setParameter(ParameterSetter parameterSetter, Object o) throws SQLException { if(o==null) o = OrderStatus.Init; int b = ((OrderStatus) o).getCode(); parameterSetter.setInt(b); } @Override public Object getResult(ResultGetter resultGetter) throws SQLException { if (resultGetter.wasNull()) return null; int code = resultGetter.getInt(); OrderStatus orderStatus = null; orderStatus = OrderStatus.getOrderStatus(code); return orderStatus; } @Override public Object valueOf(String s) { return s; }}
配置
在sqlMapConfig或者在sqlMap都可以配置
- 配置别名使用
1.在sqlMapConfig里配置:
<sqlMapConfig> <settings enhancementEnabled="true" lazyLoadingEnabled="false" cacheModelsEnabled="false" useStatementNamespaces="false" maxRequests="32" maxSessions="10" maxTransactions="5"/> <typeAlias alias="SpecialCharTypeHandler" type="com.mo.SpecialCharTypeHandler"/> <sqlMap resource="sqlmap/PayOrderDao.xml"/></sqlMapConfig>
2.在sqlMap(PayOrderDao.xml)里配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap> <typeAlias alias="SpecialCharTypeHandler" type="com.mo.SpecialCharTypeHandler"/> ...</sqlMap>
- 根据参数类型匹配使用
1.在sqlMapConfig里配置:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig> <settings enhancementEnabled="true" lazyLoadingEnabled="false" cacheModelsEnabled="true" useStatementNamespaces="false" maxRequests="32" maxSessions="10" maxTransactions="5"/> <typeHandler javaType="com.mo.OrderStatus" callback="com.mo.OrderStatusTypeHandler"/> <sqlMap resource="sqlmap/PayOrderDao.xml"/></sqlMapConfig>
2.在sqlMap(PayOrderDao.xml)里配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap> <typeHandler javaType="com.OrderStatus" callback="com.mo.OrderStatusTypeHandler"/> ...</sqlMap>
使用
- 别名配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap> <typeAlias alias="OrderStatusTypeHandler" type="com.mo.OrderStatusTypeHandler"/> .. <update id="updatePayStatus"> update tbl_pay_order set order_status=#orderStatus,handler=OrderStatusTypeHandler#, where id=#id#; </update> ..</sqlMap>
- 参数类型匹配(不需要指定handler)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap> <typeHandler javaType="com.mo.OrderStatus" callback="com.mo.OrderStatusTypeHandler"/> .. <update id="updatePayStatus"> update tbl_pay_order set order_status=#orderStatus#, where id=#id#; </update> ..</sqlMap>
0 0
- ibatis-TypeHandlerCallback使用
- 使用iBatis的类型处理器TypeHandlerCallback
- Ibatis的TypeHandlerCallback用法详解
- ibatis 使用
- ibatis使用
- iBatis使用
- Implement TypeHandlerCallback for oracle Char handle!
- ibatis学习(二)--ibatis使用介绍
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- ibatis学习(二)--ibatis使用介绍
- ibatis学习(二)--ibatis使用介绍
- ibatis学习(二)--ibatis使用介绍
- ibatis学习(二)--ibatis使用介绍
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- [Java][ibatis]Ibatis TypeHandler使用总结
- iBatis使用点滴
- IBatis详细使用例子
- ibatis 使用笔记
- UITextView的用法
- 利用GoogleEarth影像制作Skyline MPT案例
- LeetCode38——Count and Say
- IOS之Storyboard导航大揭秘
- CentOS安装scp命令
- ibatis-TypeHandlerCallback使用
- Android资源管理框架(Asset Manager)简要介绍和学习计划
- 【转】单元测试利器 JUnit 4
- 最近才知道的一款软件,可以实现在windows下使用linux下的命令,甚至可以使用简单的shell
- 蓝桥杯——算法提高 日期计算
- TorCMS,基于Tornado的开源内容管理系统
- lua语法学习1
- 1018. 锤子剪刀布 (20)
- csdn的中文it圈子文化