将mybaties.xml文件中的关于数据库的部分进行解耦
在resource下新建db.properties(后缀一定得是properties
url = jdbc:mysql://127.0.0.1:3306/mysql?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
driver = com.mysql.cj.jdbc.Driver
username = root
password = root
其中url里面的3306/后面跟的mysql是数据库的名称,根据以后数据库名称的不同改
然后mybaties.xml中的数据库部分改成这样
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<typeAliases>
<typeAlias type="com.my.pojo.Book" alias="b"></typeAlias>
<package name="com.my.pojo"/>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/BookMapper.xml"/>
</mappers>
</configuration>
configuration后面加入标签properties resource
datasource标签里面将各个value值改成&{***},括号内跟前面name的要一致