• generator自动生成mybatis配置和类信息


    我在使用mybatis写类与数据表中的映射时,由于表的列名太多,出现的问题也很多,列名与类对象属性的对应要一个一个写的话很比较复杂。

    为了方便和减少失误,可以使用 MyBatis 提供的 code generator 自动生成 mybatis 的 xml 映射文件、Model 、 Map 等信息,大家可以到

     MyBatis 官网下载一个 mybatis-generator-core-1.3.2-bundle ,在压缩包中找到 lib 下的 jar 包。然后编写 mybatis.xml ,并执行即可

     


    <generatorConfiguration>
    <!-- 这里是加载JDBC驱动的jar包地址-->
    <classPathEntry location="C:UsersAdministrator.WJ-20131202XSPSDesktopmybatis-generator-core-1.3.2libmysql-connector-java-5.1.7-bin.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

    <!-- 是否去除自动生成的注释 true:是,false:否-->
    <commentGenerator>
    <property name="suppressAllComments" value="true" />
    </commentGenerator>

    <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码-->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://localhost/workloads" userId="root" password="mysql106366">
    </jdbcConnection>

    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal-->
    <javaTypeResolver>
    <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!--应用实体的生成信息-->
    <javaModelGenerator targetPackage="com.tly.entity"
    targetProject="C: able">
    <property name="enableSubPackages" value="true" />
    <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!--sqlMapper XML文件的生成信息,包括生成路径等 targetProject表示生成文件存放的位置-->
    <sqlMapGenerator targetPackage="com.tly.mapping" targetProject="C: able">
    <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER"
    targetPackage="com.tly.dao" targetProject="C: able">
    <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!--数据库中表和程序里实体的对应关系-->
    <table tableName="project" domainObjectName="ClassRecord" enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
    </table>
    <table tableName="project_score" domainObjectName="Parameter" enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
    </table>
    </context>
    </generatorConfiguration>


    在 cmd 中输入 java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml –overwrite ,这里的路径都是绝对的,为了方便,我们可以在 mybatis-generator-core-1.3.2 所在的文件夹下建一个 bat 文件,输入

    Txt代码  
    1. @echo off  
    2. echo==========mybatis开始生成代码================  
    3. java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite  
    4. echo==========mybatis生成代码完毕================  

    最后我们可以按住shift建点击右键,直接在该文件夹处打开命令窗口,直接运行 bat 文件即可

  • 相关阅读:
    如何让Android手机顺利连接上PC
    JNI简介
    SQLite代码与工具
    一个JNI的简单示例
    SQLite的WAL机制
    NDK编程中遇到的问题之一 “/androidndk/build/gmsl/__gmsl:512: *** nonnumeric second argument to `wordlist' function”
    与Unix相关的一些规范与组织
    php获得ip,真实IP
    div里的文字左右居中 上下居中
    php防止刷点击数的方法
  • 原文地址:https://www.cnblogs.com/John-Lyn/p/3830242.html
Copyright © 2020-2023  润新知