环境准备
- Maven:3.6.3
- Jdk:1.8.0_181
- idea
1、下载mybatis源码
官网地址:https://github.com/mybatis/mybatis-3
选择需要的版本下载。
本例下载的是 mybatis-3-mybatis-3.5.1,下载完后解压。打开pom.xml,查看mybatis的依赖的父工程版本
2、下载载mybatis-parent源码
选择mybatis对应的mybatis-parent版本,本例版本是 mybatis-parent-31
官网地址:https://github.com/mybatis/parent
3、源码导入Idea
在Idea中新建一个空项目,将 mybatis 、 mybatis-parent 都放到空项目下,并导入模块
4、编译mybatis-parent源码,编译mybatis源码
1、编译mybatis-parent项目
切换mybatis-parent项目:
命令:mvn clean install
2、编译mybatis项目
切换mybatis项目(可以修改一下版本号,修改成自己特有的版本,方便区分):
修改mybatis版本(3.5.1-MY)。避免与官网依赖相同版本
命令:mvn install -Dmaven.test
Failed to execute goal org.apache.maven.plugins:maven-pdf-plugin:1.4:pdf (pdf) on project mybatis: Error during document generation: Error parsing /Users/h__d/Documents/workspace-idea/mybatis-3.5.1/mybatis-3-mybatis-3.5.1/target/pdf/site.tmp/xdoc/getting-started.xml: Error parsing the model: only whitespace content allowed before start tag and not \ufeff (position: COMMENT seen ...rning permissions and\n limitations under the License.\n\n-->\n\ufeff... @18:2)
解决:将maven-pdf-plugin插件注释,再次进行编译安装
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pdf-plugin</artifactId> </plugin> -->
5、测试使用源码
1、新建maven模块项目test-mybatis-my
引入自己编译的mybatis版本(3.5.1-MY),完整pom.xml,如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>test-mybatis-my</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <maven.compiler.source>8</maven.compiler.source> 13 <maven.compiler.target>8</maven.compiler.target> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>org.mybatis</groupId> 19 <artifactId>mybatis</artifactId> 20 <version>3.5.1-MY</version> 21 </dependency> 22 23 <!-- mysql --> 24 <dependency> 25 <groupId>mysql</groupId> 26 <artifactId>mysql-connector-java</artifactId> 27 <version>8.0.13</version> 28 </dependency> 29 30 <dependency> 31 <groupId>junit</groupId> 32 <artifactId>junit</artifactId> 33 <version>RELEASE</version> 34 <scope>compile</scope> 35 </dependency> 36 37 38 <dependency> 39 <groupId>ognl</groupId> 40 <artifactId>ognl</artifactId> 41 <version>3.2.15</version> 42 <scope>compile</scope> 43 <optional>true</optional> 44 </dependency> 45 46 <dependency> 47 <groupId>org.javassist</groupId> 48 <artifactId>javassist</artifactId> 49 <version>3.27.0-GA</version> 50 <scope>compile</scope> 51 <optional>true</optional> 52 </dependency> 53 </dependencies> 54 55 </project>
2、使用测试
注意:可能报错,如下:
解决:引入依赖
<dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.2.15</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.27.0-GA</version> <scope>compile</scope> <optional>true</optional> </dependency>
之后便能正常测试完成,使用mybatis查出数据