2014-05-10 22 views
0

我正在使用Spring Framework 4和Hibernate 4以及MySQL數據庫。對於使用數據初始化數據庫,我使用在hibernate.xml文件中配置的初始化腳本。在Spring Framework中使用DataSourceInitializer初始化數據庫時編碼不正確

初始化數據庫沒問題,但在數據庫中出現?而不是帶有變音符的字符。例如,而不是Česko我看到?esko

任何想法?由Hibernate創建

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 

database.sql

INSERT INTO COUNTRY (ID, NAME) VALUES (1, 'Slovensko'), (2, 'Česko'), (3, 'Poľsko'), (4, 'Maďarsko'); 

COUNTRY表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> 

回答

1

我設法解決這個問題。問題出在連接URL,它應該是:

jdbc.url=jdbc:mysql://localhost:3306/BPM?characterEncoding=UTF-8 
相關問題