zoukankan      html  css  js  c++  java
  • IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码

    一、安装配置maven以及在Idea中配置maven

    安装过程步骤可以看上面的博文,里面介绍得很详细。

    二、建数据表

    DROP TABLE IF EXISTS `t_user`;
    CREATE TABLE `t_user` (
      `id` varchar(100) NOT NULL,
      `username` varchar(20) DEFAULT NULL,
      `password` varchar(20) DEFAULT NULL,
      `headerPic` varchar(60) DEFAULT NULL,
      `email` varchar(60) DEFAULT NULL,
      `sex` varchar(2) DEFAULT NULL,
      `create_time` datetime DEFAULT NULL,
      `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      `is_delete` int(1) DEFAULT NULL,
      `address` varchar(200) DEFAULT NULL,
      `telephone` varchar(15) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    三、Idea创建maven项目

    1、点击create new project-》maven-》create from archetype->maven-archetype-webapp,然点击next,步骤如图:

    2、填写groupId和ArtifactId:(这两个参数值都是自己定义的),下面这段文字,是网上抄来的,以便大家更好地了解这两个参数。

    groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找。

    groupId一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的groupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,artigactId是tomcat。
      比如我创建一个项目,我一般会将groupId设置为cn.laok,cn表示域为中国,laok是我个人姓名缩写,artifactId设置为testProj,表示你这个项目的名称是testProj,依照这个设置,你的包结构最好是cn.laok.testProj打头的,如果有个UserDao,它的全路径就是cn.laok.testProj.dao.UserDao

    3、点击next,配置maven信息,如图:

    4、点击next,填写项目名称,如图:

    5、创建完成后,项目的结构如图,在生成代码之前,不需要创建其他文件夹,但是需要把resources文件夹设置成Resources Root(右键点击resources文件夹-》Mark Directory As->Resources Root)

    四、配置pom.xml和generatorConfig.xml

    1、在pom.xml中加入以下配置:

      <build>
        <finalName>create-code</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>
    

    2、在resources源文件夹下面创建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>
        <classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" />
        <context id="test" targetRuntime="MyBatis3">
            <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
            <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
            <commentGenerator>
                <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
                <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
                <property name="suppressDate" value="true" />
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->
                <property name="suppressAllComments" value="false" />
            </commentGenerator>
            <!--数据库链接URL,用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/article" userId="root" password="">
            </jdbcConnection>
            <javaTypeResolver>
                <!-- This property is used to specify whether MyBatis Generator should 
                    force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!-- 生成模型的包名和位置 文件夹自己定义-->
            <javaModelGenerator targetPackage="com.test.model"
                                targetProject="target">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- 生成映射文件的包名和位置 文件夹自己定义-->
            <sqlMapGenerator targetPackage="com.test.mapping"
                             targetProject="target">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 文件夹自己定义-->
            <javaClientGenerator type="XMLMAPPER"
                                 targetPackage="com.test.dao" implementationPackage="com.test.dao.impl"  targetProject="target">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
            <!-- 要生成哪些表 -->
            <table tableName="t_user" domainObjectName="user"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false"></table>
        </context>
    </generatorConfiguration>
    

    3、配置完成后,一定要点击Build->Rebuild project,生成target文件夹,不然生产代码的时候是生产在target文件下下面,没有这个文件夹会报错,当然也可以配置生成在其他文件夹下面。项目结构如图:

    特别注意的一点:一定要在配置文件中加入本地的mysql-connector-java-5.1.43-bin.jar,下载地址https://dev.mysql.com/downloads/connector/j/

    然后解压到本地,我的配置如下:<classPathEntry location="D:/Java/lib/mysql-connector-java-5.1.43-bin.jar" />

    这个需要大家根据自己存放的路径配置。

    五、执行生成代码

    1、点击run->Edit configurations,如图:

    2、之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”
    这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。

    3、配置完成后,点击run-》run generator,不出意外的话,在控制台中会出现BUILD SUCCESS的info信息。完整的效果如图所示:

    有写得不对的地方,烦请各位大佬指正,非常感谢。

  • 相关阅读:
    How to install VXDIAG Honda, Toyota and JLR SDD software
    16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
    Cummins INSITE locked and ask for verification code
    How to use BMW Multi Tool 7.3 to replace lost key for BMW X1
    Bleed Brake Master Cylinder with Intelligent Tester IT2
    Porsche Piwis Tester II “No VCI has been detected”,how to do?
    Creader VIII VS. Creader VII+
    How to solve GM MDI cannot complete the installation
    汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
    汽车节温器单片机开发思路
  • 原文地址:https://www.cnblogs.com/jamespan23/p/8064176.html
Copyright © 2011-2022 走看看