如何使用 sql server 2005 将 Spring 与 Hibernate 集成并获得 Unicode 支持。我尝试了很多不同的方法,但我就是无法让它发挥作用。
表中的列是 nvarchar,Spring 中的字符集是 UTF-8。我可以很好地读取 Unicode 文本(我使用 sql server 管理工具添加的),但写入不起作用,它在数据库中出现乱码。
jdbc 网址是
jdbc:sqlserver://localhost:1433;useUnicode=true;characterEncoding=UTF-8;databaseName=test;
Run Code Online (Sandbox Code Playgroud)
在休眠配置文件中使用这些属性
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF8</property>
Run Code Online (Sandbox Code Playgroud)
我还有一个过滤器可以更改所有页面的编码
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
chain.doFilter(request, response);
//do it again, since JSPs will set it to the default
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
Run Code Online (Sandbox Code Playgroud)
有没有好心人成功地做到了这一点,并且可以提供帮助?
非常感谢!
主要目标:在工厂中自动注册类(通过字符串),以便在运行时使用该字符串动态创建,类可以位于它们自己的文件中,而不是分组在一个文件中。
我有几个类,它们都继承自同一个基类,并且它们定义一个字符串作为它们的类型。
用户想要获取这些类之一的实例,但只在运行时知道类型。
因此,我有一个工厂来创建给定类型的实例。我不想硬编码“if then 语句”,所以我有一个元类来注册基类的所有子类:
class MetaRegister(type):
# we use __init__ rather than __new__ here because we want
# to modify attributes of the class *after* they have been
# created
def __init__(cls, name, bases, dct):
if not hasattr(cls, 'registry'):
# this is the base class. Create an empty registry
cls.registry = {}
else:
# this is a derived class. Add cls to the registry
interface_id = cls().get_model_type()
cls.registry[interface_id] = cls
super(MetaRegister, cls).__init__(name, bases, dct)
Run Code Online (Sandbox Code Playgroud)
问题是,要使其工作,工厂必须导入所有子类(因此元类运行)。要解决这个问题,您可以使用from X import …