1.在核心配置文件mybatis-config.xml中配置数据库连接信息
mysql的j驱动jar包是mysql-connector-java-6.0.6.jar
mysql版本5.7
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置mybatis运行环境 --> <environments default="development"> <environment id="development"> <!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 --> <transactionManager type="JDBC" /> <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI --> <!-- POOLED 表示支持JDBC数据源连接池 --> <!-- UNPOOLED 表示不支持数据源连接池 --> <!-- JNDI 表示支持外部数据源连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=true" /> <property name="username" value="root" /> <property name="password" value="iytb890214" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.mybatis.dao.EmployeeMapper" /> </mappers> </configuration>
2.在外部属性文件jdbc.properties中配置数据库连接信息
mysql的j驱动jar包是mysql-connector-java-6.0.6.jar
mysql版本5.7
配置db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url/mybatis_test?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=true jdbc.username=root jdbc.password=iytb890214
配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="db.properties"></properties> <!-- 配置mybatis运行环境 --> <environments default="development"> <environment id="development"> <!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 --> <transactionManager type="JDBC" /> <!-- mybatis提供了3种数据源类型,分别是:POOLED,UNPOOLED,JNDI --> <!-- POOLED 表示支持JDBC数据源连接池 --> <!-- UNPOOLED 表示不支持数据源连接池 --> <!-- JNDI 表示支持外部数据源连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.mybatis.dao.EmployeeMapper" /> </mappers> </configuration>