1. 程式人生 > >The reference to entity "characterEncoding" must end with the ';'

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

在配置資料庫連線池資料來源時,本來沒有錯誤,結果加上編碼轉換格式後eclipse突然報錯:

這是怎麼回事?

經過查詢,發現這個錯誤其實很好解決。

首先,原因是: .xml檔案中 ‘ & ’字元需要進行轉義!!!

看到這裡,其實已經恍然大悟,那麼,這個字元 ‘ & ’ 需要怎麼轉義呢?看下面這張表:

在xml檔案中有以下幾類字元要進行轉義替換:

 

 所以,我們在xml檔案中不能直接寫 ‘ & ’ 字元,而需要寫成 ‘ & ’

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 管理DataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- set方法注入屬性,和類中的成員屬性無關,和set方法名稱有關,比如有一個屬性叫username,但是set方法:setName -->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <!-- 轉義前 -->
        <property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"></property>
        <!-- 轉義後 -->
        <property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!-- 管理jdbcTemplate -->
    <bean id="template"
        class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>
</beans>

&n