使用hibernate中的UserType解决乱码问题

转自:http://hi.baidu.com/gacmotor/item/3ea2e62952c99acbddf69aea

总的思想还是一样:
页面编码都是utf-8,数据库是xxx,那就new String("你好".getBytes("utf-8"),"xxx")

然后读取出来的时候new String("...".getBytes("xxx"),"utf-8")
 

hibernate 访问 oracle 乱码 oracle的字符集是WE8ISO8859P1,由于历史原因,不可修改。已经修改本地 NLS_LANG,因此使用PL/SQL developer可以正常访问。 但是 hibernate使用thin方式连接数据库,中文乱码。 为了使页面可以正常显示,在取数据时在form的get方法中使用 
String new_str = new String(old_str.getBytes("iso-8859-1"));

 得到的new_str可以正常显示中文。 但是这样增加了冗余代码,而且使用好多第三方组件时,由于无法控制转码细节,导致乱码。 在互联网上搜索解决方案,搜到的大多是一个方法,加入filter,使用org.springframework.web.filter.CharacterEncodingFilter 但是对我这个情况完全无效,存取数据都是乱码,存到数据库里的数用客户端看也是乱码。 我觉得CharacterEncodingFilter应该是在oracle的字符集正确设置的情况下,比如使用ZHS16GBK,解决乱码的问题。 现在请教各位高手有没有遇到过同样的问题,如何解决这种情况下的乱码。是否需要写一个filter或者是hibernate直接可以对oracle字符集进行配置。 问题已经解决,说一下我的解决方案吧。使用hibernate的UserType 在hbm.xml中配置,

比如 <property name="roleName" type="com.xtmcc.framework.dao.GBKString"> <column name="ROLE_NAME" length="50" />
 </property> 

自定义GBKString 类
package two;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleTypes;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class a1 implements UserType {
	public a1() {
		super();
	}

	public int[] sqlTypes() {
		return new int[] { OracleTypes.VARCHAR };
	}

	public Class returnedClass() {
		return String.class;
	}

	public boolean equals(Object x, Object y) throws HibernateException {
		return (x == y) || (x != null && y != null && (x.equals(y)));
	}

	public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
			throws HibernateException, SQLException {
		String val = rs.getString(names[0]);
		if (null == val) {
			return null;
		} else {
			try {
				val = new String(val.getBytes("iso-8859-1"), "GBK");
			} catch (UnsupportedEncodingException e) {
				throw new HibernateException(e.getMessage());
			}
			return val;
		}
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index)
			throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, OracleTypes.VARCHAR);
		} else {
			String val = (String) value;
			try {
				val = new String(val.getBytes("GBK"), "ISO_8859_1");
			} catch (UnsupportedEncodingException e) {
				throw new HibernateException(e.getMessage());
			}
			st.setObject(index, val, OracleTypes.VARCHAR);
		}
	}

	public Object deepCopy(Object value) throws HibernateException {
		if (value == null)
			return null;
		return new String((String) value);
	}

	public boolean isMutable() {
		return false;
	}

	public Object assemble(Serializable arg0, Object arg1)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public Serializable disassemble(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	public int hashCode(Object arg0) throws HibernateException {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	public Object replace(Object arg0, Object arg1, Object arg2)
			throws HibernateException {
		// TODO Auto-generated method stub//
		return null;
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值