由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,

所以查资料发现有现成的工具可以自动生成底层模型类pojo类、Dao接口mapper类、甚至Mapping映射文件xml

生成代码需要的文件和jar包:

(上图文件下载地址:http://download.csdn.net/detail/u012909091/7206091

其中有mybatis框架的jar包,数据库驱动程序jar包以及MyBatis生成器jar包。其中的generatorConfig.xml是需要我们来配置的文件,配置如下:

随便从本地仓库找这几个jar,版本不同也没关系。

generatorConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--数据库驱动-->
  7. <classPathEntry location="mysql-connector-java-5.1.6.jar"/>
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <!--是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="true"/>
  13. </commentGenerator>
  14. <!--数据库链接URL,用户名、密码 -->
  15. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.0.82:13306/program_shopcart" userId="sa" password="">
  16. </jdbcConnection>
  17. <javaTypeResolver>
  18. <property name="forceBigDecimals" value="false"/>
  19. </javaTypeResolver>
  20. <!--生成模型的包名和位置-->
  21. <javaModelGenerator targetPackage="tb.db.mysql.shopcart.mybatis.pojo" targetProject="D:\temp\mysql_mybatis_shopcart">
  22. <property name="enableSubPackages" value="true"/>
  23. <property name="trimStrings" value="true"/>
  24. </javaModelGenerator>
  25. <!--生成映射文件的包名和位置-->
  26. <sqlMapGenerator targetPackage="tb.db.mysql.shopcart.mybatis.xml" targetProject="D:\temp\mysql_mybatis_shopcart">
  27. <property name="enableSubPackages" value="true"/>
  28. </sqlMapGenerator>
  29. <!--生成DAO的包名和位置-->
  30. <javaClientGenerator type="XMLMAPPER" targetPackage="tb.db.mysql.shopcart.mybatis.mapper" targetProject="D:\temp\mysql_mybatis_shopcart">
  31. <property name="enableSubPackages" value="true"/>
  32. </javaClientGenerator>
  33. <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
  34. <table tableName="account_group" domainObjectName="Account_group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  35. <table tableName="account_grouppermissionassign" domainObjectName="Account_grouppermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  36. <table tableName="account_permission" domainObjectName="Account_permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  37. <table tableName="account_role" domainObjectName="Account_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  38. <table tableName="account_rolepermissionassign" domainObjectName="Account_rolepermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  39. <table tableName="account_user" domainObjectName="Account_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  40. <table tableName="account_usergroupassign" domainObjectName="Account_usergroupassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  41. <table tableName="account_userpermissionassign" domainObjectName="Account_userpermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  42. <table tableName="account_userroleassign" domainObjectName="Account_userroleassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  43. </context>
  44. </generatorConfiguration>

1、生成mybatis文件(在IDEA的命令行里面执行)

  1. java -jar mybatis-generator-core-1.4..jar -configfile generatorConfig.xml -overwrite

 2、生成mybatis文件(在控制台里面执行),进入mybatis-generator-core-1.4.0.jar所在的目录下,执行脚本:

  1. java -jar mybatis-generator-core-1.4.0.jar -configfile generatorConfig.xml -overwrite

生成好的文件存放在磁盘目录里

 

MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping的更多相关文章

  1. mybatis generator.xml 配置 自动生成model,dao,mapping

    generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...

  2. MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  3. Mybatis 自动从数据库生成entity,mapping,dao接口

    1.下载需要的jar包 mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.39.jar 2.把上面的jar包放到某个目录,并在该目录下 ...

  4. mybatis自定义代码生成器(Generator)——自动生成model&dao代码

    花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程.感觉generator做的非常不错,给开发者也留足了空间.看完之后在generator的基础上实现了自定义的生 ...

  5. MyBatis generator 生成生成dao model mappper

    MyBatis GeneratorXML配置文件参考 在最常见的用例中,MyBatis Generator(MBG)由XML配置文件驱动. 配置文件告诉MBG: 如何连接到数据库 什么对象要生成,以及 ...

  6. MyBatis Generator生成DAO——序列化

    MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的. 还以为要手工加入(開始是手工加入的),今天遇到分页的问题,才发现生成的时候能够加入插件. 既然分页能够有插件.序列化是 ...

  7. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  8. mybatis Generator生成代码及使用方式

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...

  9. Mybatis Generator生成工具配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

随机推荐

  1. 国内A股16家上市银行的財务数据与股价的因子分析报告(1)(工具:R)

    分析人:BUPT_LX 研究目的 用某些算法对2014年12月份的16家国内A股上市的商业银行当中11项財务数据(资产总计.负债合计.股本.营业收入.流通股A.少数股东权益.净利润.经营活动的现金流量 ...

  2. (转)C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析

    C/C++ 程序设计员应聘常见 面试笔试 试题深入剖析 http://www.nowcoder.com/discuss/1826?type=2&order=0&pos=23&p ...

  3. js获取GET参数

    自定义函数 /*-----------------实现1--------------------*/ function getPar(par){ //获取当前URL var local_url = d ...

  4. android中XRecyclerView控件的使用

    控件的地址:https://github.com/XRecyclerView/XRecyclerView XRecyclerView控件是一个加强版的RecyclerView,可以很方便的实现下拉刷新 ...

  5. Spring MVC中使用errors标签

    先创建一个实体类,后续的验证都基于这个实体类: public class Goods { private String goodsName; private String city; private ...

  6. HDU2717 Catch That Cow 【广搜】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. 算法笔记_213:第七届蓝桥杯软件类决赛部分真题(Java语言C组)

    目录 1 平方末尾 2 七星填数 3 打印数字 4 赢球票 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 平方末尾 平方末尾 能够表示为某个整数的平方的数字称为“平方数” 比如,25,64 虽然无 ...

  8. InfluxDB和MySQL的读写对比测试

    今天进行了InfluxDB和MySQL的对比测试,这里记录下结果,也方便我以后查阅. 操作系统: CentOS6.5_x64InfluxDB版本 : v1.1.0MySQL版本:v5.1.73CPU ...

  9. 字符串匹配算法——BF、KMP、Sunday

    一:Brute force 从源串的第一个字符开始扫描,逐一与模式串的对应字符进行匹配,若该组字符匹配,则检测下一组字符,如遇失配,则退回到源串的第二个字符,重复上述步骤,直到整个模式串在源串中找到匹 ...

  10. 〖Linux〗Kubuntu14.04 平滑字体的设置

    有没有感觉终端的字体锯齿感觉非常强? 经过搜索后发现可以平滑字体显示得更漂亮一点: System Settings > Application Appearance > Fonts I e ...