Micro Services Architecture ------ episode Mybatis reverse engineering

1. The first is the pom.xml, we need to need to introduce a plug-mvn

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hello.spring.boot</groupId>
    <artifactId>hellosb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hellosb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>${hikaricp.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apach.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>3.4.4</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

mybatis generator  

configuration: is the location and configuration rules plug-in configuration file, if you do not write configuration position, the system will default generationConfig.xml in the resources folder under the file name
dependencies: a plug-in to run the dependent jar package

2. The configuration file structure

 

 This is just my personal configuration file structure, and newcomers can learn from it

3.generatorConfig.xml profile

<?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>
    <!-- 引入数据库连接配置 -->
    <properties resource="jdbc.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 配置 tk.mybatis 插件 -->
        <type = plugin "tk.mybatis.mapper.generator.MapperPlugin"> 
        <-! <-! Configuration Database Connection -> ->
            <property name="mappers" value="tk.mybatis.mapper.MyMapper"/>
        </ plugin>

        <!--<jdbcConnection-->
                <!--driverClass="com.mysql.cj.jdbc.Driver"-->
                <!--connectionURL="jdbc:mysql://192.168.2.18:3306/ims?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC"-->
                <!--userId="root"-->
                <!--password="root">-->
        <!--</jdbcConnection>-->
        <!-- 配置数据库连接 -->
        <!-- 生成的Java文件的编码 -->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
        <-! entity configuration class storage path ->
        </ JdbcConnection>
                userId="${jdbc.username}"
                = password "$ {} jdbc.password">

        <javaModelGenerator targetPackage = "com.hello.spring.boot.domain" targetProject = "the src / main / Java" /> 

        <-! configuration XML storage path -> 
        <sqlMapGenerator targetPackage = "Mapper" targetProject = "the src / main / Resources "/> 

        <-! configuration storage path DAO -> 
        <javaClientGenerator 
                targetPackage =" com.hello.spring.boot.mapper " 
                targetProject =" the src / main / Java " 
                type =" XMLMAPPER "/> 

        <! - - need to specify the configuration database and generate tables, tables representing all% -> 
        <table Catalog = "IMS" tableName = "ids_signal_model_t"> ids_signal_model_t "> 
            <-! MySQL Configuration ->
            <generatedKey column = "ID" = sqlStatement the "Mysql"identity="true"/>
        </table>
    </context>
</generatorConfiguration>

  These are my basic configuration. Complete configuration in detail as follows

<? xml Version = "1.0" encoding = "UTF-8"?> 
<DOCTYPE generatorConfiguration! 
  the PUBLIC "- // mybatis.org//DTD the Configuration MyBatis Generator 1.0 // EN" 
"http://mybatis.org/dtd /mybatis-generator-config_1_0.dtd "> 
<-! configuration generator -> 
<generatorConfiguration> 
<-! CI or may be used to load the configuration files, can be used in $ {propertyKey} entire configuration file way to reference the configuration items 
    resource: load address allocation of resources, the use of resource, MBG start looking from the classpath, such as COM / myproject / generatorConfig.properties         
    url: allocation of resources to load geology, using the URL of ways, such as file: /// C: / . myfolder / generatorConfig.properties 
    noted that a location only two attributes; 
    Further, if a mybatis-generator-maven-plugin, properties defined in pom.xml then can be used directly in the generatorConfig.xml 
<properties resource = "" url = "" />
 -> 
 
 <! - when MBG work, you need to load additional dependencies
     loading the full path specified location attribute jar / zip package 
<classpathentry location = "/ Program Files / the IBM / the SQLLIB / Java / the db2java.zip" /> 
  -> 
 
<-! 
    context: the environment to generate a set of 
    id: Required is selected, the context id, for alerts when errors 
    defaultModelType: the specified style generation target 
        1, conditional: similar Hierarchical; 
        2, Flat: all content (primary key, BLOB) and all generated in an object; 
        . 3, Hierarchical: primary key generating a XXKey objects (key class), Blob object and the like generated separately, other simple attributes in an object (Record class) 
    targetRuntime: 
        . 1, MyBatis 3: default value, generated based on the content MyBatis3.x above, comprising XXXBySample; 
        2, MyBatis3Simple: MyBatis 3 is similar to, but does not generate XXXBySample; 
    introspectedColumnImpl: Fully qualified class name, for expanding the MBG 
-> 
<context ID = "MySQL" defaultModelType = "Hierarchical" = targetRuntime "MyBatis3Simple" >
 
    <! - automatic identification database key, default false, if set to true, according to the keyword list SqlReservedWords defined; 
        generally leave the default values encountered database key (Java keyword) using a covering columnOverride 
     -> 
    < name = Property "autoDelimitKeywords" value = "to false" /> 
    <-! encoded file generated Java -> 
    <Property name = "javaFileEncoding" value = "UTF-. 8" /> 
    <-! java code format -> 
    <Property name = "javaFormatter" value = "org.mybatis.generator.api.dom.DefaultJavaFormatter" /> 
    <-! format XML code -> 
    <Property name = "XMLFormatter" value = "ORG. mybatis.generator.api.dom.DefaultXmlFormatter "/> 
 
    ! <- beginningDelimiter and endingDelimiter: a tag database object name specified in the symbol database, such as ORACLE is double quotes, MYSQL Based default is` backquote; ->
    <property name="beginningDelimiter" value="`"/>
    <Property name = "endingDelimiter" value = "` "/> 
 
    <- must have, use this link to configure the database! 
        @TODO: Can I extend 
        # MySQL 8.x: com.mysql.cj.jdbc.Driver 
     - -> 
    <JdbcConnection driverClass = "com.mysql.jdbc.Driver" the connectionURL = "JDBC: MySQL: /// PSS" = the userId "the root" password = "ADMIN"> 
        <- this property may be provided inside the property, each! a property attribute are set to the Driver configuration -> 
    </ JdbcConnection> 
 
    <- Java processor type! 
        for processing types to the Java DB in the type, default JavaTypeResolverDefaultImpl; 
        note that attempts to use default Integer, Long, Short, and the like corresponding to DECIMAL NUMERIC data type; 
    -> 
    <javaTypeResolver type = "org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
        <!-- 
            true: Use BigDecimal DECIMAL and NUMERIC data type corresponds to 
            false: default, 
                Scale> 0; length> 18 is: Use BigDecimal; 
                Scale = 0; length [10,18]: Use Long; 
                Scale = 0; length [5,9]: use Integer; 
                Scale = 0; length <5: use Short; 
         -> 
        <Property name = "forceBigDecimals" value = "false" /> 
    </ javaTypeResolver> 
 
 
    <- the Java model builder, is an essential element to the! 
        responsible : 1, key categories (see the context of defaultModelType); 2, java class; 3, the query type 
        :; targetPackage generated class to put the package, the real package by enableSubPackages property control 
        under the target project, specify an existing directory: targetProject generated content will be placed in the specified directory, if the directory does not exist, MBG does not automatically building the directory 
     ->
    <javaModelGenerator targetPackage = "com._520it.mybatis.domain" targetProject = "the src / main / Java"> 
        <-! for MyBatis 3 / MyBatis3Simple 
            automatically creates a constructor generated for each class constructor contains all of the field ; instead of using the setter; 
         -> 
        <Property name = "constructorBased" value = "to false" /> 
 
        <- on the basis targetPackage regenerates the database schema according to one package, finally generated based on the package! under the default is false -> 
        <Property name = "enableSubPackages" = value "to true" /> 
 
        <-! for MyBatis 3 / MyBatis3Simple 
            whether to create an immutable class, if true, 
            then the MBG will not create a setter method class, instead constructorBased similar class 
         -> 
        <Property name = "the immutable" value = "to false "/> 
 
        <-! setting a root object,
            If this root object is set, the generated keyClass or recordClass will inherit this class; you can override this option rootClass properties in Table 
            NOTE: If you have the same root class properties in key class or the record class, MBG will not again generate these properties, including: 
                1, the same attribute name, the same type, have the same getter / setter methods; 
         -> 
        <property name = "rootClass" value = "com._520it.mybatis.domain.BaseDomain" /> 
 
        < ! - set whether getter method of the String type field called trim () method -> 
        <Property name = "trimStrings" value = "to true" /> 
    </ javaModelGenerator> 
 
 
    <-! generate SQL map XML file Builder, 
        note that after Mybatis3, we can use mapper.xml file + Mapper interfaces (interfaces or without mapper), 
            or just use Mapper Interface + Annotation, so if javaClientGenerator configuration configured need to generate XML, then this element will you must configure 
        targetPackage / targetProject:同javaModelGenerator
     -->
    <sqlMapGenerator targetPackage = "com._520it.mybatis.mapper" targetProject = "the src / main / Resources"> 
        <-! targetPackage on the basis, according to a database schema regenerated layer package, finally generated based on the package the default is to false -> 
        <Property name = "enableSubPackages" = value "to true" /> 
    </ sqlMapGenerator> 
 
 
    <-! mybatis for it, i.e. generating Mapper interfaces, note that if the element is not configured, then the default does not generate Mapper interfaces 
        targetPackage / targetProject: with javaModelGenerator 
        of the type: select how to generate mapper interfaces (in / MyBatis3Simple under MyBatis 3): 
            1, ANNOTATEDMAPPER: use Mapper will generate the interface and create (SQL generated annotation in) Annotation way, not generating the corresponding XML; 
            2, MIXEDMAPPER: using a mixed configuration, the interface generates Mapper, and the Annotation appropriately adding suitable, but will generate XML in the XML; 
            . 3, XMLMAPPER: generates Mapper interface, the interface is completely dependent on XML;
        Note that if the context is MyBatis3Simple: only support ANNOTATEDMAPPER and XMLMAPPER 
    -> 
    <javaClientGenerator targetPackage = "com._520it.mybatis.mapper" of the type = "ANNOTATEDMAPPER" targetProject = "src / main / the Java"> 
        <- in targetPackage of! basis, according to a database schema regenerated layer Package, finally generated based on this Package, defaults to false -> 
        <Property name = "enableSubPackages" = value "to true" /> 
 
        <-! may be all generated interface to add a parent interface, but is only responsible for generating MBG not responsible for checking the 
        <Property name = "rootInterface" value = "" /> 
         -> 
    </ javaClientGenerator> 
 
    <!- Select a table to generate documents, there may be one or more table, the table element must be 
        selected at the file table is generated: 
        . 1, the SQL file Map 
        2, generate a primary key class; 
        3, and in addition to primary key BLOB class other fields; 
        4, comprising BLOB class;
        5, a user generates a dynamic query condition type (selectByExample, deleteByExample), optional; 
        . 6, Mapper interface (optional) 
        tableName (necessary): To generate a table object; 
        Note: The case sensitive issues. Normally, the case will automatically MBG database identifier to identify the sensitivity, in general, will MBG 
            according to the settings schema, catalog or tablename to query the data tables, the following process: 
            1, if the schema, catalog or tablename has spaces, set what format you use to specify the exact format of the case to make inquiries; 
            2, otherwise, if the identifier of the database in uppercase, then MBG table name automatically capitalized and then look for; 
            3. otherwise, if the identifier database lowercase, then automatically MBG table names to lowercase and then find; 
            4, otherwise, use the case specified format query; 
        in addition, if at the time of creation of the table, use " "the provisions of database objects case, even if a database identifier is used uppercase, in which case the case will be given to create a table to use; 
        this time, set delimitIdentifiers =" true "to preserve case format; 
        optional: 
        . 1, Schema: database Schema; 
        2, Catalog: Catalog database;
        3, alias: Alias data table is set, if you set the alias, then all of the SELECT SQL statements generated, the column name becomes: alias_actualColumnName 
        4, domainObjectName: the name of the generated domain class, if not set directly table name as a domain name like; can be set to somepck.domainName, it will automatically domainName somepck class and then put inside the package; 
        5, EnableInsert (default true): Specifies whether to generate insert statements; 
        6, enableSelectByPrimaryKey (default true): Specifies whether generated as the primary key query object statement (that getById or GET); 
        . 7, enableSelectByExample (default true): MyBatis3Simple is false, specifies whether to generate a dynamic query statement; 
        . 8, enableUpdateByPrimaryKey (default true): Specifies whether to generate modified in the primary key of the object statement (i.e., Update); 
        . 9, enableDeleteByPrimaryKey (default true): Specifies whether to generate the primary key to delete the object according to statement (i.e. delete); 
        10, enableDeleteByExample (default true): MyBatis3Simple is false, specifies whether to generate dynamic deleted Statements; 
        11, enableCountByExample (default true): MyBatis3Simple is false, specify whether to generate dynamic query statement the total number of pieces (total number of strips for paging query);
        12, enableUpdateByExample (default true): MyBatis3Simple is false, specifies whether to generate a dynamic modification statement (only modify the object attribute is not empty); 
        13 is, modelType: defaultModelType context reference elements corresponding to coverage; 
        14, delimitIdentifiers: Reference tableName explanation, note that the default delimitIdentifiers double quotes, if like this database MYSQL, using the `(backtick, you also need to set the context of beginningDelimiter and endingDelimiter property) 
        15, delimitAllColumns: set whether all the generated SQL in column names use the identifier in quotes. The default is false, delimitIdentifiers context attribute reference 
        note, table there are many parameters of a default attribute replication javaModelGenerator, context of other elements; 
     -> 
    <Table tableName = "UserInfo"> 
 
        <- Reference javaModelGenerator properties of constructorBased! -> 
        <Property name = "constructorBased" value = "to false" /> 
 
        <- defaults to false, if set to true, the generated SQL, table name or catalog not add schema;! ->
        <Property name = "ignoreQualifiersAtRuntime" value = "to false" /> 
 
        <-! Reference javaModelGenerator the immutable properties -> 
        <Property name = "immutable" value = "to false" /> 
 
        <-! Specifies whether to generate only the domain class If set to true, only generated domain class, if it is configured sqlMapGenerator, then the mapper XML file generated only resultMap element -> 
        <Property name = "modelOnly" value = "to false" /> 
 
        <-! reference the properties javaModelGenerator rootClass 
        <property name = "rootClass" value = "" /> 
         -> 
 
        <-! reference javaClientGenerator properties of rootInterface 
        <property name = "rootInterface" value = "" /> 
        -> 
 
        <!- If set runtimeCatalog, then the generated SQL, using the specified catalog, catalog rather than on the table element 
        <property name = "runtimeCatalog" value = "" />
        -> 
 
        <! - If set runtimeSchema, then the generated SQL, using the specified schema, rather than on the table schema element 
        <Property name = "runtimeSchema" value = "" /> 
        -> 
 
        <! - If set runtimeTableName, then the generated SQL, using the specified tablename, rather than on the table element tablename 
        <Property name = "runtimeTableName" value = "" /> 
        -> 
 
        <-! Note that only for a useful property MyBatis3Simple; 
            if the runtime is MyBatis3Simple selected, then the method will generate a SelectAll, if selectAllOrderByClause specified, it will add the specified conditions in this order in the SQL; 
         -> 
        <property name = "selectAllOrderByClause" value = "Age desc, asc username" /> 
 
        <!- If set to true, the generated class model will use a name column itself, and will not use camel naming method, such as BORN_DATE, the attribute name is generated BORN_DATE, but will not be bornDate ->
        <Property name = "useActualColumnNames" value = "to false" /> 
 
 
        <-! generatedKey generating method for generating a primary key, 
            if the element is set, the MBG is generated in the generated <insert> element in a correct <selectKey> element that optional 
            column: the column name primary key; 
            sqlStatement: selectKey statement to be generated, the following options: 
                Cloudscape: selectKey equivalent of SQL is: VALUES IDENTITY_VAL_LOCAL () 
                the DB2: the equivalent of SQL selectKey is: VALUES IDENTITY_VAL_LOCAL () 
                DB2_MF: selectKey equivalent of SQL is: the IDENTITY_VAL_LOCAL the SELECT () the FROM SYSIBM.SYSDUMMY1 
                Derby: selectKey equivalent of SQL is: the IDENTITY_VAL_LOCAL VALUES () 
                HSQLDB: selectKey equivalent of SQL is: CALL IDENTITY ()
                Informix: selectKey equivalent of SQL is: SELECT the dbinfo ( 'sqlca.sqlerrd1') =. 1 from the systables WHERE TABID 
                MySql: selectKey the SQL equivalent is: the LAST_INSERT_ID the SELECT () 
                SqlServer: selectKey equivalent of SQL is: SELECT SCOPE_IDENTITY () 
                SYBASE: selectKey the SQL equivalent is: the SELECT @@ the IDENTITY 
                the JDBC: means adding useGeneratedKeys = "true" and the insert element in property keyProperty generated 
        <generatedKey column = "" sqlStatement the = "" /> 
         -> 
 
        <-! - 
            this element prior to calculating the object properties according to the table name column name renaming the column names, are very suitable for the common prefix string when the columns in the table, 
            such as the column name: CUST_ID, CUST_NAME, CUST_EMAIL, CUST_ADDRESS like; 
            then it can be set to searchString "^ CUST_", and replaced with a blank, Customer object is then generated attribute name is not
            custId, custName the like, but is replaced by the first ID, NAME, EMAIL, then becomes attributes: id, name, email; 
            noted, the MBG is used to replace searchString java.util.regex.Matcher.replaceAll and replaceString, and 
            if columnOverride element used, this attribute is not valid; 
        <columnRenamingRule the searchString = "" ReplaceString = "" /> 
         -> 
 
 
         ! <- used to modify the properties of a column of the table, uses the MBG column modified to generate the domain property; 
             column: the column name to be re-set; 
             Note that there may be a plurality of table elements columnOverride elements Ha ~ 
          -> 
         <columnOverride column = "username"> 
             <- column using the property attribute to specify the attributes to be generated! name -> 
             <Property name = "Property" value = "userName" /> 
 
             <!- javaType specifies the attribute type domain generated, using the fully qualified type name 
             <property name = "javaType" value = "" />
              -> 
 
             <-! The jdbcType for the JDBC type column 
             <Property name = "the jdbcType" value = "" /> 
              -> 
 
             <-! TypeHandler used to specify the column to TypeHandler, if you want to specify , the fully qualified name of the type of processor configuration 
                 noted, the MyBatis not generated in the mybatis-config.xml typeHandler 
                 will generate similar: where id = # {id, jdbcType = BIGINT, typeHandler = com._520it.mybatis. parameter description MyTypeHandler} 
             <Property name = "the jdbcType" value = "" /> 
             -> 
 
             <-! delimitAllColumns configure the reference table element, defaults to false 
             <Property name = "delimitedColumnName" value = "" /> 
              - > 
         </ columnOverride>
 
         ! <- ignoreColumn set MGB ignored a column, if the column is set to change, then the domain generated, the generated SQL, this column will not be appear 
             column: specifies the name of the column to be ignored; 
             delimitedColumnName: Reference delimitAllColumns table element is disposed, the default is false 
             note, there may be a plurality of table elements ignoreColumn elements 
         <ignoreColumn column = "deptId" delimitedColumnName = "" /> 
         -> 
    </ table> 
 
</ context> 
 
</ generatorConfiguration>

  These are the move from Li Weimin teacher over there, just in case attach a description link   https://www.funtl.com/zh/spring-boot-mybatis/%E4%BD%BF%E7%94%A8-MyBatis -% E7% 9A% 84- Maven-% E6% 8F% 92% E4% BB% B6% E7% 94% 9F% E6% 88% 90% E4% BB% A3% E7% A0% 81.html #% E5% AE% 8C% E6% 95% B4% E9% 85% 8D% E7% BD% AE% E6% A1% 88% E4% BE% 8B

4. Data source configuration

 

 FIG source configuration data, may encounter a problem when generating the code, the database system will be given connection, this time must be added to the set parameters are marked yellow server's time zone 

Could not create connection to database server. Attempted reconnect 3 times. Giving up.

5. Generate domain mapper and

 1. Click the tool can be used to generate idea

  

 

 2 can be used as command, particularly preferably an -e can view the generation process or a process error in the doc interface may of course be without

mvn mybatis-generator:generate -e

 6. problems encountered

  Q: generating java code encoding format is GBK

  A: A Options command plus -Dfile.encoding = UTF-8.

 

Guess you like

Origin www.cnblogs.com/zmeRecord/p/11772660.html