一、区别
- 在.properties文件中&不需要修改.
- 在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&characterEncoding=utf-8</property>
<!-- &以&形式显示 -->
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
<!-- &不要以&形式,否则之后设置编码的语句设为无效,导致插入数据库数据乱码. -->
#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>
二、下面是之前记录一下自己排查的思路.
确定页面获取到为正确中文字符. - 页面编码是否正确.这里没有使用到.
解决方法 : 添加filter,字符过滤器.
确定插入数据库之前数据为正确字符. - 插入之前输出,java项目编码.
projecct 右键 → properties
数据库编码是否正确,自己用的是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;使用命令行插入中文字符. // 确定数据库编码正确.
使用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."); } }
使用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."); } }
通过对于hibernate.cfg.xml配置文件 与 applicationContext.xml中dataSource之间配置的对换.
确定为hibernate.connection.url问题,从而排查到&字符差别.
总结 :框架整合时,没有使用中文字符插入,最终导致2天排查问题,没有什么具体有意义收获.
但还是记录一下,感觉挺难找到的问题.