004. MyBatis基本使用---01MyBatis环境配置
1. pom.xml
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
2.在数据库中添加数据资源
3.使用mybatis-config.xml进行环境配置(src/main/resources/mybatis-config.xml)
4.mybatis-config.xml
<!-- 环境,可以配置多个,default:指定采用哪个环境 --> <environments default="dev"> <!-- id:唯一标识 --> <environment id="dev"> <!--采用JDBC进行事务的管理commit/rollback--> <transactionManager type="JDBC"/> <!-- 数据源,连接池类型的数据源 --> <!--<dataSource type="POOLED">--> <dataSource type="com.imooc.mybatis.datasource.C3P0DataSourceFactory"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/> <property name="user" value="root"/> <property name="password" value="197366"/> <property name="initialPoolSize" value="5"/> <property name="maxPoolSize" value="20"/> <property name="minPoolSize" value="5"/> </dataSource> </environment> </environments>