mybatis原始碼解析第一課-mybatis原始碼匯入idea執行
阿新 • • 發佈:2018-12-15
從本篇部落格開始,進入mybatis的原始碼學習,本篇部落格主要是講解mybatis原始碼的下載,並將原始碼匯入到idea,可以用來除錯;
一、下載mybatis原始碼
版本:mybatis-3-mybatis-3.4.6.zip,下載地址:https://github.com/mybatis/mybatis-3 通過檢視mybatis的pom.xml檔案,發現是一個聚合專案,依賴mybatis-parent
<parent> <groupId>org.mybatis</groupId> <artifactId>mybatis-parent</artifactId> <version>29</version> <relativePath /> </parent>
二、下載mybatis-parent
三、將以上兩個工程匯入到idea編譯除錯
匯入後效果如下: 在匯入編譯過程中遇到兩個問題典型的問題: 1、language level報錯 解決辦法:修改project和modules的language level的如下,此處使用jdk1.8
2、java檔案編譯使用的是jdk1.6,程式執行使用的是jdk1.8錯誤 因為mybatis原始碼預設在pom.xml檔案裡面配置的是使用jdk1.6,所以我在mybatis的pom.xml檔案中指定了jdk1.8,如下:
<!-- 指定maven編譯的jdk版本 --> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
3、實際在上編譯過程中還遇到了其它問題,這裡就不一一說明,反正最後就是對pom.xml檔案做了修改之後就可以了 mybatis的pom.xml檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.mybatis</groupId> <artifactId>mybatis-parent</artifactId> <version>29</version> <relativePath /> </parent> <artifactId>mybatis</artifactId> <version>3.4.6</version> <packaging>jar</packaging> <name>mybatis</name> <description> The MyBatis SQL mapper framework makes it easier to use a relational database with object-oriented applications. MyBatis couples objects with stored procedures or SQL statements using a XML descriptor or annotations. Simplicity is the biggest advantage of the MyBatis data mapper over object relational mapping tools. </description> <url>http://www.mybatis.org/mybatis-3</url> <inceptionYear>2009</inceptionYear> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.testTarget>1.8</maven.compiler.testTarget> <maven.compiler.testSource>1.8</maven.compiler.testSource> <maven.compiler.testCompilerArgument>-parameters</maven.compiler.testCompilerArgument> <clirr.comparisonVersion>3.3.1</clirr.comparisonVersion> <findbugs.onlyAnalyze>org.apache.ibatis.*</findbugs.onlyAnalyze> <osgi.export>org.apache.ibatis.*;version=${project.version};-noimport:=true</osgi.export> <osgi.import>*;resolution:=optional</osgi.import> <osgi.dynamicImport>*</osgi.dynamicImport> <maven.surefire.excludeGroups>org.apache.ibatis.test.SlowTests</maven.surefire.excludeGroups> </properties> <dependencies> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.1.16</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.22.0-GA</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> <optional>true</optional> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> <optional>true</optional> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <optional>true</optional> </dependency> <!-- Don't upgrade to 2.4+ until mybatis switches to java 7 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.3</version> <optional>true</optional> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> <optional>true</optional> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.5</version> <optional>true</optional> </dependency> <!-- Test dependencies --> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>4.12.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.3.5</version> <!-- Version 2.4.0 required jdk8 --> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>10.12.1.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.12.0</version> <scope>test</scope> </dependency> <!-- Do not go to 2.x until we are on jdk7 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.2_spec</artifactId> <version>1.0.1.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> <scope>test</scope> </dependency> <!-- postgresql driver is required to run the refcursor tests --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.1.4.jre6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>1.7.1</version> <!-- Stay on 1.7.1 to support Java 6 --> <scope>test</scope> </dependency> <dependency> <groupId>eu.codearte.catch-exception</groupId> <artifactId>catch-exception</artifactId> <version>1.4.4</version> <scope>test</scope> </dependency> <dependency> <groupId>ru.yandex.qatools.embed</groupId> <artifactId>postgresql-embedded</artifactId> <version>2.5</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <testCompilerArgument>${maven.compiler.testCompilerArgument}</testCompilerArgument> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <artifactSet> <includes> <include>org.mybatis:mybatis</include> <include>ognl:ognl</include> <include>org.javassist:javassist</include> </includes> </artifactSet> <relocations> <relocation> <pattern>ognl</pattern> <shadedPattern>org.apache.ibatis.ognl</shadedPattern> </relocation> <relocation> <pattern>javassist</pattern> <shadedPattern>org.apache.ibatis.javassist</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <configuration> <locales>en,es,ja,fr,zh_CN,ko</locales> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <configuration> <excludes> <exclude>org.apache.ibatis.ognl.*</exclude> <exclude>org.apache.ibatis.javassist.*</exclude> </excludes> </configuration> </plugin> <!-- Patch until mybatis-parent 30 (jdk9 build support) --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>animal-sniffer-maven-plugin</artifactId> <version>1.16</version> </plugin> </plugins> <resources> <resource> <directory>${project.basedir}</directory> <targetPath>META-INF</targetPath> <includes> <include>LICENSE</include> <include>NOTICE</include> </includes> </resource> <resource> <directory>${project.build.sourceDirectory}</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <testResources> <testResource> <directory>${project.build.testSourceDirectory}</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </testResource> </testResources> </build> <profiles> <!-- 指定maven編譯的jdk版本 --> <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> </profiles> </project>
mybatis-parent的pom.xml檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-parent</artifactId>
<version>29</version>
<packaging>pom</packaging>
<name>mybatis-parent</name>
<description>The MyBatis parent POM.</description>
<url>http://www.mybatis.org/</url>
<inceptionYear>2010</inceptionYear>
<organization>
<name>MyBatis.org</name>
<url>http://www.mybatis.org/</url>
</organization>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<clirr.comparisonVersion>28</clirr.comparisonVersion>
<gcu.product>${project.name}</gcu.product>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ssZ</maven.build.timestamp.format>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.testTarget>1.6</maven.compiler.testTarget>
<maven.compiler.testSource>1.6</maven.compiler.testSource>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--
| plugins configuration
-->
<checkstyle.plugin>2.17</checkstyle.plugin>
<javadoc.version>2.10.4</javadoc.version>
<surefire.version>2.20</surefire.version>
<findbugs.onlyAnalyze />
<!--
| OSGi configuration properties
-->
<osgi.symbolicName>${project.groupId}.${project.artifactId}</osgi.symbolicName>
<osgi.export>${project.groupId}.*;version=${project.version};-noimport:=true</osgi.export>
<osgi.import>*</osgi.import>
<osgi.dynamicImport />
<osgi.private />
<!-- Animal Sniffer Signature -->
<signature.group>org.codehaus.mojo.signature</signature.group>
<signature.artifact>java16</signature.artifact>
<signature.version>1.1</signature.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<header>${project.basedir}/license.txt</header>
<excludes>
<exclude>**/*.ctrl</exclude>
<exclude>**/*.dat</exclude>
<exclude>**/*.lck</exclude>
<exclude>**/*.log</exclude>
<exclude>**/*maven-wrapper.properties</exclude>
<exclude>.factorypath</exclude>
<exclude>.gitattributes</exclude>
<exclude>mvnw</exclude>
<exclude>mvnw.cmd</exclude>
<exclude>ICLA</exclude>
<exclude>LICENSE</exclude>
<exclude>KEYS</exclude>
<exclude>NOTICE</exclude>
</excludes>
<mapping>
<xml.vm>XML_STYLE</xml.vm>
</mapping>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin-git</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</plugin>
<!-- Antrun here only to override eclipse settings -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</plugin>
<!-- Assembly here only to override eclipse settings -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<optimize>true</optimize>
<!-- Slightly faster builds, see https://issues.apache.org/jira/browse/MCOMPILER-209 -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pdf-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>pdf</id>
<phase>prepare-package</phase>
<goals>
<goal>pdf</goal>
</goals>
<configuration>
<includeReports>false</includeReports>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.6</version>
<executions>
<execution>
<id>attach-descriptor</id>
<goals>
<goal>attach-descriptor</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.trajano.wagon</groupId>
<artifactId>wagon-git</artifactId>
<version>2.0.3</version> <!-- 2.0.4 has jgit errors -->
</dependency>
<!-- Fluido here only for version update checks on site page -->
<dependency>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.12</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- Checkstyle dependencies required here or they won't be used (per maven) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.plugin}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>7.8.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-java</id>
<phase>validate</phase>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>[1.6,)</version>
</requireJavaVersion>
<requireMavenVersion>
<version>[3.2.5,)</version>
</requireMavenVersion>
<requirePluginVersions>
<message>[ERROR] Best Practice is to always define plugin versions!</message>
<banLatest>true</banLatest>
<banRelease>true</banRelease>
<banSnapshots>true</banSnapshots>
<phases>clean,deploy,site</phases>
</requirePluginVersions>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<!--
| Make sure we only use Java6 methods
-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.15</version>
<configuration>
<signature>
<groupId>${signature.group}</groupId>
<artifactId>${signature.artifact}</artifactId>
<version>${signature.version}</version>
</signature>
<annotations>
<annotation>org.apache.ibatis.lang.UsesJava7</annotation>
<annotation>org.apache.ibatis.lang.UsesJava8</annotation>
</annotations>
</configuration>
<executions>
<execution>
<id>check-java-1.6-compat</id>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeDependencies>true</excludeDependencies>
<manifestLocation>${project.build.directory}/osgi</manifestLocation>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
<supportedProjectType>maven-plugin</supportedProjectType>
</supportedProjectTypes>
<instructions>
<!--
| stops the "uses" clauses being added to "Export-Package" manifest entry
-->
<_nouses>true</_nouses>
<!--
| Stop the JAVA_1_n_HOME variables from being treated as headers by Bnd
-->
<_removeheaders>JAVA_1_3_HOME,JAVA_1_4_HOME,JAVA_1_5_HOME,JAVA_1_6_HOME</_removeheaders>
<Bundle-SymbolicName>${osgi.symbolicName}</Bundle-SymbolicName>
<Export-Package>${osgi.export}</Export-Package>
<Private-Package>${osgi.private}</Private-Package>
<Import-Package>${osgi.import}</Import-Package>
<DynamicImport-Package>${osgi.dynamicImport}</DynamicImport-Package>
<Bundle-DocURL>${project.url}</Bundle-DocURL>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestFile>${project.build.directory}/osgi/MANIFEST.MF</manifestFile>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Build-Date>${maven.build.timestamp}</Implementation-Build-Date>
<X-Compile-Source-JDK>${maven.compiler.source}</X-Compile-Source-JDK>
<X-Compile-Target-JDK>${maven.compiler.target}</X-Compile-Target-JDK>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>clirr-maven-plugin</artifactId>
<version>2.8</version>
<configuration>
<comparisonVersion>${clirr.comparisonVersion}</comparisonVersion>
<failOnError>false</failOnError>
<failOnWarning>false</failOnWarning>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.9.5</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.basedir}</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>LICENSE</include>
<include>NOTICE</include>
</includes>
</resource>
</resources>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.12</version>
</extension>
</extensions>
</build>
<reporting>
<plugins>
</plugins>
</reporting>
</project>