在配置mybatis-config.xml
的mysql url
时,会遇到如下问题:
&
会报错,这是因为对于xml而言,&
和<
是非法字符,需要使用转义字符&
代替
<
会产生错误,因为解析器会把该字符解释为新元素的开始。&
也会产生错误,因为解析器会把该字符解释为字符实体的开始。
当使用外部properties文件时,还有如下问题:
<environments default="development">
<environment id="development">
<!--配置数据管理器-->
<transactionManager type="JDBC"/>
<!--配置数据源-->
<dataSource type="POOLED">
<!--配置驱动-->
<property name="driver" value="${jdbc.driver}"/>
<!--配置连接 mysql url-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</dataSource>
</environment>
</environments>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.pwd=123456
这样配置后,报错如下:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useUnicode=true&characterEncoding=UTF-8'.
### The error may exist in com/bbedu/mapper/MonsterMapper.xml
### The error may involve com.bbedu.mapper.MonsterMapper.getMonsterById
### The error occurred while executing a query
### Cause: java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malformed database URL, failed to parse the connection string near ';useUnicode=true&characterEncoding=UTF-8'.
可以看出,在properties中无需转义,转义反而会导致无法解析,因此,properties需要改为:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.pwd=123456
评论