Maybatis Reverse Engineering

1 Configure 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>com.xingxue.genernater</groupId>
    <artifactId>genernater-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- Configure mybatis core dependencies -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <!-- Import log4j log package -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>
    </dependencies>


    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <!--Reverse-engineered plugins-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

Two placement generator.properties

//Configure generator.properties
jdbc.connectionURL=jdbc:mysql:///xingxue27
jdbc.userId=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver

Three configuration generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--Import property configuration-->
    <properties resource="generator.properties"></properties>

    <!--Specify the location of the jdbc driver jar package for a specific database-->
    <!--Path to find your own installation path--> //The place to be changed
    <classPathEntry location="C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.44/mysql-connector-java-5.1.44.jar"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional, designed to control annotations when creating a class -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc database connection-->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}">
        </jdbcConnection>


        <!-- optional, type handler, conversion control between database types and java types -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model model generator, used to generate classes with primary keys, record classes and query Example classes
            targetPackage specifies the package name where the generated model is generated
            targetProject specifies the path under the project
        -->
        <javaModelGenerator targetPackage="com.xingxue.mybatis.model"//The place to be changed
                            targetProject="src/main/java">

            <!-- Whether to allow subpackages, ie targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- Whether to add a constructor to the model -->
            <property name="constructorBased" value="true"/>
            <!-- Whether to trim the data of the CHAR-like column -->
            <property name="trimStrings" value="true"/>
            <!-- Whether the created Model object is immutable, that is, the generated Model object will not have a setter method, only a constructor method -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--The directory where the Mapper mapping file is generated generates the corresponding SqlMap file for each database table -->
        <sqlMapGenerator targetPackage="com.xingxue.mybatis.mapper"//The place to be changed
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
<!-- Client code, generate easy-to-use code for Model object and XML configuration file type="ANNOTATEDMAPPER", generate Java Model and annotation-based Mapper object type="MIXEDMAPPER", generate annotation-based Java Model and corresponding The Mapper object type="XMLMAPPER" generates SQLMap XML files and independent Mapper interfaces --> <javaClientGenerator targetPackage="com.xingxue.mybatis.mapper"//The place to be changed targetProject="src/main/java" type ="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--Database table name--> <table tableName="tb_class" domainObjectName="ClassModel"//To be changed where enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> < /table> </context></generatorConfiguration>

Four help tools

package com.xingxue.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionUtil {

    private static final SqlSessionFactory factory;


    static {
        try {
            factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml"));
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    public  static SqlSessionFactory getFactory(){
        return factory;
    }


    public static SqlSession getSession(){

        return factory.openSession();
    }
}

 Click on the plugin to automatically generate mapper and model  

final test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850044&siteId=291194637