我在做一个练习的时候后台数据库是Sybase的字符集是cp850用Spring的Hibernate连接后用find()从数据库里取到的值都是乱码,我写了一个方法里到List里把它一项一项的转正了GBK,但是我想问问有没有更好的方法,不用一个一个的转。
下面是applicationContext.xml的代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
- in Spring Train by zhangzhenhua 2007.4.23
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.sybase.jdbc2.jdbc.SybDriver</value>
</property>
<property name="url">
<value>
jdbc:sybase:Tds:10.201.1.111:5000/sgmarketdatabase
</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value>su27ik</value>
</property>
</bean>
<!--
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/sysmanager"/>
-->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SybaseDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/lhd/sysmanager/pojo/MarketRole.hbm.xml
</value>
<value>
com/lhd/sysmanager/pojo/MarketWork.hbm.xml
</value>
<value>
com/lhd/sysmanager/pojo/MarketUser.hbm.xml
</value>
<value>
com/lhd/sysmanager/pojo/WorkLog.hbm.xml
</value>
<value>
com/lhd/sysmanager/pojo/PubDept.hbm.xml
</value>
</list>
</property>
</bean>
<bean id="MarketRoleDAO"
class="com.lhd.sysmanager.dao.imp.MarketRoleDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<bean id="MarketWorkDAO"
class="com.lhd.sysmanager.dao.imp.MarketWorkDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<bean id="MarketUserDAO"
class="com.lhd.sysmanager.dao.imp.MarketUserDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<bean id="WorkLogDAO"
class="com.lhd.sysmanager.dao.imp.WorkLogDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<bean id="PubDeptDAO"
class="com.lhd.sysmanager.dao.imp.PubDeptDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<bean id="PageDAO"
class="com.lhd.sysmanager.dao.imp.PageDAO">
<property name="sessionFactory">
<ref bean="SessionFactory"></ref>
</property>
</bean>
<tx:advice id="TxManager"><!-- 事务通知 -->
<tx:attributes>
<tx:method name="list*" read-only="true" rollback-for="Exception"/>
<tx:method name="find*" read-only="true" rollback-for="Exception"/>
<tx:method name="query*" read-only="true" rollback-for="Exception"/>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="dele*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config><!-- 事务管理 -->
<aop:pointcut id="defaultService" expression="execution(* com.lhd.sysmanager.manager.*.*(..))"/>
<aop:advisor pointcut-ref="defaultService" advice-ref="TxManager"/>
</aop:config>
<!-- Spring集成的hibernate事务管理类 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="SessionFactory"/></property>
</bean>
<bean id="MarketRoleService" class="com.lhd.sysmanager.manager.MarketRoleManager">
<property name="dao" ref="MarketRoleDAO"></property>
</bean>
<bean id="MarketUserService" class="com.lhd.sysmanager.manager.MarketUserManager">
<property name="dao" ref="MarketUserDAO"></property>
</bean>
<bean id="MarketWorkService" class="com.lhd.sysmanager.manager.MarketWorkManager">
<property name="dao" ref="MarketWorkDAO"></property>
</bean>
<bean id="PubDeptService" class="com.lhd.sysmanager.manager.PubDeptManager">
<property name="dao" ref="PubDeptDAO"></property>
</bean>
<bean id="WorkLogService" class="com.lhd.sysmanager.manager.WorkLogManager">
<property name="dao" ref="WorkLogDAO"></property>
</bean>
<bean id="PageService" class="com.lhd.sysmanager.manager.PageManager">
<property name="page" ref="PageDAO"></property>
</bean>
</beans>
下面是find()方法
public List find() {
log.debug("finding MarketWork instance by example");
try {
List results = getHibernateTemplate().find("from com.lhd.sysmanager.pojo.MarketUser u order by u.id desc");
log.debug("find successful, result size: "
+ results.size());
for(int i= 0;i<result.size();i++)
{
MarketUser us = (MarketUser)result.get(i);
us.setDeptName(PubMethod.ISOtoGBK(us.getDeptName()));
System.out.println("#######"+us.getDeptName()+"#####");
} return results;
} catch (RuntimeException re) {
log.error("find failed", re);
throw re;
}
}
分享到:
相关推荐
hibernate_中文乱码hibernate_中文乱码hibernate_中文乱码hibernate_中文乱码hibernate_中文乱码hibernate_中文乱码
hibernate数据库中文乱码问题2008年09月16日 星期二 下午 12:00我的数据库用的是mysq,编码我设成了utf-8! 用自己写的连接池可以设置编码
使用java的SSH+Mysql时出现插入数据乱码还有查询也出现乱码问题
解决hibernate与MySql存储中文时出现乱码问题
Hibernate_MySQL中文乱码问题.docHibernate_MySQL中文乱码问题.doc
该项目是解决hibernate反向工程生成的实体文件中文备注乱码问题的。 试用环境 hibernate-version 4.0.1 Hibernate Tools 5.1.4.v20170413-1729 org.hibernate.eclipse.feature.feature.group JBoss by Red Hat ...
struts.properties设置( struts.objectFactory = spring struts.i18n.reload=true ...struts.i18n.encoding=GBK ) web.xml(仅写出中文处理部分,spring的配置不用改变) <filter-name>encodingFilter ...
NULL 博文链接:https://kennylee26.iteye.com/blog/1229993
Struts Hibernate MyEclipse Tomcat MySQL JSP 乱码 编码出错 内码设定
hibernate+mysql 中文存入数据库乱码解决方法
乱码中乱码是很常见很头疼的,本文档讲述解决J2EE开发中乱码问题
hibernate的中文问题的解决方案
以hibernate-tools-5.2.1.Final.jar为例,5.0以下的版本尚未验证
hibernate插入数据库乱码的解决方式,折腾了好久,总结出来供大家分享
打印sql语句还是中文 到数据库发现乱码,本以为是hibernate搞的,结果证明不是hibernate 自己也是网上解决办法 自己的问题解决了 希望能帮助其他人 运行命令:SHOW VARIABLES LIKE 'character_set_%'; 结果如下 '...
如果持久化的类中有包括了汉字的String对象,那么对应到数据库中汉字的部分就会是乱码。这主要是由于MySQL数据表的字符集与我们当前使用的本地字符集不相同造成的
不会让大家后悔,里面介绍了连接池,dbutils的应用,struts-hibernate-sprint结合开发的实例详解,用的是sql server数据库,也可以改为mysql oracle只要改变数据库连接驱动和url即可。
java web hibernate struts spring 全方位解决乱码 很全!
ssh spring+stuts+hibernate+sql server 中文乱码解决方案_lj管用.txt
在eclipse上通过hibernate工具,将数据库表生成Java类和xml配置文件 总共有2个版本,根据自己的需求使用版本3或者5: 1. hibernate-tools-3.4.0.CR2.jar是修改完成的jar包,直接替换原来的jar包, 重启eclipse即可...