Construction of MyBatis environment

Posted by asuperstar103 on Thu, 27 Jan 2022 23:16:36 +0100

II Construction of MyBatis environment

Build basic environment

First, create a maven project on idea

After you name it, it looks like this

Go to the following website to get the required jar package

mvnrepository.com

After entering, the page is as follows

Find the jar package you need here

For single table query, the following jar packages are required:

fastjson,mysql,mybatis,junit

Let's give an example of how to quote packages
First, we go to the mvnrepository website to search the jar package of fastjson

Click search and the following page will appear
We click on the icon
If we choose a newer version, we'd better use the one with more items. Here we choose version 1.2.75

Click enter to jump to this page
We just need to copy the content in the red box and paste it into the POM of our idea project XML file

Click import changes again to import successfully!

According to the above steps, we import these jar packages in turn
Note: in POM When pasting addresses in XML, you need to fill in < dependencies > between code lines

<?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>MyBatis</groupId>
    <artifactId>MyBatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>

</project>

Create a new file named mybatisgen under disk C

For example: C:\Users\yixin\mybatisgen

Paste the generator and mybatis files in

Find your generator XML document, I choose to open it with notepad + +

We have several areas that need to be revised

First place

Find the location of your mysql jar package and replace the current location

Second place

The part with the horizontal line is the library. For example, my library is called user library, so this place will be renamed user

Third place

The place where the horizontal line is drawn is the location of the model layer and mapper layer you want to create respectively, which should be consistent with the directory when you create the project at that time. The following is the location of your mybatisgen

Fourth

After type, select ANNOTATEDMAPPER or XMLMAPPER. Here are two project modes, xml document or annotation. Let's first select xml mode, which is located in the mapper layer

Fifth place

It's the table name of your database. I'm here for the user table operation

After modification, enter the cmd page to perform the following operations
In fact, the above words are generated by java model, mapper layer and so on

The horizontal line indicates success

Then enter mybatisgen

Right click show in explorer in java

Copy these two files from mybatis to
Go in java

Such a basic mybatis environment will be built

Topics: Mybatis