&在.properties和xml文件中差别,导致数据库中文乱码.

本文探讨了在.properties和xml配置文件中处理&字符的区别,指出在.properties文件中不需要转义,而在xml中需要转义为&。由于未正确处理这个问题,导致数据库连接的characterEncoding设置无效,进而引发中文乱码问题。作者分享了排查思路,包括检查页面编码、项目编码、数据库编码以及使用Hibernate和Spring插入数据时遇到的问题,最终发现是&字符未正确处理所致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、区别

  1. 在.properties文件中&不需要修改.
  2. 在xml文件中包含的&字符替换为&[去掉括号]amp;

在.properties文件中设置
jdbc:mysql://localhost:3306/database?useUnicode=true&[去掉括号]amp;characterEncoding=utf-8
使数据库连接characterEncoding设置无效,从而导致数据库插入数据中文?.

ep:

hibernate配置文件中hibernate.cfg.xml

#hibernate.cfg.xml
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/BookManagement?useUnicode=true&amp;characterEncoding=utf-8</property>
<!-- &以&amp;形式显示 -->

hibernate整合spring中改为database.properties配置文件

#database.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/BookManagement?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
<!-- &不要以&amp;形式,否则之后设置编码的语句设为无效,导致插入数据库数据乱码. -->

#applicationContext.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

二、下面是之前记录一下自己排查的思路.

  1. 确定页面获取到为正确中文字符. - 页面编码是否正确.这里没有使用到.

    解决方法 : 添加filter,字符过滤器.

  2. 确定插入数据库之前数据为正确字符. - 插入之前输出,java项目编码.

    projecct 右键 → properties

    这里写图片描述

  3. 数据库编码是否正确,自己用的是mysql.

    查看数据库使用的编码 : show variables like ‘%character%’;
    修改方法 : set character_set_server = ‘utf8’; // set variable_name = ‘utf8’;

    这里写图片描述

    查看指定数据库编码 : show create database BookManagement; // show create database databaseName;
    修改方法 : alter database BookManagement character set utf8; // alter database databaseName character set utf8;

    查看指定数据表编码 : show create table book; // show create table tableName;
    修改方法 : alter table book Charset=utf8; // alter table tableName Charset=utf8;

  4. 使用命令行插入中文字符. // 确定数据库编码正确.

  5. 使用hibernate原生方法生成session进行事务插入. // 确定原生可以正常插入

    public void mgtLogAdd2() {
            Configuration cfg = new Configuration().configure("hibernate/hibernate.cfg.xml");
            SessionFactory sessionFactory = cfg.buildSessionFactory();
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                // code start.
                ManagementLog mgtLog = new ManagementLog();
                mgtLog.setM_action("登录成功.");
                mgtLog.setM_time("2017-04-21 23:24:43");
    
                Management mgt = new Management();
                mgt.setM_id(14108424);
    
                mgtLog.setM_id(mgt);
                session.save(mgtLog);
                // code end.
                tx.commit();
            } catch (Exception e) {
                if (tx != null)
                    tx.rollback();
                throw e;
            } finally {
                session.close();
                System.out.println("over.");
            }
        }
  6. 使用spring获取sessionFactory实例,进行数据插入. // 出现中文乱码, 插入为?字符.

    public void mgtLogAdd4() {
            // 通过spring获取sessionFactory实例
            ApplicationContext context = 
                    new ClassPathXmlApplicationContext("spring/config/applicationContext.xml");
            SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
            Session session = sessionFactory.openSession();
            Transaction tx = null;
            try {
                tx = session.beginTransaction();
                // code start.
                ManagementLog mgtLog = new ManagementLog();
                mgtLog.setM_action("登录成功.");
                mgtLog.setM_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    
                Management mgt = new Management();
                mgt.setM_id(14108424);
    
                mgtLog.setM_id(mgt);
                session.save(mgtLog);
                // code end.
                tx.commit();
            } catch (Exception e) {
                if (tx != null)
                    tx.rollback();
                throw e;
            } finally {
                session.close();
                System.out.println("over.");
            }
        }
  7. 通过对于hibernate.cfg.xml配置文件 与 applicationContext.xml中dataSource之间配置的对换.

    确定为hibernate.connection.url问题,从而排查到&字符差别.

总结 :框架整合时,没有使用中文字符插入,最终导致2天排查问题,没有什么具体有意义收获.

但还是记录一下,感觉挺难找到的问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值