一、maven中添加依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.mybatis</groupId> 4 <artifactId>mybatis</artifactId> 5 <version>3.5.2</version> 6 </dependency> 7 <dependency> 8 <groupId>mysql</groupId> 9 <artifactId>mysql-connector-java</artifactId> 10 <version>8.0.11</version> 11 </dependency> 12 <dependency> 13 <groupId>junit</groupId> 14 <artifactId>junit</artifactId> 15 <version>4.12</version> 16 <!-- <scope>test</scope>--> 17 <scope>compile</scope> 18 </dependency> 19 </dependencies> 20 <build> 21 <resources> 22 <resource> 23 <directory>src/main/java</directory> 24 <includes> 25 <include>**/*.properties</include> 26 <include>**/*.xml</include> 27 </includes> 28 <filtering>true</filtering> 29 </resource> 30 </resources> 31 </build>
二、添加资源文件
src/mian/resources文件夹下手动添加db.properties和mybatis-config.xml。
db.properties中添加内容:
1 //数据库驱动 2 mysql.driver=com.mysql.cj.jdbc.Driver 3 //数据库链接地址 4 mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false 5 //数据库用户名 6 mysql.username=root 7 //数据库密码 8 mysql.password=1234
mybatis-config.xml中添加内容:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 环境配置 --> 7 <!-- 加载类路径下的属性文件 --> 8 <properties resource="db.properties"/> 9 <environments default="development"> 10 <environment id="development"> 11 <transactionManager type="JDBC"/> 12 <!-- 数据库连接相关配置 ,db.properties文件中的内容--> 13 <dataSource type="POOLED"> 14 <property name="driver" value="${mysql.driver}"/> 15 <property name="url" value="${mysql.url}"/> 16 <property name="username" value="${mysql.username}"/> 17 <property name="password" value="${mysql.password}"/> 18 </dataSource> 19 </environment> 20 </environments> 21 <!-- mapping文件路径配置 --> 22 <mappers> 23 <mapper resource="mapper/UserMapper.xml"/> 24 </mappers> 25 26 </configuration>
标签:xml,11,配置,mybatis,Mybatis,添加,mysql,MyBatis,properties From: https://www.cnblogs.com/xiaobiti/p/17159143.html