java - Spring框架中使用DataSourceInitializer初始化数据库时编码不正确

标签 java mysql database spring hibernate

我正在使用 Spring Framework 4 和 Hibernate 4 以及 MySQL 数据库。为了使用数据初始化数据库,我使用 hibernate.xml 文件中配置的初始化脚本。

数据库初始化一切正常,但数据库中出现 ? 而不是带变音符号的字符。例如,我看到的是 ?esko,而不是 Česko

有什么想法吗?

数据库.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

数据库.属性

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>

最佳答案

我设法解决了这个问题。问题出在连接 URL 上,应该是:

jdbc.url=jdbc:mysql://localhost:3306/BPM?characterEncoding=UTF-8

关于java - Spring框架中使用DataSourceInitializer初始化数据库时编码不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23584846/

相关文章:

MySQL插入后自动将数据插入到另一张表中

java - 从 Android 2017 中的 URL 中提取 JSON 数据

php - 格式化日期的问题

sql - 没有表格的映射值

Java SQL 插入,其中不存在 2 个相同的值

python - python中的字符串解码方法错误

database - coldfusion 如何将pdf文件保存在数据库中并在浏览器中获取

java - 如何将默认字符串放入Jtextpane并使其在java中不可编辑

java - Java 中的标记图表示

java - 读取 CSV 并将字符串转换为 double