zoukankan      html  css  js  c++  java
  • (二)一个很好用的自动生成工具——mybatis generator

    mybatis generator-自动生成代码

    准备材料:

      一个文件夹,一个数据库的驱动包,mybatis-generator-core-1.3.5.jar,一条生成语句

      如图:(我用的是derby数据库,使用其他数据库需修改相应的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="derby.jar" />
     8      <context id="Derby"    targetRuntime="MyBatis3">
     9     <!--把数据库注释添加到java文件中-->
    10          <commentGenerator>
    11              <property name="suppressDate" value="true"/>
    12              <property name="suppressAllComments" value="true"/>
    13          </commentGenerator>
    14          <!--数据库链接地址账号密码-->
    15          <jdbcConnection driverClass="org.apache.derby.jdbc.EmbeddedDriver" connectionURL="jdbc:derby:E:/shiny/DdlUtils-test/mydb" userId="root" password="root">
    16          </jdbcConnection>
    17          <javaTypeResolver>
    18              <property name="forceBigDecimals" value="false"/>
    19          </javaTypeResolver>
    20          <!--生成Model类存放位置-->
    21          <javaModelGenerator targetPackage="com.standard.model" targetProject="src">
    22             <property name="enableSubPackages" value="true"/>
    23            <!--  <property name="trimStrings" value="true"/>  -->
    24          </javaModelGenerator>
    25          <!--生成映射文件存放地址-->
    26         <sqlMapGenerator targetPackage="com.standard.mapper" targetProject="src">
    27              <property name="enableSubPackages" value="true"/>
    28          </sqlMapGenerator>
    29          <!--生成Dao类存放地址-->
    30          <javaClientGenerator type="XMLMAPPER" targetPackage="com.standard.mapper" targetProject="src">
    31              <property name="enableSubPackages" value="true"/>
    32          </javaClientGenerator>
    33          
    34         <!--生成对应表及表名-->
    35         
    36      <table tableName="standard_user" domainObjectName="User"  enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    37           
    38          </table>
    39      <table tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    40 
    41     </context>
    42  </generatorConfiguration>    

    可以根据表中的注释配置相应的内容:

      1.数据库驱动:location  jar包名

      2.数据库链接地址(账号密码)

      3.model类、映射文件、接口类存放地址(Java项目中的包目录)

      4.需要生成映射的表名

    ----------------------------------------------------------------------------------------

    <commentGenerator>
      <property name="suppressDate" value="true"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>

    可配置数据库注释添加到Java文件中

    ----------------------------------------------------------------------------------------

    在当前目录打开cmd

      生成语句:java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite

      生成结果:

    最后将src下的文件夹复制到项目的src目录下即可

      xml中还有很多配置,还未用到,之后有机会再深入研究~

  • 相关阅读:
    做一个会切图的前端
    居中详解
    《MFC游戏开发》笔记二 建立工程、调整窗口
    [置顶] 《MFC游戏开发》笔记一 系列简介
    SRM 584 第一次玩TopCoder。。。只水题一道。。。
    c语言排序算法总结
    uva10004 Bicoloring 黑白染色问题,DFS
    uva 784 Maze Exploration 染色 搜索水题 DFS
    采用最低松弛度优先调度的实时系统调度程序
    UVA 539 The Settlers of Catan dfs找最长链
  • 原文地址:https://www.cnblogs.com/zuzZ/p/8087135.html
Copyright © 2011-2022 走看看