解决【The reference to entity "characterEncoding" must end with the ';' delimiter.】问题

版权声明:本文为博主【十甫寸木南】原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37164975/article/details/82496826

在配置数据库连接池数据源时,本来没有错误,结果加上编码转换格式后eclipse突然报错:

The reference to entity "characterEncoding" must end with the ';' delimiter

这是怎么回事?

经过查询,发现这个错误其实很好解决。

首先,原因是: .xml文件中 ‘ & ’字符需要进行转义!!!

看到这里,其实已经恍然大悟,那么,这个字符 ‘ & ’ 需要怎么转义呢?看下面这张表:

在xml文件中有以下几类字符要进行转义替换:

<

<

小于号

&gt;

>

大于号

&amp;

&

&apos;

'

单引号

&quot;

"

双引号

所以,我们在xml文件中不能直接写 ‘ & ’ 字符,而需要写成 ‘ &amp; ’

下面来一个对比:↓ ↓ ↓

首先,上错误的代码:

<!-- 配置 Spring 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 基本属性 driver、url、user、password -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url"                 
     value="jdbc:mysql://localhost:3306/sfcmn?useUnicode=true&characterEncoding=UTF-8" />
  <property name="username" value="root" />
  <property name="password" value="root" />
<!-- 。。。。。。 -->
<!-- 以下省略 -->

接下来是正确的代码:

<!-- 配置 Spring 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 基本属性 driver、url、user、password -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url"                 
   value="jdbc:mysql://localhost:3306/sfcmn?useUnicode=true&amp;characterEncoding=UTF-8" />
  <property name="username" value="root" />
  <property name="password" value="root" />
<!-- 。。。。。。 -->
<!-- 以下省略 -->

可以看到,只有一处有改动: 将 ‘ & ’ 换成了 ‘ &amp; ’

猜你喜欢

转载自blog.csdn.net/qq_37164975/article/details/82496826