mybatis-generator used in two ways: pom-plugin main method and configuration

A, MyBatis Generator Introduction

  1. Mybatis and iBatis is a code generator. The corresponding entities may be generated by a database table MyBatis Generator, sql mapping file, Dao, etc.

And table structure matching Java POJO. This may include:

A table based on the primary key (primary key if the table)

A table based on the non-primary key field (except BLOB fields)

A class table contains BLOB field (if the table has BLOB fields)

A class dynamic query, modify, and delete support

  1. MyBatis or iBATIS SQL-compliant XML mapping file. MBG simple configuration specified for each table generating function CRUD SQL, the SQL statement generated comprising:

By primary key update

By dynamically update where clause

Delete by primary key

By deleting dynamic where clause

By primary key query

By dynamic where clause query

Dynamic where clause of a query by the number of

  1. Notes on using MyBatis Generator in an iterative development process:

If an existing file with the newly generated XML file with the same name exists, MBG will automatically merge XML files, merge, MBG will not overwrite any content you add in the XML file. You can repeat without fear of loss of content generated XML file you add, MBG only replace any XML elements generated by MBG.

MBG will not merge Java files, which can cover different existing file or save a file with a unique name (No. 1,2,3, etc. plus the file name) newly generated. If the cover is repeatedly generated automatically be selected manually replace or merge the changes.

Two, MyBatis Generator implementations

mybatis-generator used in two ways pom-plugin, main method

1. Maven plugin generation

(1)   pom.xml

<build>

        <plugins>

            <plugin>

                <groupId>org.mybatis.generator</groupId>

                <artifactId>mybatis-generator-maven-plugin</artifactId>

                <version>1.3.4</version>

                <configuration>

                    <configurationFile>${basedir}/src/main/resources/mybatis-generator.xml</configurationFile>

                    <overwrite>true</overwrite>

                    <verbose>true</verbose>

                </configuration>

                <dependencies>

                    <dependency>

                        <groupId>mysql</groupId>

                        <artifactId>mysql-connector-java</artifactId>

                        <version>5.1.46</version>

                   </dependency>

             </dependencies>

          </plugin>

      </plugins>

</build>

(2)     src/main/resources/mybatis-generator.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>

    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <property name="beginningDelimiter" value="`"/>

        <property name="endingDelimiter" value="`"/>

 

        <!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">

            <property name="mappers" value="cn.xiaows.app.util.MyMapper"/>

        </plugin>-->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL="jdbc:mysql://localhost:3306/mydb"

                        userId="root"

                        password="123456"/>

        <! - where packets generated for the entity ->

        <javaModelGenerator targetPackage="cn.app.entity0" targetProject="src/main/java"/>

        <! - For the mapper-generated directory ->

        <sqlMapGenerator targetPackage="mapper0" targetProject="src/main/resources"/>

        <! - configuration corresponding mapper java map ->

        <javaClientGenerator targetPackage="cn. app.dao0" targetProject="src/main/java" type="XMLMAPPER" />

        <table tableName="%"/><!-- 所有表 -->

        <!--<table tableName="company" mapperName="CompanyDao"/>-->

        <!--<table tableName="user" mapperName="UserDao"/>-->

        <!--...-->

    </context>

</generatorConfiguration>

(3)   generate command: (maven) or directly to the generator running maven plugin

mybatis-generator:generate -f pom.xml

2. The method of generating a main

(1)   pom.xml

 

<dependencies>

        <!-- mysql -->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.46</version>

        </dependency>

        <!-- mybatis-generator -->

        <dependency>

            <groupId>org.mybatis.generator</groupId>

            <artifactId>mybatis-generator-core</artifactId>

            <version>1.3.4</version>

            <scope>compile</scope>

            <optional>true</optional>

        </dependency>

    </dependencies>

(2)   MybatisGeneratorMain.java

public class MybatisGeneratorMain {

    public static void main(String[] args) throws Exception {

        List<String> warnings = new ArrayList<>();

        ConfigurationParser cp = new ConfigurationParser(warnings);

        Configuration config = cp.parseConfiguration(new File("app-logs-web/src/main/resources/mybatis-generator.xml"));

        // Configuration config = cp.parseConfiguration(ClassLoader.getSystemResourceAsStream("mybatis-generator.xml"));

        DefaultShellCallback callback = new DefaultShellCallback(true);

        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

        myBatisGenerator.generate(null);

        for (String warning : warnings) {

            System.out.println(warning);

        }

    }

}

(3)   mybatis-generator.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>

    <context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <property name="beginningDelimiter" value="`"/>

        <property name="endingDelimiter" value="`"/>

        <!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">

            <property name="mappers" value="cn.xiaows.app.util.MyMapper"/>

        </plugin>-->

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

                        connectionURL="jdbc:mysql://localhost:3306/mydb"

                        userId="root"

                        password="123456">

        </jdbcConnection>

        <! - where packets generated for the entity ->

        <javaModelGenerator targetPackage="cn.xiaows.app.entity" targetProject="app-logs-web/src/main/java"/>

        <! - For the mapper-generated directory ->

        <sqlMapGenerator targetPackage="mapper" targetProject="app-logs-web/src/main/resources"/>

        <! - configuration corresponding mapper java map ->

        <javaClientGenerator targetPackage="cn.xiaows.app.dao" targetProject="app-logs-web/src/main/java" type="XMLMAPPER" />

        <table tableName="%"/><!-- 所有表 -->

        <!--<table tableName="user" mapperName="UserDao"/>-->

        <!--<table tableName="project" mapperName="ProjectDao"/>-->

        <!--...-->

    </context>

</generatorConfiguration>

(4)       springboot startup class: AppLogApplication.java

@SpringBootApplication

@MapperScan("cn.app.dao")

public class AppLogApplication {

    public static void main(String[] args) {

        SpringApplication.run(AppLogsWebApplication.class, args);

    }

}

Guess you like

Origin www.cnblogs.com/newxu/p/12185068.html