首页 > spring通过hibernate插入数据,mysql中文显示问号?

spring通过hibernate插入数据,mysql中文显示问号?

该转码的都转了,请问还有关键的哪里没有转呢??

userName=root
password=
driveClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/spring001?useUnicode=true&characterEncoding=UTF-8

这里转了

数据库也是utf-8啊

平常使用hibernate的时候,这样设置是没问题的。但是加上spring后,就不行了,出现中文显示问号

附上我的applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <context:component-scan base-package="dao"/>
    <context:property-placeholder location="classpath:db.properties"/>


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${userName}"/>
        <property name="password" value="${password}"/>
        <property name="driverClass" value="${driveClass}"/>
        <property name="jdbcUrl" value="${jdbcUrl}"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations" value="*.hbm.xml"/>

        <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">update</prop>
            </props>

        </property>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    <tx:advice id="ad" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="pointCut" expression="execution(* dao.*.*(..))"/>
        <aop:advisor advice-ref="ad" pointcut-ref="pointCut"/>
    </aop:config>


</beans>

已经解决。原来是url格式的问题:

如果url是写在hibernate.cfg.xml这样的xml文件中,需要在&后面加上:amp;

jdbc:mysql://localhost:3306/spring001?useUnicode=true&characterEncoding=UTF-8

如果在其他的则不用,如上,我自己写的db.properties文件则不需要加上
jdbc:mysql://localhost:3306/spring001?useUnicode=true&characterEncoding=UTF-8


在xml的配置文件中 “&”要用 “&amp; ” 代替

【热门文章】
【热门文章】