txl690956710 2017-01-04 08:11 采纳率: 66.7%
浏览 1442
已采纳

在spring集成hibernate 中,如果包含中文参数查询不出结果

我的项目在spring4.3.5中集成hibernate4.3.11,其中如果包含中文参数查询不出任何结果

下面方法的参数username如果是字母可以正常读取到数据,如果是中文则uniqueResult为null。

我谷歌了网上说的都是使用setString 设置参数就不会有中文问题了,可我本来就是用的是setString方法设置参数的,请求各位大侠赐教

 @Override
    public void updateUserAccount(String username, int price) {
        // 验证余额是否足够
        String hql = "select a.balance from Account a where a.username=?";
        Object uniqueResult = getSession().createQuery(hql).setString(0, username).uniqueResult();
        int balance = (int) uniqueResult;
        if (balance < price) {
            throw new RuntimeException("用户名为:" + username + "的用户余额不足");
        }

        hql = "update Account a set a.balance = a.balance - ? where a.username=?";
        getSession().createQuery(hql).setParameter(0, price).setParameter(1, username).executeUpdate();
    }

下面是spring的配置文件中已经设置了使用utf-8

 <!-- 配置hibernate sessionFactory的实例 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 配置数据源属性 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置hibernate的配置文件路径及名称 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- 配置hibernate 映射文件的位置及名称 -->
        <property name="mappingLocations"
            value="classpath:com/atguigu/spring/hibernate/entity/*.hbm.xml"></property>

        <property name="hibernateProperties">
            <props>
                <prop key="connection.useUnicode">true</prop>
                <prop key="connection.characterEncoding">UTF-8</prop>
            </props>
        </property>
    </bean>
  • 写回答

2条回答

  • JE_GE 2017-01-04 08:25
    关注

    可以和ssh没有关系,如果是mysql数据库是否编码有问题哦
    http://www.cnblogs.com/qmfsun/p/4846467.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题