環境准備
- 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,如下:

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查出數據