mybatis-generator有三种用法:命令行、eclipse插件、maven插件。个人觉得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。 下面是从官网上的截图:(不过官网 www.mybatis.org 最近一段时间,好象已经挂了

maven插件1:

  1. pom添加:
<!-- 代码自动生成器 --><build><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>
  1. 添加文件generatorl.xml 或者 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><classPathEntrylocation="D:\develop\maven\maven_repository\mysql\mysql-connector-java\5.1.31\mysql-connector-java-5.1.31.jar" /><context id="MysqlTables" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!--数据库连接的信息:驱动类、连接地址、用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/wbosp" userId="root"password="root"></jdbcConnection><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal --><javaTypeResolver><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- targetProject:自动生成代码的位置 --><javaModelGenerator targetPackage="com.hg.provider.model"targetProject="src\main\java"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><sqlMapGenerator targetPackage="com.hg.provider.dao"targetProject="src\main\java"><property name="enableSubPackages" value="true" /></sqlMapGenerator><javaClientGenerator type="XMLMAPPER"targetPackage="com.hg.provider.dao" targetProject="src\main\java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --><table schema="dispatch" tableName="user_t" domainObjectName="User"enableCountByExample="false" enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false">
<!--设置生成代码名称是否与数据库字段一致,(默认是false不一致,驼峰命名)-->
<property name="useActualColumnNames" value="true" /></table><!-- 可以配置多个表 --><!-- <table schema="dispatch" tableName="employee" domainObjectName="Employee"enableCountByExample="false" enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false"><property name="useActualColumnNames" value="false" /></table> --></context>
</generatorConfiguration>

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:\develop\maven\repository\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar"
/><!--
<classPathEntry
location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar"
/>--><context id="DB2Tables"
targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!-- 数据库链接URL、用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot"
userId="root" password="root"><!--<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.andy.model" targetProject="src\main\java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成的映射文件包名和位置 --><sqlMapGenerator targetPackage="com.andy.mapping" targetProject="src\main\java"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="com.andy.dao"
targetProject="src\main\java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成那些表(更改tableName和domainObjectName就可以) --><table tableName="t_user" domainObjectName="UserInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
/>
<!--<table tableName="course_info"
domainObjectName="CourseInfo" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false"
/>
<table
tableName="course_user_info"
domainObjectName="CourseUserInfo"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false" />--></context>
</generatorConfiguration>
  1. dos: mvn mybatis-generator:generate 选中pom.xml运行Run As mybatis-generator:generate 因为 eclispe 已经集成了mvn 所以就不需要 mvn 这三各字母了

生成代码字段名默认按驼峰命名
如果有特殊用途,现想让其生成的名称与数据库中一样以下配置可以 <property name="useActualColumnNames" value="true" /> 最后给一些小技巧:

  • 建表时,字段名称建议用"_"分隔多个单词,比如:AWB_NO、REC_ID...,这样生成的entity,属性名称就会变成漂亮的驼峰命名,即:awbNo、recId
  • oracle中,数值形的字段,如果指定精度,比如Number(12,2),默认生成entity属性是BigDecimal型 ,如果不指定精度,比如:Number(9),指默认生成的是Long型
  • oracle中的nvarchar/nvarchar2,mybatis-generator会识别成Object型,建议不要用nvarchar2,改用varchar2

mybatis-generator相关推荐

  1. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码

    2019独角兽企业重金招聘Python工程师标准>>> 由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所 ...

  2. SpringBoot如何自动生成实体类和Dao层以及映射文件(mybatis generator 自动生成代码)

    一.首先添加自动生成代码插件 <!-- mybatis generator 自动生成代码插件 生成时解除注释 --><plugin><groupId>org.myb ...

  3. idea mybatis generator插件_在idea中使用mybatis generator逆向工程生成代码

    用maven工具生成mybatis的代码和映射文件. 1.配置pom.xml文件 在pom.xml下添加插件如下: <build> <finalName>zsxt</fi ...

  4. Spring Boot项目利用MyBatis Generator进行数据层代码自动生成

    概 述 MyBatis Generator (简称 MBG) 是一个用于 MyBatis和 iBATIS的代码生成器.它可以为 MyBatis的所有版本以及 2.2.0之后的 iBATIS版本自动生成 ...

  5. 从命令行及java程序运行MyBatis Generator 1.3.x生成MyBatis3.x代码

    为什么80%的码农都做不了架构师?>>>    近期因为项目需要,调研了myBatis 3.x的使用,当然,顺便也就研究了一下使用Generator来通过逆向工程生成pojo,map ...

  6. mysql分页取数每一页生成xml_让MyBatis Generator产生的代码支持分页

    本文提供一种方法,让MyBatis Generator产生的代码支持分页,  适用于MySQL. 分析 如果要获取分页信息,使用MySQL语句,我们需要怎么做呢? select * from t_us ...

  7. MyBatis Generator 详解

    MyBatis Generator中文文档 MyBatis Generator中文文档地址: http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看 ...

  8. Maven多模块项目使用MyBatis Generator

    开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...

  9. MyBatis Generator模板

    注:注意替换红色部分 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorCo ...

  10. Mybatis Generator的使用

    在写代码过程中,常常要写一些简单的CURD操作,为了能够把时间用在业务逻辑上,看了Mybatis Generator生成工具,根据官网的文档,改成适合自己使用的生成器. mybatis generat ...

最新文章

  1. CentOS的el5, el6, el7代表什么
  2. 它们的定义Activity跳转动画
  3. 34 SD配置-销售凭证设置-定义销售凭证类型
  4. 数据库课程设计(学校运动会管理系统)2021-9-21
  5. f018计算机辅助设计是什么,F018,丝印Marking-电子元件丝印查询
  6. 使得在虚拟机能复制粘贴 -- Linux安装增强
  7. 儿童python编程教程-一款儿童编程入门的理想工具——PythonTurtle
  8. 一款完整的企业级CMS站群系统源码,采用了比较典型的三层架构技术,源码分享
  9. python实现匿名发邮件_Python 实现邮件发送功能(初级)
  10. NOI2016 滚粗记
  11. YOLOv5超详细的入门级教程(训练篇)(二)——VOC2007模型复现
  12. 数字电子钟Multisim仿真(数电课设+实验报告)
  13. 堆叠注入-强网杯2019随便注
  14. cs224n学习笔记
  15. 【计算机网络】实验3:虚拟机配置测试实验
  16. CDA深度分享:数据自由之路——数据产品及数据分析职业发展路径
  17. 网能云平台,掌聚网能助力机房运维的利器
  18. 下列哪项不是python中对文件的读取操作-Python—文件读写操作
  19. SugarCRM 自定义选择按钮
  20. 这,这,是个神人,我喜欢

热门文章

  1. .condarc(conda 配置文件)、换国内源
  2. 推理集 —— 特殊的时间
  3. CentOS/Ubuntu 下 MySQL 的安装
  4. 被平均(统计平均)的陷阱
  5. hitool备份3798固件方法_创维E900s海思3798芯片当贝桌面不拆机通刷固件及刷机教程201910版...
  6. 国外大神一张图学会python-12306看了会沉默,国外大神利用机器学习15分钟破解网站验证码!...
  7. python爬虫代码-学Python=写爬虫?不用代码也能爬下95%网站的数据!
  8. 从零开始学习python编程-从0开始的Python学习014面向对象编程(推荐)
  9. python工资一般多少西安-西安Python和人工智能的薪资前景到底怎么样?
  10. 学python需要什么基础-要学 Python 需要怎样的基础?