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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记