zoukankan      html  css  js  c++  java
  • 使用mybatisgenerator 辅助工具逆向工程

    使用mybatisgenerator 辅助工具生成单表的dao层接口,mapper xml 文件以及实体类,复杂的还得人手动去编写哈。。。所以我也不觉得这玩意儿在项目简单情况下有什么鸟用。。。 whatever

    由于这个执行会造成与已有项目文件重名的情况,所以,一般我们单独建立一个项目来做逆向工程

    老规矩,先上最后整体结构图:

    下面 step by step:

    1、 首先按照这篇 教程 搭建一个 架子出来:

    2、 按上图把 下面的 文件分别创建出来放到对应的 文件夹下去 (com 以及 com 子文件夹目录里的东西除外,这个由后面的代码自动生成,也就是我们要的结果

    <?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.ghc</groupId>
        <artifactId>mybatis-generator</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
            </dependency>
        </dependencies>
    
    
        <build>
            <finalName>ghc</finalName>
            <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>
    pom.xml
    jdbc.driverLocation=C:\Users\FrankLi\.m2\repository\mysql\mysql-connector-java\5.1.24\mysql-connector-java-5.1.24.jar
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.connectionURL=jdbc:mysql:///gom
    jdbc.userId=root
    jdbc.password=Mede645
    数据库配置文件generator.properties

    上面的 内容请自行改为自己的 配置信息

    <?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="generator.properties"></properties>
    
        <!--指定特定数据库的jdbc驱动jar包的位置-->
        <classPathEntry location="${jdbc.driverLocation}"/><!--${C:UsersFrankLi.m2
    epositorymysqlmysql-connector-java5.1.24mysql-connector-java-5.1.24.jar}-->
    
        <context id="default" targetRuntime="MyBatis3">
    
            <!-- optional,旨在创建class时,对注释进行控制 -->
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
    
            <!--jdbc的数据库连接 -->
            <jdbcConnection
                    driverClass="${jdbc.driverClass}"
                    connectionURL="${jdbc.connectionURL}"
                    userId="${jdbc.userId}"
                    password="${jdbc.password}">
            </jdbcConnection>
    
    
            <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
    
    
            <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
                targetPackage     指定生成的model生成所在的包名
                targetProject     指定在该项目下所在的路径
            -->
            <javaModelGenerator targetPackage="com.ghc.model"
                                targetProject="src/main/java">
    
                <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
                <property name="enableSubPackages" value="false"/>
                <!-- 是否对model添加 构造函数 -->
                <property name="constructorBased" value="true"/>
                <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
                <property name="trimStrings" value="true"/>
                <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
                <property name="immutable" value="false"/>
            </javaModelGenerator>
    
            <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
            <sqlMapGenerator targetPackage="com.ghc.mapper"
                             targetProject="src/main/java">
                <property name="enableSubPackages" value="false"/>
            </sqlMapGenerator>
    
            <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                    type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                    type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                    type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
            -->
            <javaClientGenerator targetPackage="com.ghc.dao"
                                 targetProject="src/main/java" type="XMLMAPPER">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
    
    
            <table tableName="reguser" domainObjectName="User"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
    
            <table tableName="adminuser" domainObjectName="Admin"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
            <table tableName="configinfo" domainObjectName="Confinfo"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
            <table tableName="grade" domainObjectName="Grade"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
            <table tableName="gradelog" domainObjectName="Gradelog"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
            <table tableName="reginfo" domainObjectName="Reginfo"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
        </context>
    </generatorConfiguration>
    generatorConfig.xml
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.mybatis.generator.api.MyBatisGenerator;
    import org.mybatis.generator.config.Configuration;
    import org.mybatis.generator.config.xml.ConfigurationParser;
    import org.mybatis.generator.internal.DefaultShellCallback;
    
    public class GeneratorSqlmap {
    
        public void generator() throws Exception{
    
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            //指定 逆向工程配置文件
            File configFile = new File("E:\mybatisgenerator\target\classes\generatorConfig.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                    callback, warnings);
            myBatisGenerator.generate(null);
    
        }
        public static void main(String[] args) throws Exception {
            try {
                GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
                generatorSqlmap.generator();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }
    main 函数所在代码
    log4j.rootLogger=DEBUG, Console
    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
    log4j.logger.java.sql.ResultSet=INFO
    log4j.logger.org.apache=INFO
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG
    日志信息配置

    上面都创建修改完成后,使用下面 mysql 的 数据库脚本 创建一个 generatorConfig.xml 里的对应的 表 试验一波

    create schema mybatisgenerator;
    use mybatisgenerator;
    drop table if exists reguser ;
    create table reguser(
        FRED int,
        LONG_VARCHAR_FIELD varchar(100)
    )

    最后,打开 GeneratorSqlmap.java 这个文件,右键运行 main 方法:

    当所有的执行完成,那么 则会生成 com 文件夹 以及 com 文件夹下 对应的文件了。。。。。

     最后,我觉得,这玩意儿只有当表非常多的时候才能看到效率,所以平时学习,还是自己多编写编写mapper 接口,xml配置等,这样既方便又可以练手。

    如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。
  • 相关阅读:
    内存溢出与内存泄露的区别
    <a>标签
    mac上的设置查看环境变量
    css-position
    css-overflow
    css-clear
    mongodb基本操作
    idea使用maven install命令打包(springboot),jar运行时出现没有主清单属性
    linux运行jar报错
    maven deploy时报错
  • 原文地址:https://www.cnblogs.com/Frank99/p/9003743.html
Copyright © 2011-2022 走看看