主要就三步:

1、pom 文件中引入jar包并配置 build 属性

    <dependencies>
<!-- 自动生产mapper Begin! -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<!-- 自动生产mapper End! -->
</dependencies> <build>
<plugins>
<!-- 自动生产mapper Begin! -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!-- 自动生产mapper End! -->
</plugins>
</build>

2、代码中配置 generatorConfig.xml 文件,放入resource根目录下

具体说明可以参考注释.

<?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>
<!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
<classPathEntry location="c:/Users/panghaichen/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" /> <context id="wangyongzhi_mysql_tables" targetRuntime="MyBatis3">
<!-- 防止生成的代码中有很多注释,使用如下配置;想自动生成注释,则使用后面的配置。 -->
<!-- <commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>--> <commentGenerator>
<!-- suppressAllComments设置为false,addRemarkComments设置为true会将数据库中的注释自动添加过来。虽然很多废弃说明,但是可以使用正则替换并删除空行,整体来说还是很方便的,具体使用见后续-->
<property name="suppressAllComments" value="false" />
<property name="suppressDate" value="false" />
<property name="addRemarkComments" value="true" />
</commentGenerator> <!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://10.248.224.12:21202/dmall_virtual_business?useUnicode=true&amp;characterEncoding=UTF-8"
userId="marketing"
password="3KLPHtdmKAwtA18">
</jdbcConnection> <javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver> <!-- 数据表对应的model层输出目录:DO实体类 -->
<javaModelGenerator targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator> <!-- sql mapper 映射配置文件输出目录:XML文件 -->
<sqlMapGenerator targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator> <!-- mybatis3中的mapper接口输出目录:DAO接口类 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.dmall.virtual.dao.auto" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator> <!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO类名-->
<table schema="dmall_virtual_business" tableName="denomination_manage" domainObjectName="DenominationManage"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table> <table schema="dmall_virtual_business" tableName="denomination_manage_log" domainObjectName="DenominationManageLog"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>

3、运行插件生成代码

4、清除自动生成注释中的废弃代码

  1. 在实体类文件中,用正则替换,将包含废弃说明的行替换为空白;

    正则表达式:^.* 大师兄.*$。将大师兄替换为需要的字符串即可。
  2. 删除自动生成的get、set方法;
  3. 使用Ctrl + Alt +L自动格式化代码。然后上述步骤中替换生成的空白行会被自动删除。
  4. 使用 Alt +Insert自动生成get、set方法

上述步骤绝对比你为每个字段添加注释要方便(如果字段太多的话)。

5、参考:

  1. Maven 配置之 MyBatis Generator Maven Plugin (三步学会使用 MyBatis Generator 生成代码)- 四个空格
  2. MyBatis Generator Core – Introduction to MyBatis Generator

    注:官网,及介绍。详细信息可以自行观看。
  3. MyBatis Generator Core – MyBatis Generator XML Configuration File Reference

    注:XML文件全部配置属性可参考以上。有一个中文可参考:Mybatis Generator 最完整配置详解 - 简书
  4. Intellij IDEA 中使用 MyBatis-generator 自动生成 MyBatis 代码 - 志哥的成长笔记 - SegmentFault 思否

IDEA使用mybatis generator自动生成代码的更多相关文章

  1. SpringBoot 添加mybatis generator 自动生成代码插件

    自动生成数据层代码,提高开发效率 1.pom添加插件,并指定配置文件路径 <!-- mybatis generator 自动生成代码插件 --> <plugin> <gr ...

  2. idea中mybatis generator自动生成代码配置 数据库是sqlserver

    好长时间没有写博客了,最近公司要用java语言,开始学习java,属于初学者,今天主要记录一下mybatis generator自动生成代码,首先在如下图的目录中新建两个文件,如下图 generato ...

  3. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  4. IDEA Maven Mybatis generator 自动生成代码

    IDEA Maven Mybatis generator 自动生成代码 一.安装配置maven以及在Idea中配置maven 安装过程步骤可以看上面的博文,里面介绍得很详细. 二.建数据表 DROP ...

  5. IDEA Maven Mybatis generator 自动生成代码(实例讲解)(转)

    IDEA Maven Mybatis generator 自动生成代码(实例讲解) MyBatis Generator • 简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器,可以快速的 ...

  6. 使用Mybatis Generator自动生成代码

    MyBatis Generator(MBG)是MyBatis MyBatis 和iBATIS的代码生成器.它将为所有版本的MyBatis以及版本2.2.0之后的iBATIS版本生成代码.它将内省数据库 ...

  7. Mybatis generator 自动生成代码(2)

    最近准备开始做一个项目,需要开始手动创建sql,于是将Mybatis generator 工具功能强化了下. 首先,这里引入到版本一点的包 <dependency> <groupId ...

  8. Mybatis generator 自动生成代码

    开发项目的时候,表很多,是不可能一点点的自己去写xml ,dao文件的,这里就需要用到代码的自动生成工具了. 第一步:导入jar包,当然,这之前,基本环境,像mybatis,数据库之类的都得搭建好. ...

  9. mybatis generator自动生成代码时 只生成了insert 而没有其他的

    mybatis框架提供了非常好用的逆向工程插件,但是在使用过程中会有很多问题. 我在使用中就遇到了只生成insert和insertSeletive方法,而不生成其他根据primary key查询更新删 ...

随机推荐

  1. 我是如何理解Android的Handler模型_1

    Handler Message类似于旧时的电话系统,对应关系如下: 电话局->Handler 电话机->Message 接线员->handlerMessage 接线员的工作-> ...

  2. 03_已解决 [salt.master :2195][ERROR ][6219] Failed to allocate a jid. The requested returner 'mysql' could not be loaded.

    总结: 对于python2.7环境下的salt来说,要安装pip install mysql-python 对于python3环境下的salt来说,pip install mysqlclient的时候 ...

  3. python自动华 (一)

    Python自动化 [第一篇]:Python简介和入门 Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单” ...

  4. mysql远程服务密码修改

    GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";    FLUSH PRIVILEGE ...

  5. vue使用Echarts图表

    vue使用Echarts图表 童话_xxv 关注  0.5 2018.12.11 09:09* 字数 325 阅读 1456评论 2喜欢 13 在开发后台系统时,使用图表进行数据可视化,这样会使数据更 ...

  6. 04_实时监控本机内存和硬盘剩余空间,剩余内存小于 500M、根分区剩余空间小于 1000M 时,发送报警邮件给root 管理员.

    #!/bin/bash#提取根分区剩余空间disk_size=$(df -h / | awk '/\//{print $4}')#提取内存剩余空间disk_size=$(df -h / | awk ' ...

  7. ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

    https://blog.csdn.net/qq_15071263/article/details/78459087 1. 警告解读 ** WARNING ** : Your ApplicationC ...

  8. Liunx之基础学习

    用户提权命令之-sudo sudo命令用来以其他身份来执行命令,预设的身份为root.在/etc/sudoers中设置了可执行sudo指令的用户.若其未经授权的用户企图使用sudo,则会发出警告的邮件 ...

  9. MySQL inodb cluster部署

    innodb cluster是基于组复制来实现的. 搭建一套MySQL的高可用集群innodb. 实验环境: IP 主机名 系统 软件 192.168.91.46 master RHEL7.4 mys ...

  10. 浅谈 es6 箭头函数, reduce函数介绍

    今天来谈一下箭头函数, es6的新特性 首先我们来看下箭头函数长什么样子, let result = (param1, param2) => param1+param2; 上述代码 按照以前书写 ...