• mybatis-generator插件——快捷生成Dao、实体类、Mapper配置


    1、mybatis-generator在maven的pom.xml中引用:

    <plugin>
              <groupId>org.mybatis.generator</groupId>
              <artifactId>mybatis-generator-maven-plugin</artifactId>
              <version>1.3.5</version>
              <configuration>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
                <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
              </configuration>
              <dependencies>
                <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>8.0.20</version>
                </dependency>
              </dependencies>
            </plugin>

    2、在resource下创建配置文件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>
        <properties resource="jdbc.properties"></properties>
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 数据库链接URL、用户名、密码 -->
             <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${root}" password="${123456}"> 
            <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!-- 生成模型的包名和位置 -->
            <javaModelGenerator targetPackage="com.suyi.domain" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- 生成的映射文件包名和位置 -->
            <sqlMapGenerator targetPackage="com/suyi/mapper" targetProject="src/main/resource">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.suyi.mapper" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
            <table tableName="category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="trolley" domainObjectName="Trolley" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        </context>

    3、配置maven并运行

     运行maven方法

    注:若以上配置不成提供另一简便方法

    1、新建文件,将mybatis-generator-core.jar和mysql-connection-java.jar放同一个文件。

    2、配置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>
        <!-- 数据库驱动包位置 -->
        <classPathEntry location="D:TOOLSmybatis-generatorgeneratormysql-connector-java-8.0.20.jar" />
        <!-- <classPathEntry location="C:oracleproduct10.2.0db_1jdbclibojdbc14.jar" />-->
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 数据库链接URL、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/xiaomi?useSSL=false&amp;serverTimezone=Asia/Shanghai" userId="root" password="123456">
                <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!-- 生成模型的包名和位置 -->
            <javaModelGenerator targetPackage="com.suyi.domain" targetProject="D:TOOLSmybatis-generatorgeneratorsrc">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- 生成的映射文件包名和位置 -->
            <sqlMapGenerator targetPackage="com.suyi.mapper" targetProject="D:TOOLSmybatis-generatorgeneratorsrc">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.suyi.dao" targetProject="D:TOOLSmybatis-generatorgeneratorsrc">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
            <table tableName="category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="trolley" domainObjectName="Trolley" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        </context>
    </generatorConfiguration>

    3、使用cmd.exe,dos执行  java -jar mybatis-generator-core-1.3.5.jar -configfile generator.xml -overwrite

  • 相关阅读:
    什么是软件质量?
    软件生存周期及其模型是什么?
    给你一个网站,你如何测试?
    jquery中$.get()提交和$.post()提交有区别吗?
    JQuery有几种选择器?
    ajax和layui总结
    md5加密
    Collection接口相关介绍
    JS 中document.URL 和 windows.location.href 的区别
    window.location.href的用法(动态输出跳转)
  • 原文地址:https://www.cnblogs.com/zhai1997/p/13822533.html
Copyright © 2020-2023  润新知