Beauty of Mybatis source code: 1.2 download Mybatis source code and import it into IDE tool

Download Mybatis source code and import it into IDE tool

Download the source code of Mybatis

Open the browser and enter the web address https://github.com/mybatis/mybatis-3 You can access Mybatis' warehouse in Github.

So far, the latest version of mybatis is 3.5.3-SNAPSHOT, but because the version I used in my previous learning process is 3.5.0-SNAPSHOT, this note is still written for 3.5.0-SNAPSHOT.

Environment dependent installation

  • jdk(1.8+)

    Because mybatis3 uses some of the new features of JDK 1.8, you need to use JDK 1.8 + here.

    Now you need to log in to download the version of JDK8 + through the official website.

    • Get jdk

    Click to enter Oracle JDK Download Interface

    Windows Click to download Oracle JDK 8u181

    $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession;
    $cookie = New-Object System.Net.Cookie;
    $cookie.Name = "oraclelicense";
    $cookie.Value = "accept-securebackup-cookie";
    $cookie.Domain = ".oracle.com";$session.Cookies.Add($cookie);
    iwr  -Method GET -WebSession $session -uri https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz  -OutFile jdk.tar.gz
    

    linux Click to download Oracle JDK 8u181

    wget  -nc --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz
    

    mac Click to download Oracle JDK 8u181

  • maven

    • windows

      • Download Maven

        Click to enter maven's download page

        Click to download maven TAR GZ

      • Extract the installation package and make a simple configuration

        Click download decompression tool: 7zip

        Extract the obtained Maven compressed file and get the directory named apache-maven-3.6.1. Move it to the specified folder according to your own needs. The internal files are roughly as follows:

      Enter the conf directory and simply edit the settings.xml File:

      <?xml version="1.0" encoding="UTF-8"?>
      <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <!--
      to configure maven The local warehouse stores the address. Try to configure the address to avoid downloading jar Bags stored in C Inside the disk.
      If yes no windows System, according to their own needs to change the parameters.
      -->
      <localRepository>F:\data\repository</localRepository>
      <mirrors>
      <!--
      Configure Alibaba cloud central warehouse to improve download jar Packet speed
      -->
      <id>nexus-public-snapshots</id>
      <mirrorOf>*</mirrorOf>
      <mirrorOf>public-snapshots</mirrorOf>
      <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
      </mirror>
      </mirrors>
      </settings>
      
      • Add environment variable

        This step determines whether to add to the environment variable according to your own needs.

        First return to the root directory of maven project:

      While holding down the shift key of the keyboard, right-click and select here to open the Powerschell window.

      In the pop-up Powerschell window, execute the following commands (directly copy the following four commands at once):

      $command='-noprofile -command ' + "`$current=`'$pwd`'" + ';[environment]::SetEnvironmentVariable(""""MVN_HOME""",$current,""""machine"""");'
      
      Start-Process -FilePath powershell.exe   -ArgumentList $command  -Verb runas;
      
      $command='-noprofile -command [environment]::SetEnvironmentVariable(""""path""",$env:path+"""";%MVN_HOME%\bin;"""",""""machine"""");'
      
      Start-Process -FilePath powershell.exe   -ArgumentList $command  -Verb runas;
      

      After the command is executed, if no error is reported, reopening a Powershell window to execute mvn -v will print out the version information of the maven project.

    • linux

        wget -nc http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
        tar -zxvf apache-maven-3.6.1-bin.tar.gz
        #Configure environment variables
        mv apache-maven-3.6.1 /usr/local/maven
        maven_env_exist=$(awk '{print}' /etc/profile |grep "#MAVEN_SETTINGS@JPANDA" &>>/dev/null;echo $?)
        if [ $maven_env_exist -gt 0 ]
          then
            echo "
        #MAVEN_SETTINGS@JPANDA
        export M2_HOME=/usr/local/maven
        export PATH=\$M2_HOME/bin:\$PATH
        #MAVEN
        ">>/etc/profile
        fi
        ln -s /usr/local/maven/bin /usr/local/bin/maven
        source /etc/profile
      

      Enter the directory / usr/local/maven/conf, and simply edit the settings.xml File:

      <?xml version="1.0" encoding="UTF-8"?>
      <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <!--
      to configure maven The local warehouse stores the address. Try to configure the address to avoid downloading jar Bags stored in C Inside the disk.
      If yes no windows System, according to their own needs to change the parameters.
      -->
      <localRepository>/data/maven/repository</localRepository>
      <mirrors>
      <!--
      Configure Alibaba cloud central warehouse to improve download jar Packet speed
      -->
      <id>nexus-public-snapshots</id>
      <mirrorOf>*</mirrorOf>
      <mirrorOf>public-snapshots</mirrorOf>
      <url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
      </mirror>
      </mirrors>
      </settings>
      

      Enter the command mvn -v to view the version information of the current maven project.

  • git

    • windows

      Click the Next button to enter the component selection interface.

      Select components according to your own requirements. Here, I select all components except the last update check Check daily for Git for windows updates.

      Click the Next button and select the corresponding start menu folder.

      Here I use the default Git directory and click the Next button to enter the Git editor selection interface.

      Here I choose to use VIM, and then click Next all the way to continue.

      Finally, click the Install button to complete the final installation operation.

    • linux

      Perform the following command to complete the installation:

      yum install -y git
      

