zoukankan      html  css  js  c++  java
  • Mybatis-Generator自动生成代码

      在使用mybatis开发的过程中,通常我们会给数据库的每张表编写对应的model、dao、mapping,虽然很简单,但是工作量很大,所以通常会使用代码生成器Mybatis-Generator帮我们自动生成。

    数据库表如图所示,下面将对其中的几张表进行代码生成。

      

    1.准备mybatis-generator-core-1.3.6.jar,数据库驱动包mysql-connector-java-5.1.39-bin.jar

    2.编写generator.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="C:UsersAdministratorDesktopgeneratormysql-connector-java-5.1.39-bin.jar" /> 
        <!--<classPathEntry location="C:oracleproduct10.2.0db_1jdbclibojdbc14.jar" />-->
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
            <!-- 数据库链接URL、用户名、密码 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/zblog" userId="root" password="123"> 
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
            <!-- 生成模型的包名和位置 -->
            <javaModelGenerator targetPackage="zang.model" targetProject="C:UsersAdministratorDesktopgeneratorsrc">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            <!-- 生成的映射文件包名和位置 -->
            <sqlMapGenerator targetPackage="zang.mapping" targetProject="C:UsersAdministratorDesktopgeneratorsrc">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置 -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="zang.dao" targetProject="C:UsersAdministratorDesktopgeneratorsrc">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!-- 要生成哪些表(更改tableName和domainObjectName就可以) -->
            <table tableName="article" domainObjectName="Article" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            <table tableName="category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
            
        </context>
    </generatorConfiguration>

    3.根据generator.xml文件中配置的代码文件生成位置,在该目录下创建空文件夹src,到此步,目录如下

      

    4.cmd进入该目录,执行如下语句

    java -jar mybatis-generator-core-1.3.6.jar -configfile generator.xml -overwrite

      

    注意:如果出现报错 XML Parser Error on line 1: 前言中不允许有内容。则很可能是generator.xml文件编码问题,需要使用notepad对文件进行以UTF-8无BOM编码格式进行保存。

       

    执行之后,代码按照配置自动生成:

      

  • 相关阅读:
    手机市场价格尚待规范
    Hotmail的2G邮箱被收回,只剩250M了
    网格50题(zz)
    Wallop介绍
    Hotmail邮箱居然还有2G
    IE 7.0抛弃Win2000用户?(zz)
    祝各位Blogger新春快乐!
    Gmail Invitation
    A CS Research Topic Generator(zz)
    今天"做大岁"
  • 原文地址:https://www.cnblogs.com/zjfjava/p/8598612.html
Copyright © 2011-2022 走看看