我正在使用Spring Framework 4和Hibernate 4以及MySQL数据库。对于使用数据初始化数据库,我使用在hibernate.xml文件中配置的初始化脚本。
数据库的初始化没有问题,但在数据库中显示?
而不是带有变音符号的字符。例如,我代替Česko
而不是?esko
。
有什么想法吗?
database.sql
INSERT INTO COUNTRY (ID, NAME) VALUES (1, 'Slovensko'), (2, 'Česko'), (3, 'Poľsko'), (4, 'Maďarsko');
由Hibernate创建的COUNTRY表
CREATE TABLE `country` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `UK_l7qlysdc1xbo69vsmueersr2k` (`NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
database.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/BPM?UseUnicode=true;characterEncoding=utf8
jdbc.username=root
jdbc.password=root
hibernate.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!-- Database properties file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
<!-- Configure JDBC Connection -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Configure Hibernate 4 Session Facotry -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="bpm" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>
<!-- Initialize database -->
<jdbc:initialize-database data-source="dataSource">
<jdbc:script encoding="utf-8" location="classpath:/data/database.sql" />
</jdbc:initialize-database>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="bpm"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
答案 0 :(得分:1)
我设法解决了这个问题。问题出在连接URL上,应该是:
jdbc.url=jdbc:mysql://localhost:3306/BPM?characterEncoding=UTF-8