Get Mybatis source code and import it into IDE tool

  • step1: get mybatis source code

    There are two ways to obtain the source code of Mybatis: one is to obtain the official source code of Mybatis through github, and the other is to obtain the source code translated into Chinese through gitee.

    Here, the two sources are the same at the code level. The difference is only that I added a lot of Chinese comments for the source code on gitee.

    Execute the git command to clone the source code of Mybatis in your own working directory (one of two options):

    • 1 Get the official source code of mybatis through github. Since it is too slow to visit github in China, I found the source code of mybatis in giteefork, so the following two commands get the same code.
    git clone -b mybatis-3.5.0 https://github.com/mybatis/mybatis-3.git
    //perhaps
    git clone -b mybatis-3.5.0 https://gitee.com/topanda/mybatis-3-original.git
    

    The function of this command is to clone the Mybatis source code labeled mybatis-3.5.0 to the current directory.

    • 2: Get the source code translated into Chinese through gitee
    git clone https://gitee.com/topanda/mybatis-3.git
    

    The function of this command is to clone the Mybatis source code to the current directory.

    At this time, there should be another file directory named mybatis-3-original or mybatis-3 in our working directory.

    The name of the directory depends on the git command used. If the English source code is obtained through gitee, the name is mybatis-3-original. Otherwise, it is mybatis-3

  • step2: import source code to IDE tool

    After obtaining the source code of mybatis, we can import the source code into our common IDE tools. Because I often use IntelliJ IDEA, it is recommended to use IntelliJ IDEA here. Of course, you can still choose your favorite tools according to your own preferences.

    • Import project to IDEA

    The IntelliJ IDEA version used here is IntelliJ IDEA 2018.2 (Ultimate Edition)

    Click the File button in the upper left corner of the IDEA, and then select the Open button.

    Then select the directory of Mybaits source code in the pop-up box, and click OK to import the source code into IDEA.

    After importing the source code, wait for the IDEA to compile.

    • Import project to Eclipse

    The version of Elipse used here is the Elipse photo release (4.8.0)

    Click the File button in the upper left corner of Eclipse, and select open procedures from File system

    Click the Directory button in the pop-up box, select the Directory where the Mybaits source code is located, and click OK. Wait for Eclipse verification to pass, and click Finish in the lower left corner to complete the import.

    At this time, we execute the mvn clean compile command in the root directory of the project to complete the source code compilation.

Modify POM file and add dependency for later use

Modify the pom.xml File, add the following code to its dependencies code block:

<!-- MySQL relevant -->
  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
    <scope>test</scope>
  </dependency>
  <!--add to lombok rely on-->
  <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.6</version>
    <scope>provided</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.56</version>
    <scope>test</scope>
  </dependency>

The only thing to note here is that the version number of Mysql connector Java needs to be consistent with the Mysql database used.

Add a new unit test package for later learning

In the src/test/java/org/apache / directory of mybatis project, create a new sub package named learning, which will be used to store the test classes for later learning.

Adjust log level

Modify the log4j.properties configuration file in the src/test/java/resources / directory of mybatis project.

### The global log level is ERROR
log4j.rootLogger=ERROR, stdout
### The log level in mybatis is INFO level
log4j.logger.org.apache.ibatis=INFO
### The package log level used by user-defined learning is DEBUG, and the purpose of reducing the level to DEBUG is to print SQL content
log4j.logger.org.apache.learning=DEBUG

log4j.logger.org.apache.ibatis.session.AutoMappingUnknownColumnBehavior=WARN, lastEventSavedAppender

### Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

log4j.appender.lastEventSavedAppender=org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$LastEventSavedAppender

Pay attention to me and learn more together

Tags: Programming Maven Mybatis Apache git

Posted on Wed, 20 May 2020 23:18:46 -0700 by ninib